#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include "bits_stdc++.h"
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, srb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
ll const INF = 1e13;
bool compare(pii a, pii b)
{
if (a.f.s==b.f.s) return a.f.f<b.f.f;
return a.f.s<b.f.s;
}
// RMQ
struct node {
int s, e, m;
pi val;
node * l, * r;
node(int S, int E) {
s = S, e = E, m = (s + e) / 2;
val = mp(INF, -1);
if (s != e) {
l = new node(s, m);
r = new node(m + 1, e);
}
}
void update(int X, pi V) {
if (s == e) {val = min(val, V);}
else {
if (X <= m) l -> update(X, V);
else r -> update(X, V);
val = min(val = l -> val, r -> val);
}
}
pi query(int S, int E) {
if (s == S && e == E) return val;
else if (E <= m) return l -> query(S, E);
else if (S >= m + 1) return r -> query(S, E);
else return min(l -> query(S, m), r -> query(m + 1, E));
}
}* root;
// Twok decomp
ll twok[100005][25];
int main()
{
ll n,q, l, r;
input2(n,q);
vector<pii> eve(n);
vector<pi> save(n);
vector<ll> dis;
for (ll i=0; i<n; ++i) {input2(save[i].f, save[i].s); eve[i] = mp(save[i], i); dis.pb(save[i].f), dis.pb(save[i].s);}
discretize(dis);
sort(eve.begin(), eve.end(), compare);
root = new node(0, dis.size()-1);
for (ll i=0; i<n; ++i)
{
ll l = lb(dis.begin(), dis.end(), eve[i].f.f)-dis.begin(), r = lb(dis.begin(), dis.end(), eve[i].f.s)-dis.begin(), ind = eve[i].s;
root->update(r, mp(l, ind));
pi nxt = root->query(l, r);
if (nxt.f>=l) twok[ind][0] = -1;
else twok[ind][0] = nxt.s;
}
for (ll k=1; k<20; ++k)
{
for (ll x = 0; x<n; ++x)
{
if (twok[x][k-1]==-1) twok[x][k]=-1;
else twok[x][k] = twok[twok[x][k-1]][k-1];
}
}
while (q--)
{
input2(l, r);
l--; r--;
ll ans = 0;
if (l==r) {print(0LL, '\n'); continue;}
if (save[l].s>save[r].s){ cout << "impossible" << '\n'; continue;}
if (save[r].f<=save[l].s) {print(1LL, '\n'); continue;}
for (ll i=19; i>=0; --i)
{
if (twok[r][i]==-1) continue;
if (save[twok[r][i]].f<= save[l].s) continue;
r = twok[r][i];
ans += (1LL<<i);
}
if (twok[r][0]==-1) {cout << "impossible" << endl; continue;}
print(ans+2, '\n');
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:12:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | #define input2(x, y) scanf("%lld%lld", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
events.cpp:71:2: note: in expansion of macro 'input2'
71 | input2(n,q);
| ^~~~~~
events.cpp:12:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | #define input2(x, y) scanf("%lld%lld", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
events.cpp:75:26: note: in expansion of macro 'input2'
75 | for (ll i=0; i<n; ++i) {input2(save[i].f, save[i].s); eve[i] = mp(save[i], i); dis.pb(save[i].f), dis.pb(save[i].s);}
| ^~~~~~
events.cpp:12:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | #define input2(x, y) scanf("%lld%lld", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
events.cpp:97:3: note: in expansion of macro 'input2'
97 | input2(l, r);
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
115 ms |
38556 KB |
Output is correct |
3 |
Correct |
135 ms |
41536 KB |
Output is correct |
4 |
Correct |
153 ms |
41928 KB |
Output is correct |
5 |
Correct |
139 ms |
42936 KB |
Output is correct |
6 |
Correct |
142 ms |
43972 KB |
Output is correct |
7 |
Correct |
150 ms |
44564 KB |
Output is correct |
8 |
Correct |
228 ms |
54732 KB |
Output is correct |
9 |
Correct |
222 ms |
54720 KB |
Output is correct |
10 |
Correct |
176 ms |
41920 KB |
Output is correct |
11 |
Correct |
172 ms |
47076 KB |
Output is correct |
12 |
Correct |
193 ms |
41408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2760 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2764 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2760 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2764 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
33 ms |
5200 KB |
Output is correct |
20 |
Correct |
31 ms |
4952 KB |
Output is correct |
21 |
Correct |
34 ms |
5204 KB |
Output is correct |
22 |
Correct |
49 ms |
5304 KB |
Output is correct |
23 |
Correct |
28 ms |
5712 KB |
Output is correct |
24 |
Correct |
31 ms |
5724 KB |
Output is correct |
25 |
Correct |
29 ms |
5520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2760 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2764 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Correct |
1 ms |
2908 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
86 ms |
39868 KB |
Output is correct |
20 |
Correct |
87 ms |
38956 KB |
Output is correct |
21 |
Correct |
90 ms |
39840 KB |
Output is correct |
22 |
Correct |
90 ms |
44564 KB |
Output is correct |
23 |
Correct |
97 ms |
52420 KB |
Output is correct |
24 |
Correct |
99 ms |
52416 KB |
Output is correct |
25 |
Correct |
33 ms |
26816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
38504 KB |
Output is correct |
2 |
Correct |
120 ms |
41744 KB |
Output is correct |
3 |
Correct |
151 ms |
41664 KB |
Output is correct |
4 |
Correct |
224 ms |
54548 KB |
Output is correct |
5 |
Correct |
180 ms |
42060 KB |
Output is correct |
6 |
Correct |
151 ms |
54212 KB |
Output is correct |
7 |
Correct |
152 ms |
54208 KB |
Output is correct |
8 |
Correct |
186 ms |
54392 KB |
Output is correct |
9 |
Correct |
97 ms |
52420 KB |
Output is correct |
10 |
Correct |
131 ms |
53952 KB |
Output is correct |
11 |
Correct |
130 ms |
54048 KB |
Output is correct |
12 |
Correct |
137 ms |
53956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
115 ms |
38556 KB |
Output is correct |
3 |
Correct |
135 ms |
41536 KB |
Output is correct |
4 |
Correct |
153 ms |
41928 KB |
Output is correct |
5 |
Correct |
139 ms |
42936 KB |
Output is correct |
6 |
Correct |
142 ms |
43972 KB |
Output is correct |
7 |
Correct |
150 ms |
44564 KB |
Output is correct |
8 |
Correct |
228 ms |
54732 KB |
Output is correct |
9 |
Correct |
222 ms |
54720 KB |
Output is correct |
10 |
Correct |
176 ms |
41920 KB |
Output is correct |
11 |
Correct |
172 ms |
47076 KB |
Output is correct |
12 |
Correct |
193 ms |
41408 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2760 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2764 KB |
Output is correct |
19 |
Correct |
1 ms |
2908 KB |
Output is correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
2648 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2648 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
33 ms |
5200 KB |
Output is correct |
32 |
Correct |
31 ms |
4952 KB |
Output is correct |
33 |
Correct |
34 ms |
5204 KB |
Output is correct |
34 |
Correct |
49 ms |
5304 KB |
Output is correct |
35 |
Correct |
28 ms |
5712 KB |
Output is correct |
36 |
Correct |
31 ms |
5724 KB |
Output is correct |
37 |
Correct |
29 ms |
5520 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
2652 KB |
Output is correct |
41 |
Correct |
1 ms |
2652 KB |
Output is correct |
42 |
Correct |
2 ms |
2652 KB |
Output is correct |
43 |
Correct |
1 ms |
2652 KB |
Output is correct |
44 |
Correct |
1 ms |
2908 KB |
Output is correct |
45 |
Correct |
1 ms |
2908 KB |
Output is correct |
46 |
Correct |
1 ms |
2652 KB |
Output is correct |
47 |
Correct |
86 ms |
39868 KB |
Output is correct |
48 |
Correct |
87 ms |
38956 KB |
Output is correct |
49 |
Correct |
90 ms |
39840 KB |
Output is correct |
50 |
Correct |
90 ms |
44564 KB |
Output is correct |
51 |
Correct |
97 ms |
52420 KB |
Output is correct |
52 |
Correct |
99 ms |
52416 KB |
Output is correct |
53 |
Correct |
33 ms |
26816 KB |
Output is correct |
54 |
Correct |
110 ms |
38504 KB |
Output is correct |
55 |
Correct |
120 ms |
41744 KB |
Output is correct |
56 |
Correct |
151 ms |
41664 KB |
Output is correct |
57 |
Correct |
224 ms |
54548 KB |
Output is correct |
58 |
Correct |
180 ms |
42060 KB |
Output is correct |
59 |
Correct |
151 ms |
54212 KB |
Output is correct |
60 |
Correct |
152 ms |
54208 KB |
Output is correct |
61 |
Correct |
186 ms |
54392 KB |
Output is correct |
62 |
Correct |
97 ms |
52420 KB |
Output is correct |
63 |
Correct |
131 ms |
53952 KB |
Output is correct |
64 |
Correct |
130 ms |
54048 KB |
Output is correct |
65 |
Correct |
137 ms |
53956 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
112 ms |
41704 KB |
Output is correct |
68 |
Correct |
116 ms |
41664 KB |
Output is correct |
69 |
Correct |
142 ms |
41668 KB |
Output is correct |
70 |
Correct |
139 ms |
42944 KB |
Output is correct |
71 |
Correct |
138 ms |
43968 KB |
Output is correct |
72 |
Correct |
148 ms |
44460 KB |
Output is correct |
73 |
Correct |
231 ms |
54688 KB |
Output is correct |
74 |
Correct |
231 ms |
54724 KB |
Output is correct |
75 |
Correct |
161 ms |
42132 KB |
Output is correct |
76 |
Correct |
178 ms |
46968 KB |
Output is correct |
77 |
Correct |
197 ms |
41868 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
1 ms |
2652 KB |
Output is correct |
80 |
Correct |
1 ms |
2652 KB |
Output is correct |
81 |
Correct |
1 ms |
2648 KB |
Output is correct |
82 |
Correct |
1 ms |
2648 KB |
Output is correct |
83 |
Correct |
2 ms |
2908 KB |
Output is correct |
84 |
Correct |
2 ms |
2756 KB |
Output is correct |
85 |
Correct |
1 ms |
2652 KB |
Output is correct |
86 |
Correct |
35 ms |
5232 KB |
Output is correct |
87 |
Correct |
30 ms |
5044 KB |
Output is correct |
88 |
Correct |
35 ms |
5204 KB |
Output is correct |
89 |
Correct |
50 ms |
5200 KB |
Output is correct |
90 |
Correct |
27 ms |
5712 KB |
Output is correct |
91 |
Correct |
30 ms |
5712 KB |
Output is correct |
92 |
Correct |
33 ms |
5600 KB |
Output is correct |
93 |
Correct |
84 ms |
39808 KB |
Output is correct |
94 |
Correct |
82 ms |
39104 KB |
Output is correct |
95 |
Correct |
91 ms |
39872 KB |
Output is correct |
96 |
Correct |
90 ms |
44372 KB |
Output is correct |
97 |
Correct |
100 ms |
52352 KB |
Output is correct |
98 |
Correct |
100 ms |
52392 KB |
Output is correct |
99 |
Correct |
33 ms |
26772 KB |
Output is correct |
100 |
Correct |
145 ms |
54212 KB |
Output is correct |
101 |
Correct |
163 ms |
54208 KB |
Output is correct |
102 |
Correct |
154 ms |
54464 KB |
Output is correct |
103 |
Correct |
160 ms |
53996 KB |
Output is correct |
104 |
Correct |
139 ms |
53704 KB |
Output is correct |
105 |
Correct |
148 ms |
53928 KB |
Output is correct |
106 |
Correct |
136 ms |
37812 KB |
Output is correct |
107 |
Correct |
138 ms |
40900 KB |
Output is correct |
108 |
Correct |
138 ms |
41616 KB |
Output is correct |
109 |
Correct |
142 ms |
41844 KB |
Output is correct |
110 |
Correct |
166 ms |
54208 KB |
Output is correct |
111 |
Correct |
167 ms |
54220 KB |
Output is correct |