#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<pair<int,int>,int> a,pair<pair<int,int>,int> b){
return a.first.second<b.first.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,long long 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<pair<int,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},i});
}
long long z = 0;
for(auto i:comp){
sav[i.first] = ++z;
}
for(int i = 0;i<n;i++){
v[i].first.first = sav[v[i].first.first];
v[i].first.second = sav[v[i].first.second];
}
build(1,0,z);
long long lol = -1e7;
sort(v.begin(),v.end(),cmp);
long long llol[n] = {0};
for(int i = 0;i<v.size();i++){
long long ans = query(1,0,z,v[i].first.first,v[i].first.second);
//cout<<ans<<endl;
if(ans==1e18){
lol+=1e7;
ans = lol;
}else ans++;
llol[v[i].second] = ans;
//cout<<ans<<" "<<v[i].first<<" "<<v[i].second<<endl;
update(1,0,z,v[i].first.second,ans);
}
while(q--){
int a,b;cin>>a>>b;
a--;b--;
long long a1 = llol[a];
long long a2 = llol[b];
if(abs(a1-a2)>=1e7){
cout<<"impossible\n";
}else{
cout<<abs(a1-a2)<<endl;
}
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int i = 0;i<v.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
340 ms |
14592 KB |
Output is correct |
3 |
Correct |
338 ms |
17668 KB |
Output is correct |
4 |
Correct |
338 ms |
17672 KB |
Output is correct |
5 |
Incorrect |
369 ms |
18612 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
361 ms |
14788 KB |
Output is correct |
2 |
Correct |
342 ms |
17664 KB |
Output is correct |
3 |
Correct |
335 ms |
17664 KB |
Output is correct |
4 |
Correct |
382 ms |
29440 KB |
Output is correct |
5 |
Incorrect |
344 ms |
17872 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
340 ms |
14592 KB |
Output is correct |
3 |
Correct |
338 ms |
17668 KB |
Output is correct |
4 |
Correct |
338 ms |
17672 KB |
Output is correct |
5 |
Incorrect |
369 ms |
18612 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |