//LCA
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int P[n+1][19];
for(int i = 0;i<=n;i++)P[i][0]= n;
set<pair<pair<int,int>,int>> s;
set<int> ind;
for(int i = 0;i<n;i++)ind.insert(i);
for(int i = 0;i<n;i++){
int a,b;cin>>a>>b;
int ma = -1;
pair<pair<int,int>,int> p = {{a,0},0};
auto it = s.lower_bound(p);
if(it!=s.begin()){
it--;
if((*it).first.second>=a){
ma = max(ma,(*it).second);
s.erase(it);
}
}
it = s.lower_bound(p);
if(it!=s.end()&&(*it).first.first<=b){
ma = max(ma,(*it).second);
s.erase(it);
it = s.lower_bound(p);
}
p.second = i;
p.first.second = b;
s.insert(p);
while(!ind.empty()&&(*ind.begin())<=ma){
P[(*ind.begin())][0] = i;
ind.erase(ind.begin());
}
}
for(int i = n;i>=0;i--){
for(int j= 1;j<19;j++){
P[i][j] = P[P[i][j-1]][j-1];
}
}
int q;cin>>q;
while(q--){
int x,y;cin>>x>>y;
x--;y--;
long long sum = 0;
for(int j = 18;j>=0;j--){
if(P[x][j]<=y){
x = P[x][j];
sum+=(1<<j);
}
}
cout<<sum+1<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
228 ms |
24396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
1036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
1036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
239 ms |
25172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
228 ms |
24396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |