#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);
int P[100001][17],ind[100001];
int in = 0;
for(int i = 0;i<v.size();i++){
ind[v[i].second] = i;
while(in<n&&v[in].first.first<=v[i].first.second){
in++;
}
P[i][0] = in-1;
cout<<in<<" "<<v[i].first.first<<" "<<v[i].first.second;
}
for(int i = n-1;i>=0;i--){
for(int j = 1;j<17;j++){
P[i][j] = P[P[i][j-1]][j-1];
}
}
while(q--){
int a,b;cin>>a>>b;
a--;b--;
if(ind[a]>ind[b]){
cout<<"impossible\n";
continue;
}
int sum = 0 , cur = ind[a];
for(int i = 16;i>=0;i--){
if(v[P[cur][i]].second<v[ind[b]].second){
sum+=(1<<i);
cur = P[cur][i];
}
}
if(v[P[cur][0]].second>=v[ind[b]].second){
cout<<sum+1<<endl;
}else cout<<"impossible\n";
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:54: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]
54 | for(int i = 0;i<v.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
14428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
14424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
14424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
14424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
353 ms |
33340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
14428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |