#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mxN=2e5;
int n, q, l[mxN], r[mxN], anc[mxN][18];
int main() {
cin >> n;
for (int i=0; i<n; ++i) cin >> l[i] >> r[i];
set<array<int, 2>> s;
for (int i=n-1, j=n-1; ~i; --i) {
while(true) {
if (s.empty()||r[i]<(*s.begin())[0]||l[i]>(*s.rbegin())[1]) {
s.insert({l[i], r[i]});
break;
}
auto it=s.lower_bound({l[i], -1});
if ((it==s.end()||r[i]<(*it)[0])&&(it==s.begin()||(*prev(it))[1]<l[i])) {
s.insert({l[i], r[i]});
break;
}
s.erase({l[j], r[j]});
--j;
}
anc[i][0]=j+1;
}
for (int j=1; j<18; ++j) for (int i=0; i<n; ++i) anc[i][j]=anc[i][j-1]==n?n:anc[anc[i][j-1]][j-1];
cin >> q;
while(q--) {
int a, b;
cin >> a >> b, --a, --b;
int ans = 1;
for (int i = 17; i > 0; --i)
if (anc[a][i] <= b) {
ans += 1 << i;
a = anc[a][i];
}
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
18892 KB |
Output is correct |
2 |
Correct |
186 ms |
18676 KB |
Output is correct |
3 |
Correct |
191 ms |
18724 KB |
Output is correct |
4 |
Correct |
180 ms |
18896 KB |
Output is correct |
5 |
Incorrect |
194 ms |
19756 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
192 ms |
19708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
18892 KB |
Output is correct |
2 |
Correct |
186 ms |
18676 KB |
Output is correct |
3 |
Correct |
191 ms |
18724 KB |
Output is correct |
4 |
Correct |
180 ms |
18896 KB |
Output is correct |
5 |
Incorrect |
194 ms |
19756 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |