#include <bits/stdc++.h>
using namespace std;
#define lg long long
#define mid (lr+hr)/2
const lg N = 2e5+5;
lg l[N], r[N], anc[N][20], n, seg[(N << 3)][2];
void doLazy(lg node, lg lr, lg hr)
{
if(!seg[node][1]) return;
seg[node][0] += seg[node][1]*(hr-lr+1);
if(lr != hr)
{
for(int ch = node*2; ch <= node*2+1; ch++)
{
seg[ch][1] += seg[node][1];
}
}
seg[node][1] = 0;
}
void upd(lg node, lg lr, lg hr, lg lq, lg hq, lg val)
{
doLazy(node, lr, hr);
if(lr > hq || lq > hr) return;
if(lq <= lr & hr <= hq)
{
seg[node][1] += val;
doLazy(node, lr, hr);
return;
}
upd(node << 1, lr, mid, lq, hq, val);
upd(node << 1 | 1, mid+1, hr, lq, hq, val);
seg[node][0] = seg[node << 1][0]+seg[node << 1 | 1][0];
return;
}
lg get(lg node, lg lr, lg hr, lg lq, lg hq)
{
doLazy(node, lr, hr);
if(lr > hq || lq > hr) return 0;
if(lq <= lr && hr <= hq) return seg[node][0];
return (get(node << 1, lr, mid, lq, hq)+get(node << 1 | 1, mid+1, hr, lq, hq));
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
lg n;
cin >> n;
unordered_map<lg, lg> mp;
lg id = 1;
set<lg> o;
for(int i = 1; i <= n; i++)
{
cin >> l[i] >> r[i];
o.insert(l[i]);
o.insert(r[i]);
}
for(auto it : o)
{
mp[it] = id++;
}
for(int i = 1; i <= n; i++)
{
l[i] = mp[l[i]];
r[i] = mp[r[i]];
}
lg en = 1;
for(int i = 1; i <= n; i++)
{
while(en <= n)
{
if(get(1, 1, 4e5, l[en], r[en]))
{
break;
}
upd(1, 1, 4e5, l[en], r[en], 1);
en++;
}
anc[i][0] = en-1;
upd(1, 1, 4e5, l[i], r[i], -1);
}
for(int i = n; i >= 1; i--)
{
for(int j = 1; j < 19; j++)
{
anc[i][j] = anc[anc[i][j-1]+1][j-1];
}
}
lg q;
cin >> q;
while(q--)
{
lg a, b;
cin >> a >> b;
lg ans = 0;
for(int i = 18; i >= 0 && a <= b; i--)
{
if(anc[a][i] <= b && anc[a][i])
{
ans += (1ll << i);
a = anc[a][i]+1;
}
}
if(a <= b) ans++;
cout << ans << '\n';
}
}
Compilation message
Main.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int, long long int)':
Main.cpp:30:8: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
30 | if(lq <= lr & hr <= hq)
| ~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
714 ms |
86160 KB |
Output is correct |
2 |
Correct |
725 ms |
86008 KB |
Output is correct |
3 |
Correct |
708 ms |
85540 KB |
Output is correct |
4 |
Correct |
776 ms |
86132 KB |
Output is correct |
5 |
Correct |
807 ms |
88256 KB |
Output is correct |
6 |
Correct |
168 ms |
32876 KB |
Output is correct |
7 |
Correct |
176 ms |
34768 KB |
Output is correct |
8 |
Correct |
179 ms |
34928 KB |
Output is correct |
9 |
Correct |
182 ms |
34808 KB |
Output is correct |
10 |
Correct |
184 ms |
34904 KB |
Output is correct |
11 |
Correct |
435 ms |
87268 KB |
Output is correct |
12 |
Correct |
446 ms |
84480 KB |
Output is correct |
13 |
Correct |
439 ms |
84128 KB |
Output is correct |
14 |
Correct |
494 ms |
87328 KB |
Output is correct |
15 |
Correct |
447 ms |
86204 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
173 ms |
35048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
5724 KB |
Output is correct |
2 |
Correct |
13 ms |
7876 KB |
Output is correct |
3 |
Correct |
13 ms |
7736 KB |
Output is correct |
4 |
Correct |
13 ms |
9820 KB |
Output is correct |
5 |
Correct |
13 ms |
7772 KB |
Output is correct |
6 |
Correct |
6 ms |
4444 KB |
Output is correct |
7 |
Correct |
7 ms |
4440 KB |
Output is correct |
8 |
Correct |
7 ms |
4444 KB |
Output is correct |
9 |
Correct |
6 ms |
4444 KB |
Output is correct |
10 |
Correct |
9 ms |
4444 KB |
Output is correct |
11 |
Correct |
9 ms |
7776 KB |
Output is correct |
12 |
Correct |
10 ms |
5720 KB |
Output is correct |
13 |
Correct |
8 ms |
7772 KB |
Output is correct |
14 |
Correct |
9 ms |
7784 KB |
Output is correct |
15 |
Correct |
10 ms |
7768 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
6 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
5724 KB |
Output is correct |
2 |
Correct |
13 ms |
7876 KB |
Output is correct |
3 |
Correct |
13 ms |
7736 KB |
Output is correct |
4 |
Correct |
13 ms |
9820 KB |
Output is correct |
5 |
Correct |
13 ms |
7772 KB |
Output is correct |
6 |
Correct |
6 ms |
4444 KB |
Output is correct |
7 |
Correct |
7 ms |
4440 KB |
Output is correct |
8 |
Correct |
7 ms |
4444 KB |
Output is correct |
9 |
Correct |
6 ms |
4444 KB |
Output is correct |
10 |
Correct |
9 ms |
4444 KB |
Output is correct |
11 |
Correct |
9 ms |
7776 KB |
Output is correct |
12 |
Correct |
10 ms |
5720 KB |
Output is correct |
13 |
Correct |
8 ms |
7772 KB |
Output is correct |
14 |
Correct |
9 ms |
7784 KB |
Output is correct |
15 |
Correct |
10 ms |
7768 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
6 ms |
4700 KB |
Output is correct |
18 |
Correct |
68 ms |
8620 KB |
Output is correct |
19 |
Correct |
63 ms |
8408 KB |
Output is correct |
20 |
Correct |
70 ms |
10716 KB |
Output is correct |
21 |
Correct |
63 ms |
8532 KB |
Output is correct |
22 |
Correct |
65 ms |
6632 KB |
Output is correct |
23 |
Correct |
56 ms |
5416 KB |
Output is correct |
24 |
Correct |
59 ms |
5348 KB |
Output is correct |
25 |
Correct |
59 ms |
5460 KB |
Output is correct |
26 |
Correct |
56 ms |
5200 KB |
Output is correct |
27 |
Correct |
55 ms |
5248 KB |
Output is correct |
28 |
Correct |
43 ms |
7956 KB |
Output is correct |
29 |
Correct |
48 ms |
6220 KB |
Output is correct |
30 |
Correct |
44 ms |
10008 KB |
Output is correct |
31 |
Correct |
45 ms |
5980 KB |
Output is correct |
32 |
Correct |
45 ms |
8124 KB |
Output is correct |
33 |
Correct |
1 ms |
4440 KB |
Output is correct |
34 |
Correct |
43 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
824 ms |
88380 KB |
Output is correct |
2 |
Correct |
793 ms |
91328 KB |
Output is correct |
3 |
Correct |
693 ms |
87520 KB |
Output is correct |
4 |
Correct |
701 ms |
85444 KB |
Output is correct |
5 |
Correct |
693 ms |
89216 KB |
Output is correct |
6 |
Correct |
172 ms |
38400 KB |
Output is correct |
7 |
Correct |
181 ms |
36272 KB |
Output is correct |
8 |
Correct |
188 ms |
38392 KB |
Output is correct |
9 |
Correct |
174 ms |
36176 KB |
Output is correct |
10 |
Correct |
187 ms |
38736 KB |
Output is correct |
11 |
Correct |
404 ms |
84680 KB |
Output is correct |
12 |
Correct |
462 ms |
91580 KB |
Output is correct |
13 |
Correct |
395 ms |
84676 KB |
Output is correct |
14 |
Correct |
476 ms |
88980 KB |
Output is correct |
15 |
Correct |
492 ms |
91348 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
182 ms |
39064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
714 ms |
86160 KB |
Output is correct |
2 |
Correct |
725 ms |
86008 KB |
Output is correct |
3 |
Correct |
708 ms |
85540 KB |
Output is correct |
4 |
Correct |
776 ms |
86132 KB |
Output is correct |
5 |
Correct |
807 ms |
88256 KB |
Output is correct |
6 |
Correct |
168 ms |
32876 KB |
Output is correct |
7 |
Correct |
176 ms |
34768 KB |
Output is correct |
8 |
Correct |
179 ms |
34928 KB |
Output is correct |
9 |
Correct |
182 ms |
34808 KB |
Output is correct |
10 |
Correct |
184 ms |
34904 KB |
Output is correct |
11 |
Correct |
435 ms |
87268 KB |
Output is correct |
12 |
Correct |
446 ms |
84480 KB |
Output is correct |
13 |
Correct |
439 ms |
84128 KB |
Output is correct |
14 |
Correct |
494 ms |
87328 KB |
Output is correct |
15 |
Correct |
447 ms |
86204 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
173 ms |
35048 KB |
Output is correct |
18 |
Correct |
13 ms |
5724 KB |
Output is correct |
19 |
Correct |
13 ms |
7876 KB |
Output is correct |
20 |
Correct |
13 ms |
7736 KB |
Output is correct |
21 |
Correct |
13 ms |
9820 KB |
Output is correct |
22 |
Correct |
13 ms |
7772 KB |
Output is correct |
23 |
Correct |
6 ms |
4444 KB |
Output is correct |
24 |
Correct |
7 ms |
4440 KB |
Output is correct |
25 |
Correct |
7 ms |
4444 KB |
Output is correct |
26 |
Correct |
6 ms |
4444 KB |
Output is correct |
27 |
Correct |
9 ms |
4444 KB |
Output is correct |
28 |
Correct |
9 ms |
7776 KB |
Output is correct |
29 |
Correct |
10 ms |
5720 KB |
Output is correct |
30 |
Correct |
8 ms |
7772 KB |
Output is correct |
31 |
Correct |
9 ms |
7784 KB |
Output is correct |
32 |
Correct |
10 ms |
7768 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
6 ms |
4700 KB |
Output is correct |
35 |
Correct |
68 ms |
8620 KB |
Output is correct |
36 |
Correct |
63 ms |
8408 KB |
Output is correct |
37 |
Correct |
70 ms |
10716 KB |
Output is correct |
38 |
Correct |
63 ms |
8532 KB |
Output is correct |
39 |
Correct |
65 ms |
6632 KB |
Output is correct |
40 |
Correct |
56 ms |
5416 KB |
Output is correct |
41 |
Correct |
59 ms |
5348 KB |
Output is correct |
42 |
Correct |
59 ms |
5460 KB |
Output is correct |
43 |
Correct |
56 ms |
5200 KB |
Output is correct |
44 |
Correct |
55 ms |
5248 KB |
Output is correct |
45 |
Correct |
43 ms |
7956 KB |
Output is correct |
46 |
Correct |
48 ms |
6220 KB |
Output is correct |
47 |
Correct |
44 ms |
10008 KB |
Output is correct |
48 |
Correct |
45 ms |
5980 KB |
Output is correct |
49 |
Correct |
45 ms |
8124 KB |
Output is correct |
50 |
Correct |
1 ms |
4440 KB |
Output is correct |
51 |
Correct |
43 ms |
5456 KB |
Output is correct |
52 |
Correct |
824 ms |
88380 KB |
Output is correct |
53 |
Correct |
793 ms |
91328 KB |
Output is correct |
54 |
Correct |
693 ms |
87520 KB |
Output is correct |
55 |
Correct |
701 ms |
85444 KB |
Output is correct |
56 |
Correct |
693 ms |
89216 KB |
Output is correct |
57 |
Correct |
172 ms |
38400 KB |
Output is correct |
58 |
Correct |
181 ms |
36272 KB |
Output is correct |
59 |
Correct |
188 ms |
38392 KB |
Output is correct |
60 |
Correct |
174 ms |
36176 KB |
Output is correct |
61 |
Correct |
187 ms |
38736 KB |
Output is correct |
62 |
Correct |
404 ms |
84680 KB |
Output is correct |
63 |
Correct |
462 ms |
91580 KB |
Output is correct |
64 |
Correct |
395 ms |
84676 KB |
Output is correct |
65 |
Correct |
476 ms |
88980 KB |
Output is correct |
66 |
Correct |
492 ms |
91348 KB |
Output is correct |
67 |
Correct |
1 ms |
4444 KB |
Output is correct |
68 |
Correct |
182 ms |
39064 KB |
Output is correct |
69 |
Correct |
867 ms |
92100 KB |
Output is correct |
70 |
Correct |
925 ms |
95064 KB |
Output is correct |
71 |
Correct |
844 ms |
88208 KB |
Output is correct |
72 |
Correct |
868 ms |
93752 KB |
Output is correct |
73 |
Correct |
864 ms |
92188 KB |
Output is correct |
74 |
Correct |
363 ms |
42444 KB |
Output is correct |
75 |
Correct |
330 ms |
42072 KB |
Output is correct |
76 |
Correct |
329 ms |
42532 KB |
Output is correct |
77 |
Correct |
295 ms |
42060 KB |
Output is correct |
78 |
Correct |
271 ms |
42104 KB |
Output is correct |
79 |
Correct |
556 ms |
94272 KB |
Output is correct |
80 |
Correct |
476 ms |
90016 KB |
Output is correct |
81 |
Correct |
485 ms |
89572 KB |
Output is correct |
82 |
Correct |
508 ms |
92608 KB |
Output is correct |
83 |
Correct |
501 ms |
90824 KB |
Output is correct |
84 |
Correct |
1 ms |
4444 KB |
Output is correct |
85 |
Correct |
266 ms |
41808 KB |
Output is correct |