#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int,int> a,pair<int,int> b){
return a.second<b.second;
}
long long seg[800001];
void build(int p,int l,int r){
if(l==r){
seg[p] = 1e18;
return ;
}
int md = (l+r)/2;
build(p*2,l,md);build(p*2+1,md+1,r);
seg[p] = min(seg[p*2],seg[p*2+1]);
}
long long query(int p,int l,int r,int lq,int rq){
if(l>=lq&&r<=rq)return seg[p];
if(r<lq||l>rq)return 1e18;
int md = (l+r)/2;
return min(query(p*2,l,md,lq,rq),query(p*2+1,md+1,r,lq,rq));
}
void update(int p,int l,int r,int idx,int val){
if(l==r){
seg[p] = val;
return ;
}
int md = (l+r)/2;
if(idx<=md)update(p*2,l,md,idx,val);
else update(p*2+1,md+1,r,idx,val);
seg[p] = min(seg[p*2],seg[p*2+1]);
}
int main(){
int n,q;cin>>n>>q;
vector<pair<int,int>> v;
map<int,int> comp,sav;
for(int i = 0;i<n;i++){
int a,b;
cin>>a>>b;
comp[a]++;comp[b]++;
v.push_back({a,b});
}
long long z = 0;
for(auto i:comp){
sav[i.first] = ++z;
}
for(int i = 0;i<n;i++){
v[i].first = sav[v[i].first];
v[i].second = sav[v[i].second];
}
build(1,0,z);
long long lol = -1e7;
sort(v.begin(),v.end(),cmp);
for(int i = 0;i<v.size();i++){
long long ans = query(1,0,z,v[i].first,v[i].second);
//cout<<ans<<endl;
if(ans==1e18){
lol+=1e7;
ans = lol;
}else ans++;
//cout<<ans<<" "<<v[i].first<<" "<<v[i].second<<endl;
update(1,0,z,v[i].second,ans);
}
while(q--){
int a,b;cin>>a>>b;
a--;b--;
int a1 = query(1,0,z,v[a].second,v[a].second);
int a2 = query(1,0,z,v[b].first,v[b].second);
if(abs(a1-a2)>=1e7){
cout<<"impossible\n";
}else{
cout<<abs(a1-a2)<<endl;
}
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:53:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i = 0;i<v.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
448 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
370 ms |
16772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
448 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |