//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
bool intersect(int l1, int l2, int r1, int r2) {
if(l2 < r1 || r2 < l1)
return false;
return true;
}
#define ONLINE_JUDGE
void solve() {
int n;
cin >> n;
vector <array <int, 2>> vec(n +1);
for(int i = 1; i <= n; i++) {
cin >> vec[i][0] >> vec[i][1];
}
vector <vector <int>> bl(n +2);
for(int i = 1; i <= n +1; i++)
bl[i].resize(21);
int l = 1, r = 1;
while(l <= n) {
if(r < l) {
r++;
continue;
}
auto ok = [&](int x) -> bool {
bool res = true;
for(int i = l; i < x && res; i++) {
if(intersect(vec[i][0], vec[i][1], vec[x][0], vec[x][1]))
res = false;
}
return res;
};
while(r +1 <= n && ok(r +1)) {
r++;
}
//cerr << l << " " << r << "\n";
bl[l][0] = r +1;
l++;
}
bl[n +1][0] = n +1;
for(int i = 1; i < 21; i++) {
for(int j = 1; j <= n +1; j++) {
bl[j][i] = bl[bl[j][i -1]][i -1];
}
}
auto get = [&](int l, int r) -> int {
int res = 0;
for(int i = 20; i >= 0; i--) {
if(bl[l][i] <= r) {
l = bl[l][i];
res += (1 << i);
}
}
return res +1;
};
int q;
cin >> q;
while(q--) {
int l, r;
cin >> l >> r;
cout << get(l, r) << "\n";
}
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
27984 KB |
Output is correct |
2 |
Correct |
71 ms |
27476 KB |
Output is correct |
3 |
Correct |
75 ms |
27732 KB |
Output is correct |
4 |
Correct |
70 ms |
28036 KB |
Output is correct |
5 |
Correct |
79 ms |
29280 KB |
Output is correct |
6 |
Correct |
66 ms |
26448 KB |
Output is correct |
7 |
Correct |
69 ms |
26880 KB |
Output is correct |
8 |
Correct |
76 ms |
27216 KB |
Output is correct |
9 |
Correct |
74 ms |
27988 KB |
Output is correct |
10 |
Correct |
77 ms |
27004 KB |
Output is correct |
11 |
Execution timed out |
1043 ms |
27984 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1112 KB |
Output is correct |
2 |
Correct |
3 ms |
1112 KB |
Output is correct |
3 |
Correct |
3 ms |
1196 KB |
Output is correct |
4 |
Correct |
4 ms |
1112 KB |
Output is correct |
5 |
Correct |
3 ms |
1112 KB |
Output is correct |
6 |
Correct |
3 ms |
1112 KB |
Output is correct |
7 |
Correct |
4 ms |
1380 KB |
Output is correct |
8 |
Correct |
3 ms |
1112 KB |
Output is correct |
9 |
Correct |
4 ms |
1112 KB |
Output is correct |
10 |
Correct |
3 ms |
1116 KB |
Output is correct |
11 |
Correct |
17 ms |
1116 KB |
Output is correct |
12 |
Correct |
15 ms |
1112 KB |
Output is correct |
13 |
Correct |
16 ms |
1112 KB |
Output is correct |
14 |
Correct |
9 ms |
1368 KB |
Output is correct |
15 |
Correct |
9 ms |
1112 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
4 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1112 KB |
Output is correct |
2 |
Correct |
3 ms |
1112 KB |
Output is correct |
3 |
Correct |
3 ms |
1196 KB |
Output is correct |
4 |
Correct |
4 ms |
1112 KB |
Output is correct |
5 |
Correct |
3 ms |
1112 KB |
Output is correct |
6 |
Correct |
3 ms |
1112 KB |
Output is correct |
7 |
Correct |
4 ms |
1380 KB |
Output is correct |
8 |
Correct |
3 ms |
1112 KB |
Output is correct |
9 |
Correct |
4 ms |
1112 KB |
Output is correct |
10 |
Correct |
3 ms |
1116 KB |
Output is correct |
11 |
Correct |
17 ms |
1116 KB |
Output is correct |
12 |
Correct |
15 ms |
1112 KB |
Output is correct |
13 |
Correct |
16 ms |
1112 KB |
Output is correct |
14 |
Correct |
9 ms |
1368 KB |
Output is correct |
15 |
Correct |
9 ms |
1112 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
4 ms |
1112 KB |
Output is correct |
18 |
Correct |
61 ms |
3664 KB |
Output is correct |
19 |
Correct |
57 ms |
3408 KB |
Output is correct |
20 |
Correct |
68 ms |
3832 KB |
Output is correct |
21 |
Correct |
58 ms |
3472 KB |
Output is correct |
22 |
Correct |
60 ms |
3580 KB |
Output is correct |
23 |
Correct |
65 ms |
3412 KB |
Output is correct |
24 |
Correct |
60 ms |
3664 KB |
Output is correct |
25 |
Correct |
60 ms |
3592 KB |
Output is correct |
26 |
Correct |
58 ms |
3664 KB |
Output is correct |
27 |
Correct |
53 ms |
3420 KB |
Output is correct |
28 |
Correct |
51 ms |
3152 KB |
Output is correct |
29 |
Correct |
52 ms |
3152 KB |
Output is correct |
30 |
Correct |
52 ms |
3096 KB |
Output is correct |
31 |
Correct |
46 ms |
3152 KB |
Output is correct |
32 |
Correct |
50 ms |
3188 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
44 ms |
3408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
29264 KB |
Output is correct |
2 |
Correct |
73 ms |
28584 KB |
Output is correct |
3 |
Correct |
75 ms |
26796 KB |
Output is correct |
4 |
Correct |
71 ms |
26748 KB |
Output is correct |
5 |
Correct |
69 ms |
27216 KB |
Output is correct |
6 |
Correct |
65 ms |
26700 KB |
Output is correct |
7 |
Correct |
67 ms |
26448 KB |
Output is correct |
8 |
Correct |
69 ms |
26704 KB |
Output is correct |
9 |
Correct |
68 ms |
26452 KB |
Output is correct |
10 |
Correct |
75 ms |
28496 KB |
Output is correct |
11 |
Execution timed out |
1034 ms |
26192 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
27984 KB |
Output is correct |
2 |
Correct |
71 ms |
27476 KB |
Output is correct |
3 |
Correct |
75 ms |
27732 KB |
Output is correct |
4 |
Correct |
70 ms |
28036 KB |
Output is correct |
5 |
Correct |
79 ms |
29280 KB |
Output is correct |
6 |
Correct |
66 ms |
26448 KB |
Output is correct |
7 |
Correct |
69 ms |
26880 KB |
Output is correct |
8 |
Correct |
76 ms |
27216 KB |
Output is correct |
9 |
Correct |
74 ms |
27988 KB |
Output is correct |
10 |
Correct |
77 ms |
27004 KB |
Output is correct |
11 |
Execution timed out |
1043 ms |
27984 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |