#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 2e5 + 5, LOG=18;
const int oo = 1e18, mod = 1e9 + 7;
int n, m, k, q, lef[N], rig[N], par[N][LOG];
set<ii> st;
vector<int> arr;
bool can_insert(int ind) {
if (st.empty()) return 1;
auto it=st.lower_bound({lef[ind], 0});
int nxt=it->second;
if (lef[ind]<=lef[nxt] && lef[nxt]<=rig[ind]) return 0;
if (it!=st.begin()) {
it--;
int prv=it->second;
if (lef[prv]<=lef[ind] && lef[ind]<=rig[prv]) return 0;
}
return 1;
}
void precalc() {
int lim=0;
foru(i,1,n) {
if (lim<i) {
st.clear();
lim=i;
st.insert({lef[i], i});
}
while (lim+1<=n && can_insert(lim+1)) {
lim++;
st.insert({lef[lim], lim});
}
par[i][0]=lim+1;
st.erase({lef[i], i});
}
par[n+1][0]=n+1;
foru(i,1,LOG-1) {
foru(j,1,n+1) {
par[j][i]=par[par[j][i-1]][i-1];
}
}
}
int query(int a, int b) {
int res=0;
ford(i, LOG-1, 0) {
if (par[a][i]<=b) {
a=par[a][i];
res+=1<<i;
}
}
return res+1;
}
void process() {
cin >> n;
foru(i,1,n) {
cin >> lef[i] >> rig[i];
}
precalc();
cin >> q;
foru(i,1,q) {
int a, b;
cin >> a >> b;
cout << query(a,b) << '\n';
}
return;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
/*
Xét các trường hợp đặc biệt
Kiểm tra lại input/output
Cố gắng trâu
Lật ngược bài toán
Calm down and get VOI
Flow:
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
35356 KB |
Output is correct |
2 |
Correct |
67 ms |
35156 KB |
Output is correct |
3 |
Correct |
70 ms |
35440 KB |
Output is correct |
4 |
Correct |
65 ms |
35152 KB |
Output is correct |
5 |
Correct |
67 ms |
35388 KB |
Output is correct |
6 |
Correct |
57 ms |
34896 KB |
Output is correct |
7 |
Correct |
61 ms |
35152 KB |
Output is correct |
8 |
Correct |
69 ms |
35324 KB |
Output is correct |
9 |
Correct |
69 ms |
35152 KB |
Output is correct |
10 |
Correct |
74 ms |
34868 KB |
Output is correct |
11 |
Correct |
178 ms |
47184 KB |
Output is correct |
12 |
Correct |
172 ms |
45936 KB |
Output is correct |
13 |
Correct |
171 ms |
45904 KB |
Output is correct |
14 |
Correct |
165 ms |
41904 KB |
Output is correct |
15 |
Correct |
150 ms |
40088 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
64 ms |
35264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
3 ms |
6748 KB |
Output is correct |
3 |
Correct |
3 ms |
6744 KB |
Output is correct |
4 |
Correct |
3 ms |
6748 KB |
Output is correct |
5 |
Correct |
3 ms |
6832 KB |
Output is correct |
6 |
Correct |
3 ms |
6948 KB |
Output is correct |
7 |
Correct |
4 ms |
6748 KB |
Output is correct |
8 |
Correct |
3 ms |
6748 KB |
Output is correct |
9 |
Correct |
3 ms |
6748 KB |
Output is correct |
10 |
Correct |
3 ms |
6748 KB |
Output is correct |
11 |
Correct |
4 ms |
7004 KB |
Output is correct |
12 |
Correct |
4 ms |
7004 KB |
Output is correct |
13 |
Correct |
4 ms |
7196 KB |
Output is correct |
14 |
Correct |
4 ms |
6980 KB |
Output is correct |
15 |
Correct |
4 ms |
7004 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
3 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
3 ms |
6748 KB |
Output is correct |
3 |
Correct |
3 ms |
6744 KB |
Output is correct |
4 |
Correct |
3 ms |
6748 KB |
Output is correct |
5 |
Correct |
3 ms |
6832 KB |
Output is correct |
6 |
Correct |
3 ms |
6948 KB |
Output is correct |
7 |
Correct |
4 ms |
6748 KB |
Output is correct |
8 |
Correct |
3 ms |
6748 KB |
Output is correct |
9 |
Correct |
3 ms |
6748 KB |
Output is correct |
10 |
Correct |
3 ms |
6748 KB |
Output is correct |
11 |
Correct |
4 ms |
7004 KB |
Output is correct |
12 |
Correct |
4 ms |
7004 KB |
Output is correct |
13 |
Correct |
4 ms |
7196 KB |
Output is correct |
14 |
Correct |
4 ms |
6980 KB |
Output is correct |
15 |
Correct |
4 ms |
7004 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
3 ms |
6748 KB |
Output is correct |
18 |
Correct |
54 ms |
9556 KB |
Output is correct |
19 |
Correct |
49 ms |
9268 KB |
Output is correct |
20 |
Correct |
61 ms |
9448 KB |
Output is correct |
21 |
Correct |
50 ms |
9552 KB |
Output is correct |
22 |
Correct |
59 ms |
9420 KB |
Output is correct |
23 |
Correct |
51 ms |
9320 KB |
Output is correct |
24 |
Correct |
53 ms |
9300 KB |
Output is correct |
25 |
Correct |
52 ms |
9556 KB |
Output is correct |
26 |
Correct |
50 ms |
9300 KB |
Output is correct |
27 |
Correct |
46 ms |
9176 KB |
Output is correct |
28 |
Correct |
40 ms |
9044 KB |
Output is correct |
29 |
Correct |
38 ms |
9300 KB |
Output is correct |
30 |
Correct |
38 ms |
9300 KB |
Output is correct |
31 |
Correct |
45 ms |
9188 KB |
Output is correct |
32 |
Correct |
38 ms |
9040 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
40 ms |
9200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
35604 KB |
Output is correct |
2 |
Correct |
68 ms |
35412 KB |
Output is correct |
3 |
Correct |
63 ms |
34896 KB |
Output is correct |
4 |
Correct |
60 ms |
35040 KB |
Output is correct |
5 |
Correct |
62 ms |
35156 KB |
Output is correct |
6 |
Correct |
57 ms |
35004 KB |
Output is correct |
7 |
Correct |
63 ms |
35132 KB |
Output is correct |
8 |
Correct |
62 ms |
34896 KB |
Output is correct |
9 |
Correct |
62 ms |
34896 KB |
Output is correct |
10 |
Correct |
72 ms |
35396 KB |
Output is correct |
11 |
Correct |
177 ms |
45908 KB |
Output is correct |
12 |
Correct |
194 ms |
47408 KB |
Output is correct |
13 |
Correct |
170 ms |
45904 KB |
Output is correct |
14 |
Correct |
160 ms |
40416 KB |
Output is correct |
15 |
Correct |
163 ms |
41864 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
59 ms |
35412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
35356 KB |
Output is correct |
2 |
Correct |
67 ms |
35156 KB |
Output is correct |
3 |
Correct |
70 ms |
35440 KB |
Output is correct |
4 |
Correct |
65 ms |
35152 KB |
Output is correct |
5 |
Correct |
67 ms |
35388 KB |
Output is correct |
6 |
Correct |
57 ms |
34896 KB |
Output is correct |
7 |
Correct |
61 ms |
35152 KB |
Output is correct |
8 |
Correct |
69 ms |
35324 KB |
Output is correct |
9 |
Correct |
69 ms |
35152 KB |
Output is correct |
10 |
Correct |
74 ms |
34868 KB |
Output is correct |
11 |
Correct |
178 ms |
47184 KB |
Output is correct |
12 |
Correct |
172 ms |
45936 KB |
Output is correct |
13 |
Correct |
171 ms |
45904 KB |
Output is correct |
14 |
Correct |
165 ms |
41904 KB |
Output is correct |
15 |
Correct |
150 ms |
40088 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
64 ms |
35264 KB |
Output is correct |
18 |
Correct |
3 ms |
6748 KB |
Output is correct |
19 |
Correct |
3 ms |
6748 KB |
Output is correct |
20 |
Correct |
3 ms |
6744 KB |
Output is correct |
21 |
Correct |
3 ms |
6748 KB |
Output is correct |
22 |
Correct |
3 ms |
6832 KB |
Output is correct |
23 |
Correct |
3 ms |
6948 KB |
Output is correct |
24 |
Correct |
4 ms |
6748 KB |
Output is correct |
25 |
Correct |
3 ms |
6748 KB |
Output is correct |
26 |
Correct |
3 ms |
6748 KB |
Output is correct |
27 |
Correct |
3 ms |
6748 KB |
Output is correct |
28 |
Correct |
4 ms |
7004 KB |
Output is correct |
29 |
Correct |
4 ms |
7004 KB |
Output is correct |
30 |
Correct |
4 ms |
7196 KB |
Output is correct |
31 |
Correct |
4 ms |
6980 KB |
Output is correct |
32 |
Correct |
4 ms |
7004 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
3 ms |
6748 KB |
Output is correct |
35 |
Correct |
54 ms |
9556 KB |
Output is correct |
36 |
Correct |
49 ms |
9268 KB |
Output is correct |
37 |
Correct |
61 ms |
9448 KB |
Output is correct |
38 |
Correct |
50 ms |
9552 KB |
Output is correct |
39 |
Correct |
59 ms |
9420 KB |
Output is correct |
40 |
Correct |
51 ms |
9320 KB |
Output is correct |
41 |
Correct |
53 ms |
9300 KB |
Output is correct |
42 |
Correct |
52 ms |
9556 KB |
Output is correct |
43 |
Correct |
50 ms |
9300 KB |
Output is correct |
44 |
Correct |
46 ms |
9176 KB |
Output is correct |
45 |
Correct |
40 ms |
9044 KB |
Output is correct |
46 |
Correct |
38 ms |
9300 KB |
Output is correct |
47 |
Correct |
38 ms |
9300 KB |
Output is correct |
48 |
Correct |
45 ms |
9188 KB |
Output is correct |
49 |
Correct |
38 ms |
9040 KB |
Output is correct |
50 |
Correct |
1 ms |
4444 KB |
Output is correct |
51 |
Correct |
40 ms |
9200 KB |
Output is correct |
52 |
Correct |
75 ms |
35604 KB |
Output is correct |
53 |
Correct |
68 ms |
35412 KB |
Output is correct |
54 |
Correct |
63 ms |
34896 KB |
Output is correct |
55 |
Correct |
60 ms |
35040 KB |
Output is correct |
56 |
Correct |
62 ms |
35156 KB |
Output is correct |
57 |
Correct |
57 ms |
35004 KB |
Output is correct |
58 |
Correct |
63 ms |
35132 KB |
Output is correct |
59 |
Correct |
62 ms |
34896 KB |
Output is correct |
60 |
Correct |
62 ms |
34896 KB |
Output is correct |
61 |
Correct |
72 ms |
35396 KB |
Output is correct |
62 |
Correct |
177 ms |
45908 KB |
Output is correct |
63 |
Correct |
194 ms |
47408 KB |
Output is correct |
64 |
Correct |
170 ms |
45904 KB |
Output is correct |
65 |
Correct |
160 ms |
40416 KB |
Output is correct |
66 |
Correct |
163 ms |
41864 KB |
Output is correct |
67 |
Correct |
1 ms |
4440 KB |
Output is correct |
68 |
Correct |
59 ms |
35412 KB |
Output is correct |
69 |
Correct |
209 ms |
38720 KB |
Output is correct |
70 |
Correct |
210 ms |
38968 KB |
Output is correct |
71 |
Correct |
192 ms |
38360 KB |
Output is correct |
72 |
Correct |
215 ms |
38996 KB |
Output is correct |
73 |
Correct |
201 ms |
38656 KB |
Output is correct |
74 |
Correct |
242 ms |
39428 KB |
Output is correct |
75 |
Correct |
216 ms |
38736 KB |
Output is correct |
76 |
Correct |
212 ms |
38996 KB |
Output is correct |
77 |
Correct |
174 ms |
38488 KB |
Output is correct |
78 |
Correct |
173 ms |
38740 KB |
Output is correct |
79 |
Correct |
239 ms |
50256 KB |
Output is correct |
80 |
Correct |
224 ms |
48692 KB |
Output is correct |
81 |
Correct |
215 ms |
48684 KB |
Output is correct |
82 |
Correct |
210 ms |
43312 KB |
Output is correct |
83 |
Correct |
192 ms |
42576 KB |
Output is correct |
84 |
Correct |
1 ms |
4444 KB |
Output is correct |
85 |
Correct |
116 ms |
38592 KB |
Output is correct |