#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define ll long long
#define oo 1e9 + 9
#define pii pair<int, int>
using namespace std;
const int MAX = 2e5 + 5;
int n, q;
pii arr[MAX];
int st[20][MAX];
bool checkRange(pii a, pii b){
return (a.first >= b.first && a.first <= b.second) || (a.second >= b.first && a.second <= b.second);
}
void calcMaxR(){
set<pii> s;
int l = 0;
for (int i = 0; i < n; i++)
{
while(!s.empty()){
auto itr = s.lower_bound({arr[i].first, 0});
auto itr2 = s.upper_bound({arr[i].second, 0});
if(itr == s.end()) --itr;
if(itr2 != s.begin()) --itr2;
if((checkRange(arr[i], (*itr))) || checkRange(arr[i], (*itr2))){
st[0][l] = i;
s.erase(arr[l]);
++l;
}
else{
break;
}
}
s.insert(arr[i]);
}
for (int j = l; j <= n; j++)
{
st[0][j] = n;
}
}
void build(){
for (int j = 1; j < 20; j++)
{
for (int i = 0; i <= n; i++)
{
st[j][i] = st[j - 1][st[j - 1][i]];
}
}
}
int ask(int l, int r){
int ans = 0;
for(int j = 19; j >= 0; --j){
if(st[j][l] <= r){
l = st[j][l];
ans += (1 << j);
}
}
ans += 1;
return ans;
}
int main(){
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i].first >> arr[i].second;
}
calcMaxR();
build();
cin >> q;
while(q--){
int l, r; cin >> l >> r;
--l; --r;
cout << ask(l, r) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
161 ms |
16716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
169 ms |
17396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
161 ms |
16716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |