#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
void compress(vector<int> &a){
int n = a.size();
vector<pair<int,int>> b(n);
for(int i=0;i<n;i++){
b[i].first = a[i];
b[i].second = i;
}
sort(b.begin(),b.end());
a[b[0].second] = 1;
int cnt = 1;
for(int i=1;i<n;i++){
if(b[i].first == b[i-1].first){
a[b[i].second] = a[b[i-1].second];
}
else a[b[i].second] = ++cnt;
}
}
struct segTree{
int n;
vector<int> tree;
vector<int> lazy;
segTree(int _n){
n = _n;
tree.resize(4*n+5,-1);
lazy.resize(4*n+5,-1);
}
void prop(int node,int l,int r){
tree[node] = lazy[node];
if(l != r){
lazy[node<<1] = lazy[node];
lazy[node<<1|1] = lazy[node];
}
lazy[node] = -1;
}
void update(int node,int l,int r,int lQ,int rQ,int val){
if(l>r) return;
if(~lazy[node]) prop(node,l,r);
if(lQ>r || l>rQ) return;
if(lQ<=l&&r<=rQ) return lazy[node] = val, prop(node,l,r), void();
int md = l + r >> 1;
update(node<<1,l,md,lQ,rQ,val), update(node<<1|1,md+1,r,lQ,rQ,val);
tree[node] = max(tree[node<<1] , tree[node<<1|1]);
}
void update(int l,int r,int val){
update(1,1,n,l,r,val);
}
int query(int node,int l,int r,int lQ,int rQ){
if(l>r) return -1;
if(~lazy[node]) prop(node,l,r);
if(lQ>r || l>rQ) return -1;
if(lQ<=l&&r<=rQ) return tree[node];
int md = l + r >> 1;
return max(query(node<<1,l,md,lQ,rQ) , query(node<<1|1,md+1,r,lQ,rQ));
}
int query(int l,int r){
return query(1,1,n,l,r);
}
};
int sparse[200005][30], logs[200005];
void build(vector<int> &v){
int n = v.size();
for(int i=0;i<n;i++) sparse[i][0] = v[i];
for(int k=1;k<30;k++) for(int i=0;i+(1<<k)-1<n;i++)sparse[i][k] = max(sparse[i][k-1],sparse[i+(1<<k-1)][k-1]);
for(int i=2;i<=n;i++) logs[i] = logs[i>>1] + 1;
}
int query(int l,int r){
int sz = r - l + 1;
int lg = logs[sz];
return max(sparse[l][lg],sparse[r-(1<<lg)+1][lg]);
}
void solve(){
int n;
cin>>n;
vector<pair<int,int>> v(n);
for(auto &[l,r] : v) cin>>l>>r;
vector<int> tmp;
for(auto [l,r] : v) tmp.push_back(l);
for(auto [l,r] : v) tmp.push_back(r);
compress(tmp);
for(int i=0;i<n;i++) v[i] = {tmp[i],tmp[i+n]};
segTree sg(n*2);
vector<int> lst(n);
for(int i=0;i<n;i++){
int l = v[i].first , r = v[i].second;
lst[i] = sg.query(l,r);
sg.update(l,r,i);
}
build(lst);
vector<vector<int>> P(n,vector<int>(30,1e9));
for(int i=0;i<n;i++){
int l=i+1, r = n-1;
while(l<=r){
int md = l + r >> 1;
if(query(i,md)>=i) P[i][0] = md, r = md-1;
else l = md + 1;
}
}
for(int k=1;k<30;k++){
for(int i=0;i<n;i++){
if(P[i][k-1] == 1e9) P[i][k] = 1e9;
else P[i][k] = P[P[i][k-1]][k-1];
}
}
int q;
cin>>q;
while(q--){
int a,b;
cin>>a>>b;
a --, b --;
int node = a;
int ans = 1;
for(int k=29;k>=0;k--){
if(P[node][k] > b) continue;
node = P[node][k];
ans += (1<<k);
}
cout<<ans<<endl;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message
Main.cpp: In member function 'void segTree::update(int, int, int, int, int, int)':
Main.cpp:44:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | int md = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'int segTree::query(int, int, int, int, int)':
Main.cpp:56:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int md = l + r >> 1;
| ~~^~~
Main.cpp: In function 'void build(std::vector<int>&)':
Main.cpp:67:104: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
67 | for(int k=1;k<30;k++) for(int i=0;i+(1<<k)-1<n;i++)sparse[i][k] = max(sparse[i][k-1],sparse[i+(1<<k-1)][k-1]);
| ~^~
Main.cpp: In function 'void solve()':
Main.cpp:79:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
79 | for(auto &[l,r] : v) cin>>l>>r;
| ^
Main.cpp:81:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
81 | for(auto [l,r] : v) tmp.push_back(l);
| ^
Main.cpp:82:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
82 | for(auto [l,r] : v) tmp.push_back(r);
| ^
Main.cpp:97:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
97 | int md = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
321 ms |
67612 KB |
Output is correct |
2 |
Correct |
326 ms |
70412 KB |
Output is correct |
3 |
Correct |
318 ms |
70460 KB |
Output is correct |
4 |
Correct |
323 ms |
71528 KB |
Output is correct |
5 |
Correct |
337 ms |
74464 KB |
Output is correct |
6 |
Correct |
164 ms |
68532 KB |
Output is correct |
7 |
Correct |
187 ms |
69256 KB |
Output is correct |
8 |
Correct |
194 ms |
69736 KB |
Output is correct |
9 |
Correct |
207 ms |
71228 KB |
Output is correct |
10 |
Correct |
208 ms |
69272 KB |
Output is correct |
11 |
Correct |
232 ms |
72404 KB |
Output is correct |
12 |
Correct |
217 ms |
68496 KB |
Output is correct |
13 |
Correct |
211 ms |
68376 KB |
Output is correct |
14 |
Correct |
245 ms |
72680 KB |
Output is correct |
15 |
Correct |
272 ms |
70504 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
183 ms |
72368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
3676 KB |
Output is correct |
2 |
Correct |
7 ms |
3544 KB |
Output is correct |
3 |
Correct |
7 ms |
3676 KB |
Output is correct |
4 |
Correct |
7 ms |
3676 KB |
Output is correct |
5 |
Correct |
7 ms |
3672 KB |
Output is correct |
6 |
Correct |
5 ms |
3676 KB |
Output is correct |
7 |
Correct |
5 ms |
3556 KB |
Output is correct |
8 |
Correct |
6 ms |
3828 KB |
Output is correct |
9 |
Correct |
5 ms |
3548 KB |
Output is correct |
10 |
Correct |
5 ms |
3672 KB |
Output is correct |
11 |
Correct |
5 ms |
3676 KB |
Output is correct |
12 |
Correct |
5 ms |
3676 KB |
Output is correct |
13 |
Correct |
5 ms |
3656 KB |
Output is correct |
14 |
Correct |
5 ms |
3676 KB |
Output is correct |
15 |
Correct |
6 ms |
3712 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
4 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
3676 KB |
Output is correct |
2 |
Correct |
7 ms |
3544 KB |
Output is correct |
3 |
Correct |
7 ms |
3676 KB |
Output is correct |
4 |
Correct |
7 ms |
3676 KB |
Output is correct |
5 |
Correct |
7 ms |
3672 KB |
Output is correct |
6 |
Correct |
5 ms |
3676 KB |
Output is correct |
7 |
Correct |
5 ms |
3556 KB |
Output is correct |
8 |
Correct |
6 ms |
3828 KB |
Output is correct |
9 |
Correct |
5 ms |
3548 KB |
Output is correct |
10 |
Correct |
5 ms |
3672 KB |
Output is correct |
11 |
Correct |
5 ms |
3676 KB |
Output is correct |
12 |
Correct |
5 ms |
3676 KB |
Output is correct |
13 |
Correct |
5 ms |
3656 KB |
Output is correct |
14 |
Correct |
5 ms |
3676 KB |
Output is correct |
15 |
Correct |
6 ms |
3712 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
4 ms |
3676 KB |
Output is correct |
18 |
Correct |
67 ms |
6360 KB |
Output is correct |
19 |
Correct |
62 ms |
5996 KB |
Output is correct |
20 |
Correct |
67 ms |
6480 KB |
Output is correct |
21 |
Correct |
62 ms |
6188 KB |
Output is correct |
22 |
Correct |
64 ms |
6224 KB |
Output is correct |
23 |
Correct |
61 ms |
6228 KB |
Output is correct |
24 |
Correct |
67 ms |
6464 KB |
Output is correct |
25 |
Correct |
63 ms |
6228 KB |
Output is correct |
26 |
Correct |
59 ms |
6224 KB |
Output is correct |
27 |
Correct |
55 ms |
6092 KB |
Output is correct |
28 |
Correct |
39 ms |
5724 KB |
Output is correct |
29 |
Correct |
42 ms |
5716 KB |
Output is correct |
30 |
Correct |
41 ms |
5820 KB |
Output is correct |
31 |
Correct |
41 ms |
5712 KB |
Output is correct |
32 |
Correct |
41 ms |
5832 KB |
Output is correct |
33 |
Correct |
0 ms |
2396 KB |
Output is correct |
34 |
Correct |
44 ms |
5872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
351 ms |
70800 KB |
Output is correct |
2 |
Correct |
343 ms |
73168 KB |
Output is correct |
3 |
Correct |
322 ms |
68812 KB |
Output is correct |
4 |
Correct |
302 ms |
68192 KB |
Output is correct |
5 |
Correct |
328 ms |
69972 KB |
Output is correct |
6 |
Correct |
185 ms |
68764 KB |
Output is correct |
7 |
Correct |
202 ms |
68284 KB |
Output is correct |
8 |
Correct |
187 ms |
68768 KB |
Output is correct |
9 |
Correct |
197 ms |
68392 KB |
Output is correct |
10 |
Correct |
216 ms |
73392 KB |
Output is correct |
11 |
Correct |
210 ms |
68184 KB |
Output is correct |
12 |
Correct |
230 ms |
73876 KB |
Output is correct |
13 |
Correct |
220 ms |
68172 KB |
Output is correct |
14 |
Correct |
239 ms |
69376 KB |
Output is correct |
15 |
Correct |
239 ms |
73636 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
176 ms |
69492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
321 ms |
67612 KB |
Output is correct |
2 |
Correct |
326 ms |
70412 KB |
Output is correct |
3 |
Correct |
318 ms |
70460 KB |
Output is correct |
4 |
Correct |
323 ms |
71528 KB |
Output is correct |
5 |
Correct |
337 ms |
74464 KB |
Output is correct |
6 |
Correct |
164 ms |
68532 KB |
Output is correct |
7 |
Correct |
187 ms |
69256 KB |
Output is correct |
8 |
Correct |
194 ms |
69736 KB |
Output is correct |
9 |
Correct |
207 ms |
71228 KB |
Output is correct |
10 |
Correct |
208 ms |
69272 KB |
Output is correct |
11 |
Correct |
232 ms |
72404 KB |
Output is correct |
12 |
Correct |
217 ms |
68496 KB |
Output is correct |
13 |
Correct |
211 ms |
68376 KB |
Output is correct |
14 |
Correct |
245 ms |
72680 KB |
Output is correct |
15 |
Correct |
272 ms |
70504 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
183 ms |
72368 KB |
Output is correct |
18 |
Correct |
7 ms |
3676 KB |
Output is correct |
19 |
Correct |
7 ms |
3544 KB |
Output is correct |
20 |
Correct |
7 ms |
3676 KB |
Output is correct |
21 |
Correct |
7 ms |
3676 KB |
Output is correct |
22 |
Correct |
7 ms |
3672 KB |
Output is correct |
23 |
Correct |
5 ms |
3676 KB |
Output is correct |
24 |
Correct |
5 ms |
3556 KB |
Output is correct |
25 |
Correct |
6 ms |
3828 KB |
Output is correct |
26 |
Correct |
5 ms |
3548 KB |
Output is correct |
27 |
Correct |
5 ms |
3672 KB |
Output is correct |
28 |
Correct |
5 ms |
3676 KB |
Output is correct |
29 |
Correct |
5 ms |
3676 KB |
Output is correct |
30 |
Correct |
5 ms |
3656 KB |
Output is correct |
31 |
Correct |
5 ms |
3676 KB |
Output is correct |
32 |
Correct |
6 ms |
3712 KB |
Output is correct |
33 |
Correct |
1 ms |
2392 KB |
Output is correct |
34 |
Correct |
4 ms |
3676 KB |
Output is correct |
35 |
Correct |
67 ms |
6360 KB |
Output is correct |
36 |
Correct |
62 ms |
5996 KB |
Output is correct |
37 |
Correct |
67 ms |
6480 KB |
Output is correct |
38 |
Correct |
62 ms |
6188 KB |
Output is correct |
39 |
Correct |
64 ms |
6224 KB |
Output is correct |
40 |
Correct |
61 ms |
6228 KB |
Output is correct |
41 |
Correct |
67 ms |
6464 KB |
Output is correct |
42 |
Correct |
63 ms |
6228 KB |
Output is correct |
43 |
Correct |
59 ms |
6224 KB |
Output is correct |
44 |
Correct |
55 ms |
6092 KB |
Output is correct |
45 |
Correct |
39 ms |
5724 KB |
Output is correct |
46 |
Correct |
42 ms |
5716 KB |
Output is correct |
47 |
Correct |
41 ms |
5820 KB |
Output is correct |
48 |
Correct |
41 ms |
5712 KB |
Output is correct |
49 |
Correct |
41 ms |
5832 KB |
Output is correct |
50 |
Correct |
0 ms |
2396 KB |
Output is correct |
51 |
Correct |
44 ms |
5872 KB |
Output is correct |
52 |
Correct |
351 ms |
70800 KB |
Output is correct |
53 |
Correct |
343 ms |
73168 KB |
Output is correct |
54 |
Correct |
322 ms |
68812 KB |
Output is correct |
55 |
Correct |
302 ms |
68192 KB |
Output is correct |
56 |
Correct |
328 ms |
69972 KB |
Output is correct |
57 |
Correct |
185 ms |
68764 KB |
Output is correct |
58 |
Correct |
202 ms |
68284 KB |
Output is correct |
59 |
Correct |
187 ms |
68768 KB |
Output is correct |
60 |
Correct |
197 ms |
68392 KB |
Output is correct |
61 |
Correct |
216 ms |
73392 KB |
Output is correct |
62 |
Correct |
210 ms |
68184 KB |
Output is correct |
63 |
Correct |
230 ms |
73876 KB |
Output is correct |
64 |
Correct |
220 ms |
68172 KB |
Output is correct |
65 |
Correct |
239 ms |
69376 KB |
Output is correct |
66 |
Correct |
239 ms |
73636 KB |
Output is correct |
67 |
Correct |
1 ms |
2396 KB |
Output is correct |
68 |
Correct |
176 ms |
69492 KB |
Output is correct |
69 |
Correct |
566 ms |
73340 KB |
Output is correct |
70 |
Correct |
539 ms |
76744 KB |
Output is correct |
71 |
Correct |
488 ms |
71744 KB |
Output is correct |
72 |
Correct |
532 ms |
74792 KB |
Output is correct |
73 |
Correct |
506 ms |
73552 KB |
Output is correct |
74 |
Correct |
410 ms |
77644 KB |
Output is correct |
75 |
Correct |
386 ms |
72344 KB |
Output is correct |
76 |
Correct |
380 ms |
77976 KB |
Output is correct |
77 |
Correct |
364 ms |
72536 KB |
Output is correct |
78 |
Correct |
343 ms |
73084 KB |
Output is correct |
79 |
Correct |
309 ms |
76336 KB |
Output is correct |
80 |
Correct |
278 ms |
71308 KB |
Output is correct |
81 |
Correct |
271 ms |
70944 KB |
Output is correct |
82 |
Correct |
309 ms |
74236 KB |
Output is correct |
83 |
Correct |
273 ms |
71304 KB |
Output is correct |
84 |
Correct |
1 ms |
2396 KB |
Output is correct |
85 |
Correct |
238 ms |
72408 KB |
Output is correct |