#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define F first
#define S second
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int, int>
#define vpi vector<pi>
#define vb vector<bool>
#define vvb vector<vb>
#define pb push_back
#define ppb pop_back
#define read(a) for(auto &x:a) cin >> x;
#define print(a) for(auto x:a) cout << x << " "; cout << "\n";
#define vc vector<char>
#define vvc vector<vc>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
#define ld long double
const int INF = 1e18;
const int inf = 1e9;
const int N = 2e5+10, LOG = 20;
int l[N], r[N];
int st[N][LOG+1];
set<pi> s;
bool fit(int ix){
if(s.empty()) return 1;
auto it = s.lower_bound({l[ix], 0});
pi c = (it == s.end() ? make_pair(INF, INF) : *it), p = (it == s.begin() ? make_pair(-INF, -INF) : *prev(it));
if(r[ix] >= c.F || p.S >= l[ix]) return 0;
return 1;
}
int query(int l, int r){
int ans = 0;
for(int j=LOG; j>=0; j--){
if(st[l][j] <= r){
l = st[l][j];
ans += (1LL<<j);
}
}
return ans+1;
}
void solve(){
int n; cin >> n;
for(int i=0; i<n; i++)
cin >> l[i] >> r[i];
int ix = 0;
for(int i=0; i<n; i++){
s.insert({l[i], r[i]});
ix = max(ix, i+1);
while(ix < n && fit(ix)){
s.insert({l[ix], r[ix]});
ix++;
}
st[i][0] = ix;
s.erase({l[i], r[i]});
}
for(int j=0; j<=LOG; j++)
st[n][j] = n;
for(int j=1; j<=LOG; j++)
for(int i=0; i<n; i++)
st[i][j] = st[st[i][j-1]][j-1];
int q; cin >> q;
while(q--){
int l, r; cin >> l >> r;
cout << query(l-1, r-1) << "\n";
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// #endif
int tt = 1;
// cin >> tt;
while(tt--)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
35712 KB |
Output is correct |
2 |
Correct |
97 ms |
35228 KB |
Output is correct |
3 |
Correct |
95 ms |
35320 KB |
Output is correct |
4 |
Correct |
97 ms |
35852 KB |
Output is correct |
5 |
Correct |
102 ms |
37180 KB |
Output is correct |
6 |
Correct |
95 ms |
33952 KB |
Output is correct |
7 |
Correct |
91 ms |
34520 KB |
Output is correct |
8 |
Correct |
96 ms |
34876 KB |
Output is correct |
9 |
Correct |
101 ms |
35640 KB |
Output is correct |
10 |
Correct |
109 ms |
34532 KB |
Output is correct |
11 |
Correct |
298 ms |
48768 KB |
Output is correct |
12 |
Correct |
248 ms |
45820 KB |
Output is correct |
13 |
Correct |
273 ms |
45720 KB |
Output is correct |
14 |
Correct |
253 ms |
43500 KB |
Output is correct |
15 |
Correct |
215 ms |
40792 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
91 ms |
36164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1364 KB |
Output is correct |
2 |
Correct |
4 ms |
1236 KB |
Output is correct |
3 |
Correct |
4 ms |
1232 KB |
Output is correct |
4 |
Correct |
5 ms |
1364 KB |
Output is correct |
5 |
Correct |
4 ms |
1236 KB |
Output is correct |
6 |
Correct |
4 ms |
1364 KB |
Output is correct |
7 |
Correct |
4 ms |
1236 KB |
Output is correct |
8 |
Correct |
4 ms |
1364 KB |
Output is correct |
9 |
Correct |
4 ms |
1236 KB |
Output is correct |
10 |
Correct |
4 ms |
1280 KB |
Output is correct |
11 |
Correct |
5 ms |
1616 KB |
Output is correct |
12 |
Correct |
5 ms |
1488 KB |
Output is correct |
13 |
Correct |
5 ms |
1492 KB |
Output is correct |
14 |
Correct |
5 ms |
1492 KB |
Output is correct |
15 |
Correct |
5 ms |
1368 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
4 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1364 KB |
Output is correct |
2 |
Correct |
4 ms |
1236 KB |
Output is correct |
3 |
Correct |
4 ms |
1232 KB |
Output is correct |
4 |
Correct |
5 ms |
1364 KB |
Output is correct |
5 |
Correct |
4 ms |
1236 KB |
Output is correct |
6 |
Correct |
4 ms |
1364 KB |
Output is correct |
7 |
Correct |
4 ms |
1236 KB |
Output is correct |
8 |
Correct |
4 ms |
1364 KB |
Output is correct |
9 |
Correct |
4 ms |
1236 KB |
Output is correct |
10 |
Correct |
4 ms |
1280 KB |
Output is correct |
11 |
Correct |
5 ms |
1616 KB |
Output is correct |
12 |
Correct |
5 ms |
1488 KB |
Output is correct |
13 |
Correct |
5 ms |
1492 KB |
Output is correct |
14 |
Correct |
5 ms |
1492 KB |
Output is correct |
15 |
Correct |
5 ms |
1368 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
4 ms |
1364 KB |
Output is correct |
18 |
Correct |
84 ms |
3992 KB |
Output is correct |
19 |
Correct |
59 ms |
3732 KB |
Output is correct |
20 |
Correct |
68 ms |
4044 KB |
Output is correct |
21 |
Correct |
61 ms |
3780 KB |
Output is correct |
22 |
Correct |
65 ms |
3784 KB |
Output is correct |
23 |
Correct |
61 ms |
3724 KB |
Output is correct |
24 |
Correct |
64 ms |
4016 KB |
Output is correct |
25 |
Correct |
67 ms |
3916 KB |
Output is correct |
26 |
Correct |
73 ms |
3896 KB |
Output is correct |
27 |
Correct |
59 ms |
3636 KB |
Output is correct |
28 |
Correct |
46 ms |
3536 KB |
Output is correct |
29 |
Correct |
50 ms |
3784 KB |
Output is correct |
30 |
Correct |
52 ms |
3636 KB |
Output is correct |
31 |
Correct |
51 ms |
3404 KB |
Output is correct |
32 |
Correct |
50 ms |
3508 KB |
Output is correct |
33 |
Correct |
0 ms |
328 KB |
Output is correct |
34 |
Correct |
46 ms |
3564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
37188 KB |
Output is correct |
2 |
Correct |
103 ms |
39340 KB |
Output is correct |
3 |
Correct |
93 ms |
36684 KB |
Output is correct |
4 |
Correct |
111 ms |
36224 KB |
Output is correct |
5 |
Correct |
95 ms |
37420 KB |
Output is correct |
6 |
Correct |
87 ms |
36768 KB |
Output is correct |
7 |
Correct |
90 ms |
36432 KB |
Output is correct |
8 |
Correct |
115 ms |
36776 KB |
Output is correct |
9 |
Correct |
102 ms |
36372 KB |
Output is correct |
10 |
Correct |
110 ms |
39276 KB |
Output is correct |
11 |
Correct |
257 ms |
47448 KB |
Output is correct |
12 |
Correct |
321 ms |
52072 KB |
Output is correct |
13 |
Correct |
251 ms |
47444 KB |
Output is correct |
14 |
Correct |
242 ms |
42840 KB |
Output is correct |
15 |
Correct |
266 ms |
46224 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
89 ms |
37196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
35712 KB |
Output is correct |
2 |
Correct |
97 ms |
35228 KB |
Output is correct |
3 |
Correct |
95 ms |
35320 KB |
Output is correct |
4 |
Correct |
97 ms |
35852 KB |
Output is correct |
5 |
Correct |
102 ms |
37180 KB |
Output is correct |
6 |
Correct |
95 ms |
33952 KB |
Output is correct |
7 |
Correct |
91 ms |
34520 KB |
Output is correct |
8 |
Correct |
96 ms |
34876 KB |
Output is correct |
9 |
Correct |
101 ms |
35640 KB |
Output is correct |
10 |
Correct |
109 ms |
34532 KB |
Output is correct |
11 |
Correct |
298 ms |
48768 KB |
Output is correct |
12 |
Correct |
248 ms |
45820 KB |
Output is correct |
13 |
Correct |
273 ms |
45720 KB |
Output is correct |
14 |
Correct |
253 ms |
43500 KB |
Output is correct |
15 |
Correct |
215 ms |
40792 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
91 ms |
36164 KB |
Output is correct |
18 |
Correct |
6 ms |
1364 KB |
Output is correct |
19 |
Correct |
4 ms |
1236 KB |
Output is correct |
20 |
Correct |
4 ms |
1232 KB |
Output is correct |
21 |
Correct |
5 ms |
1364 KB |
Output is correct |
22 |
Correct |
4 ms |
1236 KB |
Output is correct |
23 |
Correct |
4 ms |
1364 KB |
Output is correct |
24 |
Correct |
4 ms |
1236 KB |
Output is correct |
25 |
Correct |
4 ms |
1364 KB |
Output is correct |
26 |
Correct |
4 ms |
1236 KB |
Output is correct |
27 |
Correct |
4 ms |
1280 KB |
Output is correct |
28 |
Correct |
5 ms |
1616 KB |
Output is correct |
29 |
Correct |
5 ms |
1488 KB |
Output is correct |
30 |
Correct |
5 ms |
1492 KB |
Output is correct |
31 |
Correct |
5 ms |
1492 KB |
Output is correct |
32 |
Correct |
5 ms |
1368 KB |
Output is correct |
33 |
Correct |
1 ms |
324 KB |
Output is correct |
34 |
Correct |
4 ms |
1364 KB |
Output is correct |
35 |
Correct |
84 ms |
3992 KB |
Output is correct |
36 |
Correct |
59 ms |
3732 KB |
Output is correct |
37 |
Correct |
68 ms |
4044 KB |
Output is correct |
38 |
Correct |
61 ms |
3780 KB |
Output is correct |
39 |
Correct |
65 ms |
3784 KB |
Output is correct |
40 |
Correct |
61 ms |
3724 KB |
Output is correct |
41 |
Correct |
64 ms |
4016 KB |
Output is correct |
42 |
Correct |
67 ms |
3916 KB |
Output is correct |
43 |
Correct |
73 ms |
3896 KB |
Output is correct |
44 |
Correct |
59 ms |
3636 KB |
Output is correct |
45 |
Correct |
46 ms |
3536 KB |
Output is correct |
46 |
Correct |
50 ms |
3784 KB |
Output is correct |
47 |
Correct |
52 ms |
3636 KB |
Output is correct |
48 |
Correct |
51 ms |
3404 KB |
Output is correct |
49 |
Correct |
50 ms |
3508 KB |
Output is correct |
50 |
Correct |
0 ms |
328 KB |
Output is correct |
51 |
Correct |
46 ms |
3564 KB |
Output is correct |
52 |
Correct |
102 ms |
37188 KB |
Output is correct |
53 |
Correct |
103 ms |
39340 KB |
Output is correct |
54 |
Correct |
93 ms |
36684 KB |
Output is correct |
55 |
Correct |
111 ms |
36224 KB |
Output is correct |
56 |
Correct |
95 ms |
37420 KB |
Output is correct |
57 |
Correct |
87 ms |
36768 KB |
Output is correct |
58 |
Correct |
90 ms |
36432 KB |
Output is correct |
59 |
Correct |
115 ms |
36776 KB |
Output is correct |
60 |
Correct |
102 ms |
36372 KB |
Output is correct |
61 |
Correct |
110 ms |
39276 KB |
Output is correct |
62 |
Correct |
257 ms |
47448 KB |
Output is correct |
63 |
Correct |
321 ms |
52072 KB |
Output is correct |
64 |
Correct |
251 ms |
47444 KB |
Output is correct |
65 |
Correct |
242 ms |
42840 KB |
Output is correct |
66 |
Correct |
266 ms |
46224 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
89 ms |
37196 KB |
Output is correct |
69 |
Correct |
312 ms |
40816 KB |
Output is correct |
70 |
Correct |
314 ms |
42980 KB |
Output is correct |
71 |
Correct |
292 ms |
39800 KB |
Output is correct |
72 |
Correct |
297 ms |
41808 KB |
Output is correct |
73 |
Correct |
304 ms |
41176 KB |
Output is correct |
74 |
Correct |
328 ms |
43264 KB |
Output is correct |
75 |
Correct |
298 ms |
40288 KB |
Output is correct |
76 |
Correct |
288 ms |
43508 KB |
Output is correct |
77 |
Correct |
288 ms |
40224 KB |
Output is correct |
78 |
Correct |
297 ms |
40760 KB |
Output is correct |
79 |
Correct |
355 ms |
54580 KB |
Output is correct |
80 |
Correct |
325 ms |
50636 KB |
Output is correct |
81 |
Correct |
310 ms |
50272 KB |
Output is correct |
82 |
Correct |
285 ms |
46568 KB |
Output is correct |
83 |
Correct |
302 ms |
44620 KB |
Output is correct |
84 |
Correct |
2 ms |
328 KB |
Output is correct |
85 |
Correct |
173 ms |
40236 KB |
Output is correct |