#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 = -1e9;
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+=1e9;
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(a2-a1>=1e7||a2-a1<0){
cout<<"impossible\n";
}else{
cout<<a2-a1<<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 |
356 ms |
19712 KB |
Output is correct |
3 |
Correct |
368 ms |
19660 KB |
Output is correct |
4 |
Correct |
345 ms |
19448 KB |
Output is correct |
5 |
Incorrect |
341 ms |
20984 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 |
Correct |
2 ms |
600 KB |
Output is correct |
5 |
Correct |
2 ms |
600 KB |
Output is correct |
6 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
7 |
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 |
Correct |
2 ms |
600 KB |
Output is correct |
5 |
Correct |
2 ms |
600 KB |
Output is correct |
6 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
7 |
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 |
Correct |
2 ms |
600 KB |
Output is correct |
5 |
Correct |
2 ms |
600 KB |
Output is correct |
6 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
354 ms |
19920 KB |
Output is correct |
2 |
Correct |
343 ms |
19632 KB |
Output is correct |
3 |
Correct |
361 ms |
19772 KB |
Output is correct |
4 |
Correct |
417 ms |
34796 KB |
Output is correct |
5 |
Correct |
328 ms |
20144 KB |
Output is correct |
6 |
Incorrect |
466 ms |
37628 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
356 ms |
19712 KB |
Output is correct |
3 |
Correct |
368 ms |
19660 KB |
Output is correct |
4 |
Correct |
345 ms |
19448 KB |
Output is correct |
5 |
Incorrect |
341 ms |
20984 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |