#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int S[100000],E[100000];
vi all;
vpii qq[100000];
int order[100000],ans[100000];
int parent[200000],out[200000],outnum[200000],mm[200000];
int B;
int rebuild(int b) {
int i;
if (mm[b] == 1e9) {
for (i = b*B; i < min((b+1)*B,(int) all.size()); i++) {
if (parent[i] >= i) out[i] = 1e9;
else if (parent[i] < b*B) out[i] = parent[i],outnum[i] = 1;
else out[i] = out[parent[i]],outnum[i] = outnum[parent[i]]+1;
}
}
return 0;
}
int main() {
int i;
int N,Q,s,e;
scanf("%d %d",&N,&Q);
for (i = 0; i < N; i++) scanf("%d %d",&S[i],&E[i]),all.pb(S[i]),all.pb(E[i]);
for (i = 0; i < Q; i++) {
scanf("%d %d",&s,&e),s--,e--;
qq[e].pb(mp(s,i));
}
sort(all.begin(),all.end());
all.resize(unique(all.begin(),all.end())-all.begin());
for (i = 0; i < N; i++) {
S[i] = lower_bound(all.begin(),all.end(),S[i])-all.begin();
E[i] = lower_bound(all.begin(),all.end(),E[i])-all.begin();
order[i] = i;
}
sort(order,order+N,[&](int a,int b) {
if (E[a] == E[b]) return S[a] < S[b];
else return E[a] < E[b];
});
int j,k;
B = sqrt(all.size());
for (i = 0; i < all.size(); i++) parent[i] = mm[i] = out[i] = 1e9;
for (i = 0; i < N; i++) {
int u = order[i];
if (S[u]/B == E[u]/B) {
for (j = S[u]; j <= E[u]; j++) parent[j] = min(parent[j],S[u]);
rebuild(S[u]/B);
}
else {
for (j = S[u]; j < (S[u]/B+1)*B; j++) parent[j] = min(parent[j],S[u]);
for (j = S[u]/B+1; j < E[u]/B; j++) {
if (j == S[u]/B+1) {
for (k = j*B; k < (j+1)*B; k++) parent[k] = min(parent[k],S[u]);
rebuild(j);
}
else mm[j] = min(mm[j],S[u]);
}
for (j = (E[u]/B)*B; j <= E[u]; j++) parent[j] = min(parent[j],S[u]);
rebuild(S[u]/B),rebuild(E[u]/B);
}
for (auto [s,j]: qq[u]) {
if (s == u) ans[j] = 0;
else if (E[s] > E[u]) ans[j] = -1;
else {
int v = S[u];
ans[j] = 1;
while (v > E[s]) {
if ((v-E[s] < 2*B+5) || (mm[v/B] != 1e9)) {
if (min(mm[v/B],parent[v]) >= v) {
ans[j] = -1;
break;
}
else v = min(mm[v/B],parent[v]),ans[j]++;
}
else {
ans[j] += outnum[v],v = out[v];
if (v == 1e9) {
ans[j] = -1;
break;
}
}
}
}
}
}
for (i = 0; i < Q; i++) {
if (ans[i] == -1) printf("impossible\n");
else printf("%d\n",ans[i]);
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:98:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (i = 0; i < all.size(); i++) parent[i] = mm[i] = out[i] = 1e9;
| ~~^~~~~~~~~~~~
events.cpp:78:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d %d",&N,&Q);
| ~~~~~^~~~~~~~~~~~~~~
events.cpp:79:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | for (i = 0; i < N; i++) scanf("%d %d",&S[i],&E[i]),all.pb(S[i]),all.pb(E[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
events.cpp:81:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d %d",&s,&e),s--,e--;
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
871 ms |
8012 KB |
Output is correct |
3 |
Correct |
774 ms |
8212 KB |
Output is correct |
4 |
Correct |
727 ms |
8656 KB |
Output is correct |
5 |
Correct |
664 ms |
8452 KB |
Output is correct |
6 |
Correct |
645 ms |
11712 KB |
Output is correct |
7 |
Correct |
575 ms |
11748 KB |
Output is correct |
8 |
Correct |
183 ms |
13372 KB |
Output is correct |
9 |
Correct |
154 ms |
13296 KB |
Output is correct |
10 |
Correct |
345 ms |
12480 KB |
Output is correct |
11 |
Correct |
273 ms |
13152 KB |
Output is correct |
12 |
Correct |
95 ms |
10772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2692 KB |
Output is correct |
7 |
Correct |
4 ms |
2644 KB |
Output is correct |
8 |
Correct |
4 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2692 KB |
Output is correct |
7 |
Correct |
4 ms |
2644 KB |
Output is correct |
8 |
Correct |
4 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2696 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
3 ms |
2692 KB |
Output is correct |
13 |
Correct |
4 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
4 ms |
2644 KB |
Output is correct |
16 |
Correct |
4 ms |
2688 KB |
Output is correct |
17 |
Correct |
3 ms |
2696 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
104 ms |
5824 KB |
Output is correct |
20 |
Correct |
161 ms |
5760 KB |
Output is correct |
21 |
Correct |
98 ms |
6344 KB |
Output is correct |
22 |
Correct |
47 ms |
6428 KB |
Output is correct |
23 |
Correct |
66 ms |
6352 KB |
Output is correct |
24 |
Correct |
38 ms |
6368 KB |
Output is correct |
25 |
Correct |
73 ms |
5672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2692 KB |
Output is correct |
7 |
Correct |
4 ms |
2644 KB |
Output is correct |
8 |
Correct |
4 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2696 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
3 ms |
2644 KB |
Output is correct |
13 |
Correct |
4 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2692 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
3 ms |
2644 KB |
Output is correct |
17 |
Correct |
3 ms |
2644 KB |
Output is correct |
18 |
Correct |
3 ms |
2692 KB |
Output is correct |
19 |
Correct |
152 ms |
7284 KB |
Output is correct |
20 |
Correct |
165 ms |
7372 KB |
Output is correct |
21 |
Correct |
221 ms |
8016 KB |
Output is correct |
22 |
Correct |
154 ms |
8588 KB |
Output is correct |
23 |
Correct |
519 ms |
9316 KB |
Output is correct |
24 |
Correct |
271 ms |
9712 KB |
Output is correct |
25 |
Correct |
32 ms |
5880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
927 ms |
8040 KB |
Output is correct |
2 |
Correct |
805 ms |
8272 KB |
Output is correct |
3 |
Correct |
713 ms |
8692 KB |
Output is correct |
4 |
Correct |
194 ms |
10224 KB |
Output is correct |
5 |
Correct |
329 ms |
12516 KB |
Output is correct |
6 |
Correct |
325 ms |
13836 KB |
Output is correct |
7 |
Correct |
345 ms |
13776 KB |
Output is correct |
8 |
Correct |
332 ms |
13948 KB |
Output is correct |
9 |
Correct |
505 ms |
9336 KB |
Output is correct |
10 |
Correct |
558 ms |
12684 KB |
Output is correct |
11 |
Correct |
408 ms |
12276 KB |
Output is correct |
12 |
Correct |
596 ms |
12564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
871 ms |
8012 KB |
Output is correct |
3 |
Correct |
774 ms |
8212 KB |
Output is correct |
4 |
Correct |
727 ms |
8656 KB |
Output is correct |
5 |
Correct |
664 ms |
8452 KB |
Output is correct |
6 |
Correct |
645 ms |
11712 KB |
Output is correct |
7 |
Correct |
575 ms |
11748 KB |
Output is correct |
8 |
Correct |
183 ms |
13372 KB |
Output is correct |
9 |
Correct |
154 ms |
13296 KB |
Output is correct |
10 |
Correct |
345 ms |
12480 KB |
Output is correct |
11 |
Correct |
273 ms |
13152 KB |
Output is correct |
12 |
Correct |
95 ms |
10772 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
3 ms |
2644 KB |
Output is correct |
17 |
Correct |
3 ms |
2644 KB |
Output is correct |
18 |
Correct |
3 ms |
2692 KB |
Output is correct |
19 |
Correct |
4 ms |
2644 KB |
Output is correct |
20 |
Correct |
4 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2696 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2688 KB |
Output is correct |
24 |
Correct |
3 ms |
2692 KB |
Output is correct |
25 |
Correct |
4 ms |
2644 KB |
Output is correct |
26 |
Correct |
3 ms |
2688 KB |
Output is correct |
27 |
Correct |
4 ms |
2644 KB |
Output is correct |
28 |
Correct |
4 ms |
2688 KB |
Output is correct |
29 |
Correct |
3 ms |
2696 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
104 ms |
5824 KB |
Output is correct |
32 |
Correct |
161 ms |
5760 KB |
Output is correct |
33 |
Correct |
98 ms |
6344 KB |
Output is correct |
34 |
Correct |
47 ms |
6428 KB |
Output is correct |
35 |
Correct |
66 ms |
6352 KB |
Output is correct |
36 |
Correct |
38 ms |
6368 KB |
Output is correct |
37 |
Correct |
73 ms |
5672 KB |
Output is correct |
38 |
Correct |
2 ms |
2644 KB |
Output is correct |
39 |
Correct |
2 ms |
2644 KB |
Output is correct |
40 |
Correct |
3 ms |
2644 KB |
Output is correct |
41 |
Correct |
4 ms |
2644 KB |
Output is correct |
42 |
Correct |
3 ms |
2692 KB |
Output is correct |
43 |
Correct |
3 ms |
2644 KB |
Output is correct |
44 |
Correct |
3 ms |
2644 KB |
Output is correct |
45 |
Correct |
3 ms |
2644 KB |
Output is correct |
46 |
Correct |
3 ms |
2692 KB |
Output is correct |
47 |
Correct |
152 ms |
7284 KB |
Output is correct |
48 |
Correct |
165 ms |
7372 KB |
Output is correct |
49 |
Correct |
221 ms |
8016 KB |
Output is correct |
50 |
Correct |
154 ms |
8588 KB |
Output is correct |
51 |
Correct |
519 ms |
9316 KB |
Output is correct |
52 |
Correct |
271 ms |
9712 KB |
Output is correct |
53 |
Correct |
32 ms |
5880 KB |
Output is correct |
54 |
Correct |
927 ms |
8040 KB |
Output is correct |
55 |
Correct |
805 ms |
8272 KB |
Output is correct |
56 |
Correct |
713 ms |
8692 KB |
Output is correct |
57 |
Correct |
194 ms |
10224 KB |
Output is correct |
58 |
Correct |
329 ms |
12516 KB |
Output is correct |
59 |
Correct |
325 ms |
13836 KB |
Output is correct |
60 |
Correct |
345 ms |
13776 KB |
Output is correct |
61 |
Correct |
332 ms |
13948 KB |
Output is correct |
62 |
Correct |
505 ms |
9336 KB |
Output is correct |
63 |
Correct |
558 ms |
12684 KB |
Output is correct |
64 |
Correct |
408 ms |
12276 KB |
Output is correct |
65 |
Correct |
596 ms |
12564 KB |
Output is correct |
66 |
Correct |
2 ms |
2644 KB |
Output is correct |
67 |
Correct |
844 ms |
11008 KB |
Output is correct |
68 |
Correct |
725 ms |
11236 KB |
Output is correct |
69 |
Correct |
715 ms |
11740 KB |
Output is correct |
70 |
Correct |
644 ms |
11468 KB |
Output is correct |
71 |
Correct |
601 ms |
11632 KB |
Output is correct |
72 |
Correct |
544 ms |
11776 KB |
Output is correct |
73 |
Correct |
197 ms |
13240 KB |
Output is correct |
74 |
Correct |
154 ms |
13380 KB |
Output is correct |
75 |
Correct |
341 ms |
12516 KB |
Output is correct |
76 |
Correct |
259 ms |
13136 KB |
Output is correct |
77 |
Correct |
128 ms |
10788 KB |
Output is correct |
78 |
Correct |
3 ms |
2644 KB |
Output is correct |
79 |
Correct |
3 ms |
2644 KB |
Output is correct |
80 |
Correct |
3 ms |
2644 KB |
Output is correct |
81 |
Correct |
3 ms |
2692 KB |
Output is correct |
82 |
Correct |
3 ms |
2680 KB |
Output is correct |
83 |
Correct |
3 ms |
2772 KB |
Output is correct |
84 |
Correct |
3 ms |
2644 KB |
Output is correct |
85 |
Correct |
2 ms |
2644 KB |
Output is correct |
86 |
Correct |
123 ms |
5836 KB |
Output is correct |
87 |
Correct |
166 ms |
5788 KB |
Output is correct |
88 |
Correct |
100 ms |
6332 KB |
Output is correct |
89 |
Correct |
44 ms |
6452 KB |
Output is correct |
90 |
Correct |
48 ms |
6388 KB |
Output is correct |
91 |
Correct |
39 ms |
6304 KB |
Output is correct |
92 |
Correct |
73 ms |
5684 KB |
Output is correct |
93 |
Correct |
181 ms |
8128 KB |
Output is correct |
94 |
Correct |
145 ms |
7424 KB |
Output is correct |
95 |
Correct |
191 ms |
8124 KB |
Output is correct |
96 |
Correct |
150 ms |
8788 KB |
Output is correct |
97 |
Correct |
545 ms |
9292 KB |
Output is correct |
98 |
Correct |
272 ms |
9664 KB |
Output is correct |
99 |
Correct |
33 ms |
5924 KB |
Output is correct |
100 |
Correct |
301 ms |
13788 KB |
Output is correct |
101 |
Correct |
363 ms |
13832 KB |
Output is correct |
102 |
Correct |
307 ms |
13868 KB |
Output is correct |
103 |
Correct |
521 ms |
12648 KB |
Output is correct |
104 |
Correct |
408 ms |
12164 KB |
Output is correct |
105 |
Correct |
547 ms |
12676 KB |
Output is correct |
106 |
Correct |
362 ms |
11296 KB |
Output is correct |
107 |
Correct |
434 ms |
11648 KB |
Output is correct |
108 |
Correct |
270 ms |
12336 KB |
Output is correct |
109 |
Correct |
227 ms |
12296 KB |
Output is correct |
110 |
Correct |
467 ms |
13760 KB |
Output is correct |
111 |
Correct |
437 ms |
13780 KB |
Output is correct |