#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int n,q;
pii s[1<<20];
pii pos[200001],com_pos[200001];
int jump[200001][18];
map<int,int> com;
void update(int l, int r, int idx, int x, pii node) {
if (x>r || x<l) return;
if (l==r) s[idx]=min(s[idx],node);
else {
int mid=(l+r)/2;
update(l,mid,2*idx,x,node);
update(mid+1,r,2*idx+1,x,node);
s[idx]=min(s[2*idx],s[2*idx+1]);
}
}
pii query(int l, int r, int idx, int x, int y) {
if (x>r || y<l) return pii(INT_MAX,INT_MAX);
if (x<=l && r<=y) return s[idx];
int mid=(l+r)/2;
return min(query(l,mid,2*idx,x,y),query(mid+1,r,2*idx+1,x,y));
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>q;
for (int i=0; i<1<<18; ++i) s[i]=pii(INT_MAX,INT_MAX);
for (int i=1; i<=n; ++i) {
int st,ed; cin>>st>>ed;
pos[i].first=st;
pos[i].second=ed;
com[st]=1;
com[ed]=1;
}
int cnt=1;
for (auto it=com.begin(); it!=com.end(); ++it) (*it).second=cnt++;
for (int i=1; i<=n; ++i) {
com_pos[i]=pii(com[pos[i].first],com[pos[i].second]);
update(1,2*n,1,com_pos[i].second,pii(com_pos[i].first,i));
}
for (int i=1; i<=n; ++i) jump[i][0]=query(1,2*n,1,com_pos[i].first,com_pos[i].second).second;
for (int i=1; i<17; ++i) {
for (int j=1; j<=n; ++j) {
jump[j][i]=jump[jump[j][i-1]][i-1];
}
}
while (q--) {
int st,ed,cnt=1;
cin>>st>>ed;
if (com_pos[st].second>com_pos[ed].second) cout<<"impossible\n";
else if (st==ed) cout<<"0\n";
else {
if (com_pos[ed].first>com_pos[st].second) {
for (int i=16; i>=0; --i) {
if (com_pos[jump[ed][i]].first>com_pos[st].second) {
ed=jump[ed][i];
cnt+=(1<<i);
}
}
if (com_pos[jump[ed][0]].first<=com_pos[st].second) cout<<cnt+1<<"\n";
else cout<<"impossible\n";
} else cout<<1<<"\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Incorrect |
218 ms |
17128 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2516 KB |
Output is correct |
4 |
Correct |
2 ms |
2516 KB |
Output is correct |
5 |
Correct |
2 ms |
2508 KB |
Output is correct |
6 |
Correct |
2 ms |
2516 KB |
Output is correct |
7 |
Correct |
2 ms |
2516 KB |
Output is correct |
8 |
Correct |
2 ms |
2516 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2516 KB |
Output is correct |
4 |
Correct |
2 ms |
2516 KB |
Output is correct |
5 |
Correct |
2 ms |
2508 KB |
Output is correct |
6 |
Correct |
2 ms |
2516 KB |
Output is correct |
7 |
Correct |
2 ms |
2516 KB |
Output is correct |
8 |
Correct |
2 ms |
2516 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2388 KB |
Output is correct |
11 |
Correct |
1 ms |
2380 KB |
Output is correct |
12 |
Correct |
2 ms |
2516 KB |
Output is correct |
13 |
Correct |
3 ms |
2516 KB |
Output is correct |
14 |
Correct |
3 ms |
2516 KB |
Output is correct |
15 |
Correct |
3 ms |
2516 KB |
Output is correct |
16 |
Correct |
3 ms |
2516 KB |
Output is correct |
17 |
Correct |
4 ms |
2516 KB |
Output is correct |
18 |
Correct |
2 ms |
2384 KB |
Output is correct |
19 |
Correct |
35 ms |
3832 KB |
Output is correct |
20 |
Correct |
31 ms |
3788 KB |
Output is correct |
21 |
Correct |
32 ms |
4044 KB |
Output is correct |
22 |
Correct |
27 ms |
4024 KB |
Output is correct |
23 |
Correct |
36 ms |
4124 KB |
Output is correct |
24 |
Correct |
36 ms |
4148 KB |
Output is correct |
25 |
Correct |
30 ms |
3788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2516 KB |
Output is correct |
4 |
Correct |
2 ms |
2516 KB |
Output is correct |
5 |
Correct |
2 ms |
2508 KB |
Output is correct |
6 |
Correct |
2 ms |
2516 KB |
Output is correct |
7 |
Correct |
2 ms |
2516 KB |
Output is correct |
8 |
Correct |
2 ms |
2516 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
1 ms |
2388 KB |
Output is correct |
12 |
Correct |
3 ms |
2516 KB |
Output is correct |
13 |
Correct |
3 ms |
2516 KB |
Output is correct |
14 |
Correct |
2 ms |
2516 KB |
Output is correct |
15 |
Correct |
3 ms |
2516 KB |
Output is correct |
16 |
Correct |
3 ms |
2516 KB |
Output is correct |
17 |
Correct |
4 ms |
2516 KB |
Output is correct |
18 |
Correct |
2 ms |
2388 KB |
Output is correct |
19 |
Incorrect |
158 ms |
16964 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
232 ms |
16796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Incorrect |
218 ms |
17128 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |