#include <bits/stdc++.h>
using namespace std;
int n,q;
typedef pair<int,int> P;
P arr[100000];
vector<int> pr;
const int sz=262144;
P seg[sz*2];
int table[100000][17];
P merge(P a,P b) {
return a<b?a:b;
}
P sum(int l,int r,int node=1,int nodel=0,int noder=sz-1) {
if (r<nodel||l>noder) {
return P(1e9+7,-1);
}
if (l<=nodel&&noder<=r) {
return seg[node];
}
int mid=(nodel+noder)/2;
return merge(sum(l,r,node*2,nodel,mid),sum(l,r,node*2+1,mid+1,noder));
}
void update(int i,P val) {
i+=sz;
seg[i]=merge(seg[i],val);
while (i>1) {
i/=2;
seg[i]=merge(seg[i*2],seg[i*2+1]);
}
}
int main(void) {
scanf("%d %d",&n,&q);
for(int i=1;i<sz*2;i++) {
seg[i]=P(1e9+7,-1);
}
for(int i=0;i<n;i++) {
scanf("%d %d",&arr[i].first,&arr[i].second);
pr.push_back(arr[i].first);
pr.push_back(arr[i].second);
}
sort(pr.begin(),pr.end());
pr.erase(unique(pr.begin(),pr.end()),pr.end());
for(int i=0;i<n;i++) {
arr[i].first=lower_bound(pr.begin(),pr.end(),arr[i].first)-pr.begin();
arr[i].second=lower_bound(pr.begin(),pr.end(),arr[i].second)-pr.begin();
//printf("%d %d\n",arr[i].first,arr[i].second);
update(arr[i].second,P(arr[i].first,i));
}
memset(table,-1,sizeof(table));
for(int i=0;i<n;i++) {
P val=sum(arr[i].first,arr[i].second);
//printf("%d %d\n",val.first,val.second);
if (val.first<arr[i].first) {
table[i][0]=val.second;
}
//printf("%d.\n",table[i][0]);
}
for(int j=1;j<17;j++) {
for(int i=0;i<n;i++) {
if (table[i][j-1]!=-1) {
table[i][j]=table[table[i][j-1]][j-1];
}
}
}
for(int i=0;i<q;i++) {
int s,e;
scanf("%d %d",&s,&e);
s--;
e--;
if (s==e) {
printf("0\n");
continue;
}
if (arr[e].second<arr[s].second) {
printf("impossible\n");
continue;
}
if (arr[e].first<=arr[s].second) {
printf("1\n");
continue;
}
int now=e;
int ret=0;
for(int j=16;j>=0;j--) {
int nt=table[now][j];
//printf("%d %d %d\n",i,j,nt);
if (nt!=-1&&arr[nt].first>arr[s].second) {
ret+=(1<<j);
now=nt;
}
}
int nt=table[now][0];
if (nt==-1||arr[nt].first>arr[s].second) {
printf("impossible\n");
continue;
}
printf("%d\n",ret+2);
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%d %d",&n,&q);
| ~~~~~^~~~~~~~~~~~~~~
events.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d %d",&arr[i].first,&arr[i].second);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d %d",&s,&e);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10960 KB |
Output is correct |
2 |
Correct |
132 ms |
13280 KB |
Output is correct |
3 |
Correct |
141 ms |
13244 KB |
Output is correct |
4 |
Correct |
174 ms |
13444 KB |
Output is correct |
5 |
Correct |
156 ms |
13348 KB |
Output is correct |
6 |
Correct |
149 ms |
13396 KB |
Output is correct |
7 |
Correct |
154 ms |
13432 KB |
Output is correct |
8 |
Correct |
135 ms |
13780 KB |
Output is correct |
9 |
Correct |
127 ms |
13784 KB |
Output is correct |
10 |
Correct |
154 ms |
13640 KB |
Output is correct |
11 |
Correct |
141 ms |
13652 KB |
Output is correct |
12 |
Correct |
105 ms |
13776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10964 KB |
Output is correct |
2 |
Correct |
5 ms |
10964 KB |
Output is correct |
3 |
Correct |
5 ms |
11040 KB |
Output is correct |
4 |
Correct |
6 ms |
10964 KB |
Output is correct |
5 |
Correct |
6 ms |
10964 KB |
Output is correct |
6 |
Correct |
6 ms |
10964 KB |
Output is correct |
7 |
Correct |
6 ms |
11092 KB |
Output is correct |
8 |
Correct |
6 ms |
11092 KB |
Output is correct |
9 |
Correct |
6 ms |
10964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10964 KB |
Output is correct |
2 |
Correct |
5 ms |
10964 KB |
Output is correct |
3 |
Correct |
5 ms |
11040 KB |
Output is correct |
4 |
Correct |
6 ms |
10964 KB |
Output is correct |
5 |
Correct |
6 ms |
10964 KB |
Output is correct |
6 |
Correct |
6 ms |
10964 KB |
Output is correct |
7 |
Correct |
6 ms |
11092 KB |
Output is correct |
8 |
Correct |
6 ms |
11092 KB |
Output is correct |
9 |
Correct |
6 ms |
10964 KB |
Output is correct |
10 |
Correct |
5 ms |
10988 KB |
Output is correct |
11 |
Correct |
7 ms |
11044 KB |
Output is correct |
12 |
Correct |
6 ms |
10964 KB |
Output is correct |
13 |
Correct |
9 ms |
10964 KB |
Output is correct |
14 |
Correct |
6 ms |
10964 KB |
Output is correct |
15 |
Correct |
6 ms |
10964 KB |
Output is correct |
16 |
Correct |
6 ms |
10968 KB |
Output is correct |
17 |
Correct |
6 ms |
11092 KB |
Output is correct |
18 |
Correct |
6 ms |
11092 KB |
Output is correct |
19 |
Correct |
45 ms |
11604 KB |
Output is correct |
20 |
Correct |
44 ms |
11596 KB |
Output is correct |
21 |
Correct |
47 ms |
11904 KB |
Output is correct |
22 |
Correct |
38 ms |
11920 KB |
Output is correct |
23 |
Correct |
35 ms |
11860 KB |
Output is correct |
24 |
Correct |
31 ms |
11788 KB |
Output is correct |
25 |
Correct |
37 ms |
11468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10964 KB |
Output is correct |
2 |
Correct |
5 ms |
10964 KB |
Output is correct |
3 |
Correct |
5 ms |
11040 KB |
Output is correct |
4 |
Correct |
6 ms |
10964 KB |
Output is correct |
5 |
Correct |
6 ms |
10964 KB |
Output is correct |
6 |
Correct |
6 ms |
10964 KB |
Output is correct |
7 |
Correct |
6 ms |
11092 KB |
Output is correct |
8 |
Correct |
6 ms |
11092 KB |
Output is correct |
9 |
Correct |
6 ms |
10964 KB |
Output is correct |
10 |
Correct |
6 ms |
10980 KB |
Output is correct |
11 |
Correct |
7 ms |
11044 KB |
Output is correct |
12 |
Correct |
6 ms |
11032 KB |
Output is correct |
13 |
Correct |
6 ms |
10964 KB |
Output is correct |
14 |
Correct |
7 ms |
11008 KB |
Output is correct |
15 |
Correct |
6 ms |
10964 KB |
Output is correct |
16 |
Correct |
6 ms |
11092 KB |
Output is correct |
17 |
Correct |
7 ms |
10964 KB |
Output is correct |
18 |
Correct |
7 ms |
10976 KB |
Output is correct |
19 |
Correct |
120 ms |
12732 KB |
Output is correct |
20 |
Correct |
119 ms |
12732 KB |
Output is correct |
21 |
Correct |
123 ms |
12624 KB |
Output is correct |
22 |
Correct |
121 ms |
12632 KB |
Output is correct |
23 |
Correct |
130 ms |
12736 KB |
Output is correct |
24 |
Correct |
122 ms |
12624 KB |
Output is correct |
25 |
Correct |
63 ms |
12628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
13268 KB |
Output is correct |
2 |
Correct |
146 ms |
13252 KB |
Output is correct |
3 |
Correct |
217 ms |
13200 KB |
Output is correct |
4 |
Correct |
129 ms |
13812 KB |
Output is correct |
5 |
Correct |
169 ms |
13632 KB |
Output is correct |
6 |
Correct |
203 ms |
13372 KB |
Output is correct |
7 |
Correct |
182 ms |
13452 KB |
Output is correct |
8 |
Correct |
174 ms |
13540 KB |
Output is correct |
9 |
Correct |
187 ms |
12632 KB |
Output is correct |
10 |
Correct |
175 ms |
13136 KB |
Output is correct |
11 |
Correct |
177 ms |
12896 KB |
Output is correct |
12 |
Correct |
181 ms |
13064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10960 KB |
Output is correct |
2 |
Correct |
132 ms |
13280 KB |
Output is correct |
3 |
Correct |
141 ms |
13244 KB |
Output is correct |
4 |
Correct |
174 ms |
13444 KB |
Output is correct |
5 |
Correct |
156 ms |
13348 KB |
Output is correct |
6 |
Correct |
149 ms |
13396 KB |
Output is correct |
7 |
Correct |
154 ms |
13432 KB |
Output is correct |
8 |
Correct |
135 ms |
13780 KB |
Output is correct |
9 |
Correct |
127 ms |
13784 KB |
Output is correct |
10 |
Correct |
154 ms |
13640 KB |
Output is correct |
11 |
Correct |
141 ms |
13652 KB |
Output is correct |
12 |
Correct |
105 ms |
13776 KB |
Output is correct |
13 |
Correct |
5 ms |
10964 KB |
Output is correct |
14 |
Correct |
5 ms |
10964 KB |
Output is correct |
15 |
Correct |
5 ms |
11040 KB |
Output is correct |
16 |
Correct |
6 ms |
10964 KB |
Output is correct |
17 |
Correct |
6 ms |
10964 KB |
Output is correct |
18 |
Correct |
6 ms |
10964 KB |
Output is correct |
19 |
Correct |
6 ms |
11092 KB |
Output is correct |
20 |
Correct |
6 ms |
11092 KB |
Output is correct |
21 |
Correct |
6 ms |
10964 KB |
Output is correct |
22 |
Correct |
5 ms |
10988 KB |
Output is correct |
23 |
Correct |
7 ms |
11044 KB |
Output is correct |
24 |
Correct |
6 ms |
10964 KB |
Output is correct |
25 |
Correct |
9 ms |
10964 KB |
Output is correct |
26 |
Correct |
6 ms |
10964 KB |
Output is correct |
27 |
Correct |
6 ms |
10964 KB |
Output is correct |
28 |
Correct |
6 ms |
10968 KB |
Output is correct |
29 |
Correct |
6 ms |
11092 KB |
Output is correct |
30 |
Correct |
6 ms |
11092 KB |
Output is correct |
31 |
Correct |
45 ms |
11604 KB |
Output is correct |
32 |
Correct |
44 ms |
11596 KB |
Output is correct |
33 |
Correct |
47 ms |
11904 KB |
Output is correct |
34 |
Correct |
38 ms |
11920 KB |
Output is correct |
35 |
Correct |
35 ms |
11860 KB |
Output is correct |
36 |
Correct |
31 ms |
11788 KB |
Output is correct |
37 |
Correct |
37 ms |
11468 KB |
Output is correct |
38 |
Correct |
6 ms |
10980 KB |
Output is correct |
39 |
Correct |
7 ms |
11044 KB |
Output is correct |
40 |
Correct |
6 ms |
11032 KB |
Output is correct |
41 |
Correct |
6 ms |
10964 KB |
Output is correct |
42 |
Correct |
7 ms |
11008 KB |
Output is correct |
43 |
Correct |
6 ms |
10964 KB |
Output is correct |
44 |
Correct |
6 ms |
11092 KB |
Output is correct |
45 |
Correct |
7 ms |
10964 KB |
Output is correct |
46 |
Correct |
7 ms |
10976 KB |
Output is correct |
47 |
Correct |
120 ms |
12732 KB |
Output is correct |
48 |
Correct |
119 ms |
12732 KB |
Output is correct |
49 |
Correct |
123 ms |
12624 KB |
Output is correct |
50 |
Correct |
121 ms |
12632 KB |
Output is correct |
51 |
Correct |
130 ms |
12736 KB |
Output is correct |
52 |
Correct |
122 ms |
12624 KB |
Output is correct |
53 |
Correct |
63 ms |
12628 KB |
Output is correct |
54 |
Correct |
160 ms |
13268 KB |
Output is correct |
55 |
Correct |
146 ms |
13252 KB |
Output is correct |
56 |
Correct |
217 ms |
13200 KB |
Output is correct |
57 |
Correct |
129 ms |
13812 KB |
Output is correct |
58 |
Correct |
169 ms |
13632 KB |
Output is correct |
59 |
Correct |
203 ms |
13372 KB |
Output is correct |
60 |
Correct |
182 ms |
13452 KB |
Output is correct |
61 |
Correct |
174 ms |
13540 KB |
Output is correct |
62 |
Correct |
187 ms |
12632 KB |
Output is correct |
63 |
Correct |
175 ms |
13136 KB |
Output is correct |
64 |
Correct |
177 ms |
12896 KB |
Output is correct |
65 |
Correct |
181 ms |
13064 KB |
Output is correct |
66 |
Correct |
7 ms |
10988 KB |
Output is correct |
67 |
Correct |
144 ms |
13324 KB |
Output is correct |
68 |
Correct |
170 ms |
13252 KB |
Output is correct |
69 |
Correct |
169 ms |
13252 KB |
Output is correct |
70 |
Correct |
148 ms |
13324 KB |
Output is correct |
71 |
Correct |
153 ms |
13300 KB |
Output is correct |
72 |
Correct |
167 ms |
13392 KB |
Output is correct |
73 |
Correct |
136 ms |
13776 KB |
Output is correct |
74 |
Correct |
127 ms |
13784 KB |
Output is correct |
75 |
Correct |
164 ms |
13588 KB |
Output is correct |
76 |
Correct |
139 ms |
13756 KB |
Output is correct |
77 |
Correct |
97 ms |
13760 KB |
Output is correct |
78 |
Correct |
5 ms |
10964 KB |
Output is correct |
79 |
Correct |
6 ms |
10968 KB |
Output is correct |
80 |
Correct |
8 ms |
11056 KB |
Output is correct |
81 |
Correct |
6 ms |
10964 KB |
Output is correct |
82 |
Correct |
7 ms |
10964 KB |
Output is correct |
83 |
Correct |
6 ms |
10964 KB |
Output is correct |
84 |
Correct |
6 ms |
10964 KB |
Output is correct |
85 |
Correct |
5 ms |
10964 KB |
Output is correct |
86 |
Correct |
45 ms |
11684 KB |
Output is correct |
87 |
Correct |
39 ms |
11692 KB |
Output is correct |
88 |
Correct |
42 ms |
11852 KB |
Output is correct |
89 |
Correct |
35 ms |
11920 KB |
Output is correct |
90 |
Correct |
33 ms |
11724 KB |
Output is correct |
91 |
Correct |
34 ms |
11876 KB |
Output is correct |
92 |
Correct |
39 ms |
11464 KB |
Output is correct |
93 |
Correct |
108 ms |
12632 KB |
Output is correct |
94 |
Correct |
112 ms |
12624 KB |
Output is correct |
95 |
Correct |
130 ms |
12620 KB |
Output is correct |
96 |
Correct |
114 ms |
12672 KB |
Output is correct |
97 |
Correct |
137 ms |
12624 KB |
Output is correct |
98 |
Correct |
113 ms |
12624 KB |
Output is correct |
99 |
Correct |
64 ms |
12636 KB |
Output is correct |
100 |
Correct |
178 ms |
13504 KB |
Output is correct |
101 |
Correct |
160 ms |
13384 KB |
Output is correct |
102 |
Correct |
190 ms |
13680 KB |
Output is correct |
103 |
Correct |
166 ms |
13112 KB |
Output is correct |
104 |
Correct |
177 ms |
12884 KB |
Output is correct |
105 |
Correct |
155 ms |
13032 KB |
Output is correct |
106 |
Correct |
146 ms |
13472 KB |
Output is correct |
107 |
Correct |
157 ms |
13484 KB |
Output is correct |
108 |
Correct |
182 ms |
13440 KB |
Output is correct |
109 |
Correct |
152 ms |
13504 KB |
Output is correct |
110 |
Correct |
175 ms |
13400 KB |
Output is correct |
111 |
Correct |
194 ms |
13396 KB |
Output is correct |