码迷,mamicode.com
首页 > 其他好文 > 详细

hihocoder1079 离散化

时间:2018-06-11 22:14:34      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:code   线段树   离散   AC   col   测试   erase   hihocoder   begin   

思路:
线段树 + 离散化。

测试用例:

3 10

1 10

1 3

6 10

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int, int> pii;
 4 const int N = 200005;
 5 int tree[N << 2], lazy[N << 2], a[N];
 6 
 7 void init()
 8 {
 9     memset(tree, 0, sizeof tree);
10     memset(lazy, 0, sizeof lazy);
11 }
12 
13 void pushdown(int num, int cl, int cr)
14 {
15     if (!lazy[num]) return;
16     tree[num << 1] = lazy[num] * cl;
17     tree[num << 1 | 1] = lazy[num] * cr;
18     lazy[num << 1] = lazy[num];
19     lazy[num << 1 | 1] = lazy[num];
20     lazy[num] = 0;
21 }
22 
23 void update(int num, int l, int r, int x, int y, int p)
24 {
25     if (x <= l && y >= r) { tree[num] = (r - l + 1) * p; lazy[num] = p; return; }
26     int m = l + r >> 1;
27     pushdown(num, m - l + 1, r - m);
28     if (x <= m) update(num << 1, l, m, x, y, p);
29     if (y >= m + 1) update(num << 1 | 1, m + 1, r, x, y, p);
30     tree[num] = tree[num << 1] + tree[num << 1 | 1];
31 }
32 
33 int query(int num, int l, int r, int x, int y)
34 {
35     if (x <= l && y >= r) return tree[num];
36     int m = l + r >> 1;
37     pushdown(num, m - l + 1, r - m);
38     int ans = 0;
39     if (x <= m) ans += query(num << 1, l, m, x, y);
40     if (y >= m + 1) ans += query(num << 1 | 1, m + 1, r, x, y);
41     return ans;
42 }
43 
44 int main()
45 {
46     ios::sync_with_stdio(false);
47     init();
48     int n, l, x, y;
49     cin >> n >> l;
50     vector<pii> q;
51     vector<int> v;
52     for (int i = 0; i < n; i++)
53     {
54         cin >> x >> y;
55         q.push_back(pii(x, y));
56         v.push_back(x); v.push_back(y);
57     }
58     sort(v.begin(), v.end());
59     v.erase(unique(v.begin(), v.end()), v.end());
60     map<int, int> mp;
61     for (int i = 0; i < v.size(); i++) mp[v[i]] = 2 * i + 1;
62     int ans = 0, m = v.size() * 2;
63     for (int i = n - 1; i >= 0; i--)
64     {
65         int x = mp[q[i].first], y = mp[q[i].second];
66         if (query(1, 1, m, x, y) < y - x + 1) ans++;
67         update(1, 1, m, x, y, 1);
68     }
69     cout << ans << endl;
70     return 0;
71 }

 

hihocoder1079 离散化

标签:code   线段树   离散   AC   col   测试   erase   hihocoder   begin   

原文地址:https://www.cnblogs.com/wangyiming/p/9168898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!