#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=1e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
struct node
{
ll val,id;
node()
{
val=inf,id=inf;
}
node operator+(const node&o)
{
node ans;
if(val<o.val) ans.val=val,ans.id=id;
else ans=o;
return ans;
}
}st[4*maxN];
ll n,q;
void update(ll pos,ll val,ll id=1,ll l=1,ll r=n)
{
if(l==r)
{
st[id].val=val;
st[id].id=l;
return;
}
ll mid=l+r>>1;
if(pos<=mid) update(pos,val,id*2,l,mid);
else update(pos,val,id*2+1,mid+1,r);
st[id]=st[id*2]+st[id*2+1];
}
node get(ll i,ll j,ll id=1,ll l=1,ll r=n)
{
if(j<l||r<i) return node();
if(i<=l&&r<=j) return st[id];
ll mid=l+r>>1;
return get(i,j,id*2,l,mid)+get(i,j,id*2+1,mid+1,r);
}
struct qq
{
ll s,e,id;
bool operator<(const qq&o)
{
if(e==o.e) return s<o.s;
return e<o.e;
}
}a[maxN];
ll pos[maxN];
ll pre[maxN][19];
void solve()
{
cin >> n >> q;
for(int i=1;i<=n;i++)
{
cin >> a[i].s >> a[i].e;
a[i].id=i;
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
pos[a[i].id]=i;
ll low=1,high=i-1;
while(low<=high)
{
ll mid=low+high>>1;
if(a[mid].e<a[i].s) low=mid+1;
else high=mid-1;
}
auto val=get(low,i-1);
if(val.val==inf||val.val>=a[i].s) pre[i][0]=i;
else pre[i][0]=val.id;
update(i,a[i].s);
}
for(int j=1;j<=18;j++)
{
for(int i=1;i<=n;i++)
{
pre[i][j]=pre[pre[i][j-1]][j-1];
}
}
for(int i=1;i<=q;i++)
{
ll x,y;
cin >> x >> y;
x=pos[x];
y=pos[y];
if(x==y)
{
cout << 0<<'\n';
}
else if(a[x].e==a[y].e)
{
cout << 1<<'\n';
}
else if(x>y)
{
cout << "impossible\n";
}
else if(a[x].e<=a[y].e&&a[x].e>=a[y].s)
{
cout << 1 << '\n';
}
else
{
ll ans=0;
for(int j=18;j>=0;j--)
{
if(a[pre[y][j]].s>a[x].e)
{
y=pre[y][j],ans+=1<<j;
}
}
if(a[pre[y][0]].s<=a[x].e&&a[pre[y][0]].e>=a[x].e)
{
ans+=2;
cout << ans << '\n';
}
else
{
cout << "impossible\n";
}
}
}
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
events.cpp: In function 'void update(ll, ll, ll, ll, ll)':
events.cpp:37:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | ll mid=l+r>>1;
| ~^~
events.cpp: In function 'node get(ll, ll, ll, ll, ll)':
events.cpp:46:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | ll mid=l+r>>1;
| ~^~
events.cpp: In function 'void solve()':
events.cpp:75:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
75 | ll mid=low+high>>1;
| ~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
103 ms |
25144 KB |
Output is correct |
3 |
Correct |
106 ms |
25164 KB |
Output is correct |
4 |
Correct |
135 ms |
25144 KB |
Output is correct |
5 |
Correct |
116 ms |
28344 KB |
Output is correct |
6 |
Correct |
124 ms |
28312 KB |
Output is correct |
7 |
Correct |
120 ms |
28360 KB |
Output is correct |
8 |
Correct |
101 ms |
28704 KB |
Output is correct |
9 |
Correct |
104 ms |
28748 KB |
Output is correct |
10 |
Correct |
135 ms |
28620 KB |
Output is correct |
11 |
Correct |
135 ms |
28784 KB |
Output is correct |
12 |
Correct |
97 ms |
27980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6608 KB |
Output is correct |
3 |
Correct |
3 ms |
6740 KB |
Output is correct |
4 |
Correct |
5 ms |
6720 KB |
Output is correct |
5 |
Correct |
4 ms |
6740 KB |
Output is correct |
6 |
Correct |
4 ms |
6736 KB |
Output is correct |
7 |
Correct |
5 ms |
6740 KB |
Output is correct |
8 |
Correct |
4 ms |
6732 KB |
Output is correct |
9 |
Correct |
3 ms |
6740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6608 KB |
Output is correct |
3 |
Correct |
3 ms |
6740 KB |
Output is correct |
4 |
Correct |
5 ms |
6720 KB |
Output is correct |
5 |
Correct |
4 ms |
6740 KB |
Output is correct |
6 |
Correct |
4 ms |
6736 KB |
Output is correct |
7 |
Correct |
5 ms |
6740 KB |
Output is correct |
8 |
Correct |
4 ms |
6732 KB |
Output is correct |
9 |
Correct |
3 ms |
6740 KB |
Output is correct |
10 |
Correct |
3 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6484 KB |
Output is correct |
12 |
Correct |
3 ms |
6740 KB |
Output is correct |
13 |
Correct |
5 ms |
6772 KB |
Output is correct |
14 |
Correct |
4 ms |
6740 KB |
Output is correct |
15 |
Correct |
4 ms |
6740 KB |
Output is correct |
16 |
Correct |
4 ms |
6740 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
4 ms |
6740 KB |
Output is correct |
19 |
Correct |
34 ms |
8868 KB |
Output is correct |
20 |
Correct |
30 ms |
8908 KB |
Output is correct |
21 |
Correct |
36 ms |
9164 KB |
Output is correct |
22 |
Correct |
27 ms |
9248 KB |
Output is correct |
23 |
Correct |
29 ms |
9172 KB |
Output is correct |
24 |
Correct |
35 ms |
9164 KB |
Output is correct |
25 |
Correct |
34 ms |
8780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6608 KB |
Output is correct |
3 |
Correct |
3 ms |
6740 KB |
Output is correct |
4 |
Correct |
5 ms |
6720 KB |
Output is correct |
5 |
Correct |
4 ms |
6740 KB |
Output is correct |
6 |
Correct |
4 ms |
6736 KB |
Output is correct |
7 |
Correct |
5 ms |
6740 KB |
Output is correct |
8 |
Correct |
4 ms |
6732 KB |
Output is correct |
9 |
Correct |
3 ms |
6740 KB |
Output is correct |
10 |
Correct |
3 ms |
6548 KB |
Output is correct |
11 |
Correct |
3 ms |
6596 KB |
Output is correct |
12 |
Correct |
4 ms |
6712 KB |
Output is correct |
13 |
Correct |
3 ms |
6736 KB |
Output is correct |
14 |
Correct |
4 ms |
6740 KB |
Output is correct |
15 |
Correct |
4 ms |
6728 KB |
Output is correct |
16 |
Correct |
4 ms |
6740 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
4 ms |
6728 KB |
Output is correct |
19 |
Correct |
83 ms |
26496 KB |
Output is correct |
20 |
Correct |
74 ms |
25720 KB |
Output is correct |
21 |
Correct |
92 ms |
26484 KB |
Output is correct |
22 |
Correct |
81 ms |
26444 KB |
Output is correct |
23 |
Correct |
85 ms |
26484 KB |
Output is correct |
24 |
Correct |
94 ms |
26496 KB |
Output is correct |
25 |
Correct |
66 ms |
25868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
25120 KB |
Output is correct |
2 |
Correct |
113 ms |
25140 KB |
Output is correct |
3 |
Correct |
115 ms |
25048 KB |
Output is correct |
4 |
Correct |
120 ms |
28676 KB |
Output is correct |
5 |
Correct |
161 ms |
28608 KB |
Output is correct |
6 |
Correct |
158 ms |
28400 KB |
Output is correct |
7 |
Correct |
154 ms |
28288 KB |
Output is correct |
8 |
Correct |
145 ms |
28616 KB |
Output is correct |
9 |
Correct |
75 ms |
26508 KB |
Output is correct |
10 |
Correct |
123 ms |
27964 KB |
Output is correct |
11 |
Correct |
120 ms |
27788 KB |
Output is correct |
12 |
Correct |
137 ms |
27976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
103 ms |
25144 KB |
Output is correct |
3 |
Correct |
106 ms |
25164 KB |
Output is correct |
4 |
Correct |
135 ms |
25144 KB |
Output is correct |
5 |
Correct |
116 ms |
28344 KB |
Output is correct |
6 |
Correct |
124 ms |
28312 KB |
Output is correct |
7 |
Correct |
120 ms |
28360 KB |
Output is correct |
8 |
Correct |
101 ms |
28704 KB |
Output is correct |
9 |
Correct |
104 ms |
28748 KB |
Output is correct |
10 |
Correct |
135 ms |
28620 KB |
Output is correct |
11 |
Correct |
135 ms |
28784 KB |
Output is correct |
12 |
Correct |
97 ms |
27980 KB |
Output is correct |
13 |
Correct |
4 ms |
6484 KB |
Output is correct |
14 |
Correct |
3 ms |
6608 KB |
Output is correct |
15 |
Correct |
3 ms |
6740 KB |
Output is correct |
16 |
Correct |
5 ms |
6720 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
4 ms |
6736 KB |
Output is correct |
19 |
Correct |
5 ms |
6740 KB |
Output is correct |
20 |
Correct |
4 ms |
6732 KB |
Output is correct |
21 |
Correct |
3 ms |
6740 KB |
Output is correct |
22 |
Correct |
3 ms |
6484 KB |
Output is correct |
23 |
Correct |
3 ms |
6484 KB |
Output is correct |
24 |
Correct |
3 ms |
6740 KB |
Output is correct |
25 |
Correct |
5 ms |
6772 KB |
Output is correct |
26 |
Correct |
4 ms |
6740 KB |
Output is correct |
27 |
Correct |
4 ms |
6740 KB |
Output is correct |
28 |
Correct |
4 ms |
6740 KB |
Output is correct |
29 |
Correct |
4 ms |
6740 KB |
Output is correct |
30 |
Correct |
4 ms |
6740 KB |
Output is correct |
31 |
Correct |
34 ms |
8868 KB |
Output is correct |
32 |
Correct |
30 ms |
8908 KB |
Output is correct |
33 |
Correct |
36 ms |
9164 KB |
Output is correct |
34 |
Correct |
27 ms |
9248 KB |
Output is correct |
35 |
Correct |
29 ms |
9172 KB |
Output is correct |
36 |
Correct |
35 ms |
9164 KB |
Output is correct |
37 |
Correct |
34 ms |
8780 KB |
Output is correct |
38 |
Correct |
3 ms |
6548 KB |
Output is correct |
39 |
Correct |
3 ms |
6596 KB |
Output is correct |
40 |
Correct |
4 ms |
6712 KB |
Output is correct |
41 |
Correct |
3 ms |
6736 KB |
Output is correct |
42 |
Correct |
4 ms |
6740 KB |
Output is correct |
43 |
Correct |
4 ms |
6728 KB |
Output is correct |
44 |
Correct |
4 ms |
6740 KB |
Output is correct |
45 |
Correct |
4 ms |
6740 KB |
Output is correct |
46 |
Correct |
4 ms |
6728 KB |
Output is correct |
47 |
Correct |
83 ms |
26496 KB |
Output is correct |
48 |
Correct |
74 ms |
25720 KB |
Output is correct |
49 |
Correct |
92 ms |
26484 KB |
Output is correct |
50 |
Correct |
81 ms |
26444 KB |
Output is correct |
51 |
Correct |
85 ms |
26484 KB |
Output is correct |
52 |
Correct |
94 ms |
26496 KB |
Output is correct |
53 |
Correct |
66 ms |
25868 KB |
Output is correct |
54 |
Correct |
133 ms |
25120 KB |
Output is correct |
55 |
Correct |
113 ms |
25140 KB |
Output is correct |
56 |
Correct |
115 ms |
25048 KB |
Output is correct |
57 |
Correct |
120 ms |
28676 KB |
Output is correct |
58 |
Correct |
161 ms |
28608 KB |
Output is correct |
59 |
Correct |
158 ms |
28400 KB |
Output is correct |
60 |
Correct |
154 ms |
28288 KB |
Output is correct |
61 |
Correct |
145 ms |
28616 KB |
Output is correct |
62 |
Correct |
75 ms |
26508 KB |
Output is correct |
63 |
Correct |
123 ms |
27964 KB |
Output is correct |
64 |
Correct |
120 ms |
27788 KB |
Output is correct |
65 |
Correct |
137 ms |
27976 KB |
Output is correct |
66 |
Correct |
4 ms |
6588 KB |
Output is correct |
67 |
Correct |
109 ms |
28308 KB |
Output is correct |
68 |
Correct |
118 ms |
28264 KB |
Output is correct |
69 |
Correct |
131 ms |
28264 KB |
Output is correct |
70 |
Correct |
110 ms |
28228 KB |
Output is correct |
71 |
Correct |
126 ms |
28272 KB |
Output is correct |
72 |
Correct |
114 ms |
28344 KB |
Output is correct |
73 |
Correct |
104 ms |
28664 KB |
Output is correct |
74 |
Correct |
108 ms |
28696 KB |
Output is correct |
75 |
Correct |
148 ms |
28544 KB |
Output is correct |
76 |
Correct |
132 ms |
28664 KB |
Output is correct |
77 |
Correct |
99 ms |
27924 KB |
Output is correct |
78 |
Correct |
3 ms |
6484 KB |
Output is correct |
79 |
Correct |
4 ms |
6756 KB |
Output is correct |
80 |
Correct |
4 ms |
6740 KB |
Output is correct |
81 |
Correct |
4 ms |
6740 KB |
Output is correct |
82 |
Correct |
4 ms |
6740 KB |
Output is correct |
83 |
Correct |
4 ms |
6740 KB |
Output is correct |
84 |
Correct |
3 ms |
6740 KB |
Output is correct |
85 |
Correct |
3 ms |
6740 KB |
Output is correct |
86 |
Correct |
34 ms |
8936 KB |
Output is correct |
87 |
Correct |
42 ms |
8908 KB |
Output is correct |
88 |
Correct |
39 ms |
9248 KB |
Output is correct |
89 |
Correct |
32 ms |
9280 KB |
Output is correct |
90 |
Correct |
28 ms |
9160 KB |
Output is correct |
91 |
Correct |
27 ms |
9164 KB |
Output is correct |
92 |
Correct |
29 ms |
8712 KB |
Output is correct |
93 |
Correct |
85 ms |
26736 KB |
Output is correct |
94 |
Correct |
83 ms |
25724 KB |
Output is correct |
95 |
Correct |
90 ms |
26300 KB |
Output is correct |
96 |
Correct |
79 ms |
26496 KB |
Output is correct |
97 |
Correct |
76 ms |
26480 KB |
Output is correct |
98 |
Correct |
93 ms |
26500 KB |
Output is correct |
99 |
Correct |
74 ms |
25796 KB |
Output is correct |
100 |
Correct |
151 ms |
28416 KB |
Output is correct |
101 |
Correct |
143 ms |
28440 KB |
Output is correct |
102 |
Correct |
134 ms |
28568 KB |
Output is correct |
103 |
Correct |
112 ms |
27976 KB |
Output is correct |
104 |
Correct |
117 ms |
27788 KB |
Output is correct |
105 |
Correct |
119 ms |
28016 KB |
Output is correct |
106 |
Correct |
144 ms |
27664 KB |
Output is correct |
107 |
Correct |
176 ms |
27680 KB |
Output is correct |
108 |
Correct |
144 ms |
28272 KB |
Output is correct |
109 |
Correct |
120 ms |
28316 KB |
Output is correct |
110 |
Correct |
154 ms |
28280 KB |
Output is correct |
111 |
Correct |
149 ms |
28336 KB |
Output is correct |