#include <bits/stdc++.h>
using namespace std;
#define int long long
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]);
}
signed 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},ind[n] = {0};
long long la = 0;
for(int i = 0;i<v.size();i++){
ind[v[i].second] = 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;
//assert(ans>=la);
la = 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--;
if(ind[a]>ind[b]){
cout<<"impossible\n";
continue;
}
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:56:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int i = 0;i<v.size();i++){
| ~^~~~~~~~~
events.cpp:55:15: warning: variable 'la' set but not used [-Wunused-but-set-variable]
55 | long long la = 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
338 ms |
19780 KB |
Output is correct |
3 |
Correct |
355 ms |
19692 KB |
Output is correct |
4 |
Correct |
353 ms |
19564 KB |
Output is correct |
5 |
Incorrect |
348 ms |
20832 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
600 KB |
Output is correct |
4 |
Incorrect |
2 ms |
600 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
600 KB |
Output is correct |
4 |
Incorrect |
2 ms |
600 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
600 KB |
Output is correct |
4 |
Incorrect |
2 ms |
600 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
354 ms |
19612 KB |
Output is correct |
2 |
Correct |
342 ms |
20220 KB |
Output is correct |
3 |
Correct |
346 ms |
19480 KB |
Output is correct |
4 |
Correct |
396 ms |
35364 KB |
Output is correct |
5 |
Incorrect |
340 ms |
20476 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
338 ms |
19780 KB |
Output is correct |
3 |
Correct |
355 ms |
19692 KB |
Output is correct |
4 |
Correct |
353 ms |
19564 KB |
Output is correct |
5 |
Incorrect |
348 ms |
20832 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |