#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#define getchar_unlocked getchar
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define psf push_front
#define ppb pop_back
#define ppf pop_front
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(),x.end()
#define lb(x,v) (lower_bound(all(x),v)-x.begin())
#define ub(x,v) (upper_bound(all(x),v)-x.begin())
#define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin());
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> iii;
typedef tuple<int,int,int,int> iiii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
mt19937 rng(time(0));
#define mod 1000000007
inline int add(int a,int b){
int r=a+b;
while(r>=mod)r-=mod;
while(r<0)r+=mod;
return r;
}
inline int mult(int a,int b){
return (int)(((ll)(a*b))%mod);
}
inline int rd(){
int x=0;
char ch=getchar_unlocked();
while(!(ch&16))ch=getchar();//keep reading while current character is whitespace
while(ch&16){//this will break when ‘\n’ or ‘ ‘ is encountered
x=(x<<3)+(x<<1)+(ch&15);
ch=getchar_unlocked();
}
return x;
}
#define maxn 100005
int n,q,m,s[maxn],e[maxn],p[20][maxn];
ii st[20][2*maxn];
vector<int> d;
inline ii qmn(int l,int r){
int k=31-__builtin_clz(r-l+1);
return min(st[k][l],st[k][r-(1<<k)+1]);
}
int main(){
sf("%d%d",&n,&q);
for(int i=0;i<n;++i){
sf("%d%d",&s[i],&e[i]);
d.pb(s[i]);d.pb(e[i]);
}
disc(d);
for(int i=0;i<n;++i){
s[i]=lb(d,s[i]);
e[i]=lb(d,e[i]);
}
m=sz(d);
for(int i=0;i<m;++i)st[0][i]={INF,-1};
for(int i=0;i<n;++i)mnto(st[0][e[i]],ii(s[i],i));
for(int k=1;k<20;++k){
for(int i=0;i+(1<<k)<=m;++i){
st[k][i]=min(st[k-1][i],st[k-1][i+(1<<k-1)]);
}
}
for(int i=0;i<n;++i)p[0][i]=qmn(s[i],e[i]).se;
for(int k=0;k<19;++k){
for(int i=0;i<n;++i){
if(p[k][i]==-1)p[k+1][i]=-1;
else p[k+1][i]=p[k][p[k][i]];
}
}
for(int i=0;i<q;++i){
int x,y;sf("%d%d",&x,&y);
if(x==y){
pf("0\n");
continue;
}
--x,--y;
if(e[y]<e[x]){
pf("impossible\n");
continue;
}
if(s[y]<=e[x]){
pf("1\n");
continue;
}
int cur=y,ans=0;
for(int k=19;k>=0;--k){
int pcur=p[k][cur];
if(pcur!=-1&&e[x]<s[pcur]){
cur=pcur;
ans+=1<<k;
}
}
int pcur=p[0][cur];
if(pcur==-1||e[x]<s[pcur]){
pf("impossible\n");
}
else pf("%d\n",ans+2);
}
}
/*
10 10
0 1
1 5
2 4
3 5
5 8
6 8
6 7
7 10
9 11
5 12
*/
Compilation message
events.cpp: In function 'int main()':
events.cpp:89:43: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
89 | st[k][i]=min(st[k-1][i],st[k-1][i+(1<<k-1)]);
| ~^~
events.cpp:74:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | sf("%d%d",&n,&q);
| ^
events.cpp:76:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | sf("%d%d",&s[i],&e[i]);
| ^
events.cpp:102:13: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | int x,y;sf("%d%d",&x,&y);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
102 ms |
22780 KB |
Output is correct |
3 |
Correct |
113 ms |
22764 KB |
Output is correct |
4 |
Correct |
148 ms |
22720 KB |
Output is correct |
5 |
Correct |
127 ms |
27204 KB |
Output is correct |
6 |
Correct |
126 ms |
28484 KB |
Output is correct |
7 |
Correct |
130 ms |
28860 KB |
Output is correct |
8 |
Correct |
139 ms |
40128 KB |
Output is correct |
9 |
Correct |
139 ms |
40124 KB |
Output is correct |
10 |
Correct |
120 ms |
26148 KB |
Output is correct |
11 |
Correct |
141 ms |
31552 KB |
Output is correct |
12 |
Correct |
66 ms |
25532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
712 KB |
Output is correct |
8 |
Correct |
1 ms |
744 KB |
Output is correct |
9 |
Correct |
1 ms |
456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
712 KB |
Output is correct |
8 |
Correct |
1 ms |
744 KB |
Output is correct |
9 |
Correct |
1 ms |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
580 KB |
Output is correct |
15 |
Correct |
1 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
36 ms |
3076 KB |
Output is correct |
20 |
Correct |
36 ms |
2892 KB |
Output is correct |
21 |
Correct |
37 ms |
3132 KB |
Output is correct |
22 |
Correct |
32 ms |
3296 KB |
Output is correct |
23 |
Correct |
27 ms |
3648 KB |
Output is correct |
24 |
Correct |
27 ms |
3568 KB |
Output is correct |
25 |
Correct |
42 ms |
3232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
712 KB |
Output is correct |
8 |
Correct |
1 ms |
744 KB |
Output is correct |
9 |
Correct |
1 ms |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
584 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
580 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
70 ms |
24148 KB |
Output is correct |
20 |
Correct |
68 ms |
23252 KB |
Output is correct |
21 |
Correct |
71 ms |
23948 KB |
Output is correct |
22 |
Correct |
79 ms |
28976 KB |
Output is correct |
23 |
Correct |
80 ms |
37856 KB |
Output is correct |
24 |
Correct |
84 ms |
37948 KB |
Output is correct |
25 |
Correct |
26 ms |
11092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
22780 KB |
Output is correct |
2 |
Correct |
114 ms |
22724 KB |
Output is correct |
3 |
Correct |
145 ms |
22660 KB |
Output is correct |
4 |
Correct |
141 ms |
40256 KB |
Output is correct |
5 |
Correct |
123 ms |
26168 KB |
Output is correct |
6 |
Correct |
131 ms |
39804 KB |
Output is correct |
7 |
Correct |
133 ms |
39752 KB |
Output is correct |
8 |
Correct |
120 ms |
39912 KB |
Output is correct |
9 |
Correct |
80 ms |
37808 KB |
Output is correct |
10 |
Correct |
123 ms |
39464 KB |
Output is correct |
11 |
Correct |
115 ms |
39236 KB |
Output is correct |
12 |
Correct |
122 ms |
39376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
102 ms |
22780 KB |
Output is correct |
3 |
Correct |
113 ms |
22764 KB |
Output is correct |
4 |
Correct |
148 ms |
22720 KB |
Output is correct |
5 |
Correct |
127 ms |
27204 KB |
Output is correct |
6 |
Correct |
126 ms |
28484 KB |
Output is correct |
7 |
Correct |
130 ms |
28860 KB |
Output is correct |
8 |
Correct |
139 ms |
40128 KB |
Output is correct |
9 |
Correct |
139 ms |
40124 KB |
Output is correct |
10 |
Correct |
120 ms |
26148 KB |
Output is correct |
11 |
Correct |
141 ms |
31552 KB |
Output is correct |
12 |
Correct |
66 ms |
25532 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
712 KB |
Output is correct |
20 |
Correct |
1 ms |
744 KB |
Output is correct |
21 |
Correct |
1 ms |
456 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
580 KB |
Output is correct |
27 |
Correct |
1 ms |
620 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
36 ms |
3076 KB |
Output is correct |
32 |
Correct |
36 ms |
2892 KB |
Output is correct |
33 |
Correct |
37 ms |
3132 KB |
Output is correct |
34 |
Correct |
32 ms |
3296 KB |
Output is correct |
35 |
Correct |
27 ms |
3648 KB |
Output is correct |
36 |
Correct |
27 ms |
3568 KB |
Output is correct |
37 |
Correct |
42 ms |
3232 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
584 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
2 ms |
580 KB |
Output is correct |
44 |
Correct |
1 ms |
724 KB |
Output is correct |
45 |
Correct |
2 ms |
724 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
70 ms |
24148 KB |
Output is correct |
48 |
Correct |
68 ms |
23252 KB |
Output is correct |
49 |
Correct |
71 ms |
23948 KB |
Output is correct |
50 |
Correct |
79 ms |
28976 KB |
Output is correct |
51 |
Correct |
80 ms |
37856 KB |
Output is correct |
52 |
Correct |
84 ms |
37948 KB |
Output is correct |
53 |
Correct |
26 ms |
11092 KB |
Output is correct |
54 |
Correct |
98 ms |
22780 KB |
Output is correct |
55 |
Correct |
114 ms |
22724 KB |
Output is correct |
56 |
Correct |
145 ms |
22660 KB |
Output is correct |
57 |
Correct |
141 ms |
40256 KB |
Output is correct |
58 |
Correct |
123 ms |
26168 KB |
Output is correct |
59 |
Correct |
131 ms |
39804 KB |
Output is correct |
60 |
Correct |
133 ms |
39752 KB |
Output is correct |
61 |
Correct |
120 ms |
39912 KB |
Output is correct |
62 |
Correct |
80 ms |
37808 KB |
Output is correct |
63 |
Correct |
123 ms |
39464 KB |
Output is correct |
64 |
Correct |
115 ms |
39236 KB |
Output is correct |
65 |
Correct |
122 ms |
39376 KB |
Output is correct |
66 |
Correct |
0 ms |
340 KB |
Output is correct |
67 |
Correct |
98 ms |
25792 KB |
Output is correct |
68 |
Correct |
116 ms |
25792 KB |
Output is correct |
69 |
Correct |
153 ms |
25816 KB |
Output is correct |
70 |
Correct |
124 ms |
27172 KB |
Output is correct |
71 |
Correct |
123 ms |
28480 KB |
Output is correct |
72 |
Correct |
124 ms |
28864 KB |
Output is correct |
73 |
Correct |
136 ms |
40128 KB |
Output is correct |
74 |
Correct |
147 ms |
40172 KB |
Output is correct |
75 |
Correct |
115 ms |
26152 KB |
Output is correct |
76 |
Correct |
128 ms |
31840 KB |
Output is correct |
77 |
Correct |
67 ms |
25532 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
624 KB |
Output is correct |
80 |
Correct |
1 ms |
584 KB |
Output is correct |
81 |
Correct |
1 ms |
596 KB |
Output is correct |
82 |
Correct |
1 ms |
620 KB |
Output is correct |
83 |
Correct |
1 ms |
724 KB |
Output is correct |
84 |
Correct |
1 ms |
724 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
36 ms |
3096 KB |
Output is correct |
87 |
Correct |
40 ms |
2916 KB |
Output is correct |
88 |
Correct |
38 ms |
3144 KB |
Output is correct |
89 |
Correct |
31 ms |
3276 KB |
Output is correct |
90 |
Correct |
26 ms |
3616 KB |
Output is correct |
91 |
Correct |
24 ms |
3668 KB |
Output is correct |
92 |
Correct |
33 ms |
3332 KB |
Output is correct |
93 |
Correct |
68 ms |
24116 KB |
Output is correct |
94 |
Correct |
65 ms |
23264 KB |
Output is correct |
95 |
Correct |
73 ms |
23952 KB |
Output is correct |
96 |
Correct |
74 ms |
28988 KB |
Output is correct |
97 |
Correct |
77 ms |
37824 KB |
Output is correct |
98 |
Correct |
81 ms |
37876 KB |
Output is correct |
99 |
Correct |
25 ms |
11064 KB |
Output is correct |
100 |
Correct |
138 ms |
39744 KB |
Output is correct |
101 |
Correct |
137 ms |
39728 KB |
Output is correct |
102 |
Correct |
118 ms |
39848 KB |
Output is correct |
103 |
Correct |
129 ms |
39368 KB |
Output is correct |
104 |
Correct |
112 ms |
39236 KB |
Output is correct |
105 |
Correct |
120 ms |
39360 KB |
Output is correct |
106 |
Correct |
111 ms |
21916 KB |
Output is correct |
107 |
Correct |
147 ms |
25284 KB |
Output is correct |
108 |
Correct |
113 ms |
25936 KB |
Output is correct |
109 |
Correct |
113 ms |
26044 KB |
Output is correct |
110 |
Correct |
108 ms |
39668 KB |
Output is correct |
111 |
Correct |
112 ms |
39616 KB |
Output is correct |