#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 = itr;
if(itr != s.begin()) --itr2;
if(itr == s.end()) --itr;
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]];
}
st[j][n] = n;
}
}
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 |
169 ms |
20444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
1004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
1004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
201 ms |
21324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
169 ms |
20444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |