#include <iostream>
#include <vector>
#include <set>
using namespace std;
using vi = vector<int>;
using pii = pair<int, int>;
using vvi = vector<vi>;
const int lg = 18;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
int l[1+n], r[1+n];
for(int i = 1; i <= n; i++) cin >> l[i] >> r[i];
vvi nxt(1+n+1, vi(1+lg, 1+n));
set<pii> S;
int y = 0;
for(int x = 1; x <= n; x++)
{
if(y < x)
{
y = x;
S.insert({l[x], r[x]});
}
while(1)
{
bool bad = 0;
pii z;
if(y+1 > n) bad = 1;
else
{
// cerr << "#\n";
z = {l[y+1], r[y+1]};
auto it = S.lower_bound(z);
if(it != S.end() && it->first <= r[y+1]) bad = 1;
// cerr << "bad1 = " << bad << '\n';
if(it != S.begin())
{
it--;
// cerr << it->first << " " << it->second << " : " << l[x]
if(it->second >= l[y+1]) bad = 1;
}
}
// cerr << x << ' ' << y+1 << ' ' << bad << '\n';
if(bad)
{
nxt[x][0] = y+1;
break;
}
else
{
y++;
S.insert(z);
}
}
S.erase({l[x], r[x]});
}
// for(int i = 1; i <= n; i++) cerr << nxt[i][0] << '\n';
for(int e = 1; e <= lg; e++)
for(int i = 1; i <= n; i++)
nxt[i][e] = nxt[ nxt[i][e-1] ][e-1];
int q;
cin >> q;
for(int j = 1; j <= q; j++)
{
int l, r;
cin >> l >> r;
r++;
int res = 0;
for(int e = lg; e >= 0; e--)
{
if(nxt[l][e] >= r) continue;
// cerr << l << " -> " << nxt[l][e] << ' ' << (1<<e) << '\n';
res += (1<<e);
l = nxt[l][e];
}
res += 1;
cout << res << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
27880 KB |
Output is correct |
2 |
Correct |
102 ms |
27488 KB |
Output is correct |
3 |
Correct |
95 ms |
27476 KB |
Output is correct |
4 |
Correct |
101 ms |
27880 KB |
Output is correct |
5 |
Correct |
102 ms |
29108 KB |
Output is correct |
6 |
Correct |
94 ms |
26524 KB |
Output is correct |
7 |
Correct |
93 ms |
26928 KB |
Output is correct |
8 |
Correct |
99 ms |
27080 KB |
Output is correct |
9 |
Correct |
107 ms |
27712 KB |
Output is correct |
10 |
Correct |
105 ms |
26804 KB |
Output is correct |
11 |
Correct |
230 ms |
37200 KB |
Output is correct |
12 |
Correct |
203 ms |
34816 KB |
Output is correct |
13 |
Correct |
210 ms |
34728 KB |
Output is correct |
14 |
Correct |
189 ms |
33164 KB |
Output is correct |
15 |
Correct |
191 ms |
31200 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
94 ms |
28296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1092 KB |
Output is correct |
2 |
Correct |
4 ms |
972 KB |
Output is correct |
3 |
Correct |
4 ms |
972 KB |
Output is correct |
4 |
Correct |
4 ms |
972 KB |
Output is correct |
5 |
Correct |
4 ms |
968 KB |
Output is correct |
6 |
Correct |
4 ms |
964 KB |
Output is correct |
7 |
Correct |
5 ms |
972 KB |
Output is correct |
8 |
Correct |
4 ms |
972 KB |
Output is correct |
9 |
Correct |
4 ms |
964 KB |
Output is correct |
10 |
Correct |
4 ms |
964 KB |
Output is correct |
11 |
Correct |
5 ms |
1216 KB |
Output is correct |
12 |
Correct |
5 ms |
1156 KB |
Output is correct |
13 |
Correct |
5 ms |
1228 KB |
Output is correct |
14 |
Correct |
5 ms |
1092 KB |
Output is correct |
15 |
Correct |
5 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
4 ms |
968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1092 KB |
Output is correct |
2 |
Correct |
4 ms |
972 KB |
Output is correct |
3 |
Correct |
4 ms |
972 KB |
Output is correct |
4 |
Correct |
4 ms |
972 KB |
Output is correct |
5 |
Correct |
4 ms |
968 KB |
Output is correct |
6 |
Correct |
4 ms |
964 KB |
Output is correct |
7 |
Correct |
5 ms |
972 KB |
Output is correct |
8 |
Correct |
4 ms |
972 KB |
Output is correct |
9 |
Correct |
4 ms |
964 KB |
Output is correct |
10 |
Correct |
4 ms |
964 KB |
Output is correct |
11 |
Correct |
5 ms |
1216 KB |
Output is correct |
12 |
Correct |
5 ms |
1156 KB |
Output is correct |
13 |
Correct |
5 ms |
1228 KB |
Output is correct |
14 |
Correct |
5 ms |
1092 KB |
Output is correct |
15 |
Correct |
5 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
4 ms |
968 KB |
Output is correct |
18 |
Correct |
79 ms |
3632 KB |
Output is correct |
19 |
Correct |
68 ms |
3436 KB |
Output is correct |
20 |
Correct |
104 ms |
3652 KB |
Output is correct |
21 |
Correct |
75 ms |
3392 KB |
Output is correct |
22 |
Correct |
75 ms |
3524 KB |
Output is correct |
23 |
Correct |
72 ms |
3492 KB |
Output is correct |
24 |
Correct |
76 ms |
3524 KB |
Output is correct |
25 |
Correct |
76 ms |
3776 KB |
Output is correct |
26 |
Correct |
70 ms |
3512 KB |
Output is correct |
27 |
Correct |
66 ms |
3308 KB |
Output is correct |
28 |
Correct |
56 ms |
3232 KB |
Output is correct |
29 |
Correct |
49 ms |
3356 KB |
Output is correct |
30 |
Correct |
48 ms |
3268 KB |
Output is correct |
31 |
Correct |
50 ms |
3144 KB |
Output is correct |
32 |
Correct |
53 ms |
3252 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
56 ms |
3380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
29124 KB |
Output is correct |
2 |
Correct |
100 ms |
28684 KB |
Output is correct |
3 |
Correct |
92 ms |
26560 KB |
Output is correct |
4 |
Correct |
92 ms |
26276 KB |
Output is correct |
5 |
Correct |
93 ms |
27072 KB |
Output is correct |
6 |
Correct |
89 ms |
26692 KB |
Output is correct |
7 |
Correct |
89 ms |
26396 KB |
Output is correct |
8 |
Correct |
96 ms |
26540 KB |
Output is correct |
9 |
Correct |
94 ms |
26384 KB |
Output is correct |
10 |
Correct |
117 ms |
28432 KB |
Output is correct |
11 |
Correct |
213 ms |
34588 KB |
Output is correct |
12 |
Correct |
229 ms |
37956 KB |
Output is correct |
13 |
Correct |
217 ms |
34596 KB |
Output is correct |
14 |
Correct |
200 ms |
31060 KB |
Output is correct |
15 |
Correct |
201 ms |
33568 KB |
Output is correct |
16 |
Correct |
1 ms |
288 KB |
Output is correct |
17 |
Correct |
87 ms |
27072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
27880 KB |
Output is correct |
2 |
Correct |
102 ms |
27488 KB |
Output is correct |
3 |
Correct |
95 ms |
27476 KB |
Output is correct |
4 |
Correct |
101 ms |
27880 KB |
Output is correct |
5 |
Correct |
102 ms |
29108 KB |
Output is correct |
6 |
Correct |
94 ms |
26524 KB |
Output is correct |
7 |
Correct |
93 ms |
26928 KB |
Output is correct |
8 |
Correct |
99 ms |
27080 KB |
Output is correct |
9 |
Correct |
107 ms |
27712 KB |
Output is correct |
10 |
Correct |
105 ms |
26804 KB |
Output is correct |
11 |
Correct |
230 ms |
37200 KB |
Output is correct |
12 |
Correct |
203 ms |
34816 KB |
Output is correct |
13 |
Correct |
210 ms |
34728 KB |
Output is correct |
14 |
Correct |
189 ms |
33164 KB |
Output is correct |
15 |
Correct |
191 ms |
31200 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
94 ms |
28296 KB |
Output is correct |
18 |
Correct |
5 ms |
1092 KB |
Output is correct |
19 |
Correct |
4 ms |
972 KB |
Output is correct |
20 |
Correct |
4 ms |
972 KB |
Output is correct |
21 |
Correct |
4 ms |
972 KB |
Output is correct |
22 |
Correct |
4 ms |
968 KB |
Output is correct |
23 |
Correct |
4 ms |
964 KB |
Output is correct |
24 |
Correct |
5 ms |
972 KB |
Output is correct |
25 |
Correct |
4 ms |
972 KB |
Output is correct |
26 |
Correct |
4 ms |
964 KB |
Output is correct |
27 |
Correct |
4 ms |
964 KB |
Output is correct |
28 |
Correct |
5 ms |
1216 KB |
Output is correct |
29 |
Correct |
5 ms |
1156 KB |
Output is correct |
30 |
Correct |
5 ms |
1228 KB |
Output is correct |
31 |
Correct |
5 ms |
1092 KB |
Output is correct |
32 |
Correct |
5 ms |
1100 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
4 ms |
968 KB |
Output is correct |
35 |
Correct |
79 ms |
3632 KB |
Output is correct |
36 |
Correct |
68 ms |
3436 KB |
Output is correct |
37 |
Correct |
104 ms |
3652 KB |
Output is correct |
38 |
Correct |
75 ms |
3392 KB |
Output is correct |
39 |
Correct |
75 ms |
3524 KB |
Output is correct |
40 |
Correct |
72 ms |
3492 KB |
Output is correct |
41 |
Correct |
76 ms |
3524 KB |
Output is correct |
42 |
Correct |
76 ms |
3776 KB |
Output is correct |
43 |
Correct |
70 ms |
3512 KB |
Output is correct |
44 |
Correct |
66 ms |
3308 KB |
Output is correct |
45 |
Correct |
56 ms |
3232 KB |
Output is correct |
46 |
Correct |
49 ms |
3356 KB |
Output is correct |
47 |
Correct |
48 ms |
3268 KB |
Output is correct |
48 |
Correct |
50 ms |
3144 KB |
Output is correct |
49 |
Correct |
53 ms |
3252 KB |
Output is correct |
50 |
Correct |
0 ms |
204 KB |
Output is correct |
51 |
Correct |
56 ms |
3380 KB |
Output is correct |
52 |
Correct |
102 ms |
29124 KB |
Output is correct |
53 |
Correct |
100 ms |
28684 KB |
Output is correct |
54 |
Correct |
92 ms |
26560 KB |
Output is correct |
55 |
Correct |
92 ms |
26276 KB |
Output is correct |
56 |
Correct |
93 ms |
27072 KB |
Output is correct |
57 |
Correct |
89 ms |
26692 KB |
Output is correct |
58 |
Correct |
89 ms |
26396 KB |
Output is correct |
59 |
Correct |
96 ms |
26540 KB |
Output is correct |
60 |
Correct |
94 ms |
26384 KB |
Output is correct |
61 |
Correct |
117 ms |
28432 KB |
Output is correct |
62 |
Correct |
213 ms |
34588 KB |
Output is correct |
63 |
Correct |
229 ms |
37956 KB |
Output is correct |
64 |
Correct |
217 ms |
34596 KB |
Output is correct |
65 |
Correct |
200 ms |
31060 KB |
Output is correct |
66 |
Correct |
201 ms |
33568 KB |
Output is correct |
67 |
Correct |
1 ms |
288 KB |
Output is correct |
68 |
Correct |
87 ms |
27072 KB |
Output is correct |
69 |
Correct |
374 ms |
30784 KB |
Output is correct |
70 |
Correct |
381 ms |
32232 KB |
Output is correct |
71 |
Correct |
353 ms |
29800 KB |
Output is correct |
72 |
Correct |
417 ms |
31264 KB |
Output is correct |
73 |
Correct |
401 ms |
30892 KB |
Output is correct |
74 |
Correct |
404 ms |
32584 KB |
Output is correct |
75 |
Correct |
371 ms |
30268 KB |
Output is correct |
76 |
Correct |
395 ms |
32644 KB |
Output is correct |
77 |
Correct |
336 ms |
30116 KB |
Output is correct |
78 |
Correct |
343 ms |
30824 KB |
Output is correct |
79 |
Correct |
319 ms |
40496 KB |
Output is correct |
80 |
Correct |
290 ms |
37936 KB |
Output is correct |
81 |
Correct |
282 ms |
37540 KB |
Output is correct |
82 |
Correct |
270 ms |
34488 KB |
Output is correct |
83 |
Correct |
253 ms |
33092 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Correct |
174 ms |
30252 KB |
Output is correct |