#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ll int
//~ #define int ll
typedef pair<int32_t, int32_t> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 2000010
#define INF (ll)1e9
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
int N,Q;
int S[maxn],E[maxn],s,e;
vpii queries[maxn];
const int sqrtn = 1000;
vi A[maxn];
vi adj[maxn];
int par[maxn], twok[maxn][21];
bool vis[maxn];
struct node{
int s,e,m;
pi val = pi(INF,INF), lazy=pi(INF,INF);
node *l,*r;
node(int ss,int ee){
s = ss; e = ee; m = (s + e) / 2;
if (s != e){
l = new node(s,m); r = new node(m+1,e);
}
}
void prop(){
if (s == e || lazy == pi(INF,INF)) return;
l->lazy = min(l->lazy, lazy); r->lazy = min(r->lazy, lazy);
l->val = min(l->val, lazy); r->val = min(r->val, lazy);
lazy = pi(INF,INF);
}
void upd(int a,int b,pi c){
prop();
if (a <= s && e <= b){
val = min(val, c); lazy = min(lazy, c);
}else if (a > e || s > b) return;
else{
l->upd(a,b,c); r->upd(a,b,c);
val = min(l->val, r->val);
}
}
pi qry(int a,int b){
prop();
if (a <= s && e <= b) return val;
else if (a > e || s > b) return pi(INF,INF);
else return min(l->qry(a,b), r->qry(a,b));
}
}*root;
void dfs(int x, int p){
twok[x][0] = p;
vis[x] = 1;
FOR(i,1,20){
if (twok[x][i-1] == -1) break;
twok[x][i] = twok[twok[x][i-1]][i-1];
}
aFOR(i,adj[x]) dfs(i,x);
}
int ans[maxn];
void solve(){
cin >> N >> Q;
vi v;
FOR(i,1,N){
cin >> S[i] >> E[i];
v.pb(S[i]); v.pb(E[i]);
}
sort(all(v)); v.erase(unique(all(v)), v.end());
FOR(i,1,N){
S[i] = lbd(v, S[i]) - v.begin() + 1;
E[i] = lbd(v, E[i]) - v.begin() + 1;
}
FOR(i,1,N) A[E[i]].pb(i);
//~ cout << A[7].size();
//~ return;
//~ FOR(i,1,N){
//~ cout << S[i] << ' ' << E[i] << '\n';
//~ }
//~ return;
//~ FOR(i,1,Q){
//~ cin >> s >> e;
//~ if (s == e) ans[i] = 0;
//~ else if (S[e] <= E[s] && E[s] <= E[e]) ans[i] = 1;
//~ else queries[E[e]].pb(pii(pi(s,e), i));
//~ }
root = new node(1,2*N);
//~ root->upd(1,1,pi(1,1
FOR(i,1,N) root->upd(E[i], E[i], pi(S[i], i));
FOR(i,1,N){
par[i] = root->qry(S[i], E[i]).s;
if (par[i] == i) par[i] = -1;
else adj[par[i]].pb(i);
//~ cout << par[i] << ' ';
}
//~ cout << '\n';
FOR(i,1,N) if (!vis[i]) dfs(i,-1);
FOR(i,1,Q){
cin >> s >> e;
if (s == e) cout << 0;
else if (S[e] <= E[s] && E[s] <= E[e]) cout << 1;
else{
int x = e, ans = 0;
DEC(k,20,0) if (twok[x][k] != -1 && S[twok[x][k]] > E[s]){
x = twok[x][k]; ans += (1<<k);
}
if (par[x] == -1) cout << "impossible";
else{
x = par[x];
if (S[x] <= E[s] && E[s] <= E[x]) cout << ans + 2;
else cout << "impossible";
}
}
cout << '\n';
}
}
int32_t main(){
fast;
//~ cin >> TC;
//~ while (TC--) solve();
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
141160 KB |
Output is correct |
2 |
Correct |
787 ms |
186512 KB |
Output is correct |
3 |
Correct |
605 ms |
186576 KB |
Output is correct |
4 |
Correct |
887 ms |
186652 KB |
Output is correct |
5 |
Correct |
612 ms |
186064 KB |
Output is correct |
6 |
Correct |
495 ms |
185388 KB |
Output is correct |
7 |
Correct |
632 ms |
185288 KB |
Output is correct |
8 |
Correct |
296 ms |
180756 KB |
Output is correct |
9 |
Correct |
295 ms |
180904 KB |
Output is correct |
10 |
Correct |
534 ms |
185276 KB |
Output is correct |
11 |
Correct |
443 ms |
183716 KB |
Output is correct |
12 |
Correct |
186 ms |
180804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
141224 KB |
Output is correct |
2 |
Correct |
74 ms |
141256 KB |
Output is correct |
3 |
Correct |
77 ms |
141600 KB |
Output is correct |
4 |
Correct |
72 ms |
141644 KB |
Output is correct |
5 |
Correct |
82 ms |
141664 KB |
Output is correct |
6 |
Correct |
72 ms |
141576 KB |
Output is correct |
7 |
Correct |
72 ms |
141648 KB |
Output is correct |
8 |
Correct |
72 ms |
141596 KB |
Output is correct |
9 |
Correct |
71 ms |
141724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
141224 KB |
Output is correct |
2 |
Correct |
74 ms |
141256 KB |
Output is correct |
3 |
Correct |
77 ms |
141600 KB |
Output is correct |
4 |
Correct |
72 ms |
141644 KB |
Output is correct |
5 |
Correct |
82 ms |
141664 KB |
Output is correct |
6 |
Correct |
72 ms |
141576 KB |
Output is correct |
7 |
Correct |
72 ms |
141648 KB |
Output is correct |
8 |
Correct |
72 ms |
141596 KB |
Output is correct |
9 |
Correct |
71 ms |
141724 KB |
Output is correct |
10 |
Correct |
71 ms |
141248 KB |
Output is correct |
11 |
Correct |
71 ms |
141256 KB |
Output is correct |
12 |
Correct |
73 ms |
141640 KB |
Output is correct |
13 |
Correct |
72 ms |
141644 KB |
Output is correct |
14 |
Correct |
72 ms |
141644 KB |
Output is correct |
15 |
Correct |
72 ms |
141516 KB |
Output is correct |
16 |
Correct |
71 ms |
141528 KB |
Output is correct |
17 |
Correct |
70 ms |
141644 KB |
Output is correct |
18 |
Correct |
72 ms |
141616 KB |
Output is correct |
19 |
Correct |
107 ms |
143756 KB |
Output is correct |
20 |
Correct |
103 ms |
143916 KB |
Output is correct |
21 |
Correct |
116 ms |
144192 KB |
Output is correct |
22 |
Correct |
102 ms |
143872 KB |
Output is correct |
23 |
Correct |
98 ms |
143788 KB |
Output is correct |
24 |
Correct |
96 ms |
143948 KB |
Output is correct |
25 |
Correct |
103 ms |
143732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
141224 KB |
Output is correct |
2 |
Correct |
74 ms |
141256 KB |
Output is correct |
3 |
Correct |
77 ms |
141600 KB |
Output is correct |
4 |
Correct |
72 ms |
141644 KB |
Output is correct |
5 |
Correct |
82 ms |
141664 KB |
Output is correct |
6 |
Correct |
72 ms |
141576 KB |
Output is correct |
7 |
Correct |
72 ms |
141648 KB |
Output is correct |
8 |
Correct |
72 ms |
141596 KB |
Output is correct |
9 |
Correct |
71 ms |
141724 KB |
Output is correct |
10 |
Correct |
72 ms |
141248 KB |
Output is correct |
11 |
Correct |
74 ms |
141188 KB |
Output is correct |
12 |
Correct |
73 ms |
141660 KB |
Output is correct |
13 |
Correct |
74 ms |
141608 KB |
Output is correct |
14 |
Correct |
73 ms |
141700 KB |
Output is correct |
15 |
Correct |
73 ms |
141536 KB |
Output is correct |
16 |
Correct |
72 ms |
141552 KB |
Output is correct |
17 |
Correct |
75 ms |
141628 KB |
Output is correct |
18 |
Correct |
74 ms |
141628 KB |
Output is correct |
19 |
Correct |
506 ms |
187464 KB |
Output is correct |
20 |
Correct |
822 ms |
186100 KB |
Output is correct |
21 |
Correct |
278 ms |
179196 KB |
Output is correct |
22 |
Correct |
434 ms |
182824 KB |
Output is correct |
23 |
Correct |
317 ms |
180808 KB |
Output is correct |
24 |
Correct |
464 ms |
182872 KB |
Output is correct |
25 |
Correct |
143 ms |
177844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
790 ms |
186456 KB |
Output is correct |
2 |
Correct |
618 ms |
186460 KB |
Output is correct |
3 |
Correct |
853 ms |
186564 KB |
Output is correct |
4 |
Correct |
312 ms |
180804 KB |
Output is correct |
5 |
Correct |
548 ms |
185288 KB |
Output is correct |
6 |
Correct |
530 ms |
183620 KB |
Output is correct |
7 |
Correct |
561 ms |
183616 KB |
Output is correct |
8 |
Correct |
453 ms |
182196 KB |
Output is correct |
9 |
Correct |
315 ms |
180596 KB |
Output is correct |
10 |
Correct |
472 ms |
183232 KB |
Output is correct |
11 |
Correct |
384 ms |
182344 KB |
Output is correct |
12 |
Correct |
517 ms |
183312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
141160 KB |
Output is correct |
2 |
Correct |
787 ms |
186512 KB |
Output is correct |
3 |
Correct |
605 ms |
186576 KB |
Output is correct |
4 |
Correct |
887 ms |
186652 KB |
Output is correct |
5 |
Correct |
612 ms |
186064 KB |
Output is correct |
6 |
Correct |
495 ms |
185388 KB |
Output is correct |
7 |
Correct |
632 ms |
185288 KB |
Output is correct |
8 |
Correct |
296 ms |
180756 KB |
Output is correct |
9 |
Correct |
295 ms |
180904 KB |
Output is correct |
10 |
Correct |
534 ms |
185276 KB |
Output is correct |
11 |
Correct |
443 ms |
183716 KB |
Output is correct |
12 |
Correct |
186 ms |
180804 KB |
Output is correct |
13 |
Correct |
79 ms |
141224 KB |
Output is correct |
14 |
Correct |
74 ms |
141256 KB |
Output is correct |
15 |
Correct |
77 ms |
141600 KB |
Output is correct |
16 |
Correct |
72 ms |
141644 KB |
Output is correct |
17 |
Correct |
82 ms |
141664 KB |
Output is correct |
18 |
Correct |
72 ms |
141576 KB |
Output is correct |
19 |
Correct |
72 ms |
141648 KB |
Output is correct |
20 |
Correct |
72 ms |
141596 KB |
Output is correct |
21 |
Correct |
71 ms |
141724 KB |
Output is correct |
22 |
Correct |
71 ms |
141248 KB |
Output is correct |
23 |
Correct |
71 ms |
141256 KB |
Output is correct |
24 |
Correct |
73 ms |
141640 KB |
Output is correct |
25 |
Correct |
72 ms |
141644 KB |
Output is correct |
26 |
Correct |
72 ms |
141644 KB |
Output is correct |
27 |
Correct |
72 ms |
141516 KB |
Output is correct |
28 |
Correct |
71 ms |
141528 KB |
Output is correct |
29 |
Correct |
70 ms |
141644 KB |
Output is correct |
30 |
Correct |
72 ms |
141616 KB |
Output is correct |
31 |
Correct |
107 ms |
143756 KB |
Output is correct |
32 |
Correct |
103 ms |
143916 KB |
Output is correct |
33 |
Correct |
116 ms |
144192 KB |
Output is correct |
34 |
Correct |
102 ms |
143872 KB |
Output is correct |
35 |
Correct |
98 ms |
143788 KB |
Output is correct |
36 |
Correct |
96 ms |
143948 KB |
Output is correct |
37 |
Correct |
103 ms |
143732 KB |
Output is correct |
38 |
Correct |
72 ms |
141248 KB |
Output is correct |
39 |
Correct |
74 ms |
141188 KB |
Output is correct |
40 |
Correct |
73 ms |
141660 KB |
Output is correct |
41 |
Correct |
74 ms |
141608 KB |
Output is correct |
42 |
Correct |
73 ms |
141700 KB |
Output is correct |
43 |
Correct |
73 ms |
141536 KB |
Output is correct |
44 |
Correct |
72 ms |
141552 KB |
Output is correct |
45 |
Correct |
75 ms |
141628 KB |
Output is correct |
46 |
Correct |
74 ms |
141628 KB |
Output is correct |
47 |
Correct |
506 ms |
187464 KB |
Output is correct |
48 |
Correct |
822 ms |
186100 KB |
Output is correct |
49 |
Correct |
278 ms |
179196 KB |
Output is correct |
50 |
Correct |
434 ms |
182824 KB |
Output is correct |
51 |
Correct |
317 ms |
180808 KB |
Output is correct |
52 |
Correct |
464 ms |
182872 KB |
Output is correct |
53 |
Correct |
143 ms |
177844 KB |
Output is correct |
54 |
Correct |
790 ms |
186456 KB |
Output is correct |
55 |
Correct |
618 ms |
186460 KB |
Output is correct |
56 |
Correct |
853 ms |
186564 KB |
Output is correct |
57 |
Correct |
312 ms |
180804 KB |
Output is correct |
58 |
Correct |
548 ms |
185288 KB |
Output is correct |
59 |
Correct |
530 ms |
183620 KB |
Output is correct |
60 |
Correct |
561 ms |
183616 KB |
Output is correct |
61 |
Correct |
453 ms |
182196 KB |
Output is correct |
62 |
Correct |
315 ms |
180596 KB |
Output is correct |
63 |
Correct |
472 ms |
183232 KB |
Output is correct |
64 |
Correct |
384 ms |
182344 KB |
Output is correct |
65 |
Correct |
517 ms |
183312 KB |
Output is correct |
66 |
Correct |
72 ms |
141132 KB |
Output is correct |
67 |
Correct |
793 ms |
186584 KB |
Output is correct |
68 |
Correct |
611 ms |
186680 KB |
Output is correct |
69 |
Correct |
863 ms |
186556 KB |
Output is correct |
70 |
Correct |
605 ms |
186032 KB |
Output is correct |
71 |
Correct |
483 ms |
185412 KB |
Output is correct |
72 |
Correct |
682 ms |
185216 KB |
Output is correct |
73 |
Correct |
296 ms |
180732 KB |
Output is correct |
74 |
Correct |
294 ms |
180832 KB |
Output is correct |
75 |
Correct |
564 ms |
185764 KB |
Output is correct |
76 |
Correct |
448 ms |
183688 KB |
Output is correct |
77 |
Correct |
186 ms |
180696 KB |
Output is correct |
78 |
Correct |
74 ms |
141156 KB |
Output is correct |
79 |
Correct |
73 ms |
141704 KB |
Output is correct |
80 |
Correct |
73 ms |
141564 KB |
Output is correct |
81 |
Correct |
72 ms |
141656 KB |
Output is correct |
82 |
Correct |
72 ms |
141576 KB |
Output is correct |
83 |
Correct |
77 ms |
141668 KB |
Output is correct |
84 |
Correct |
72 ms |
141516 KB |
Output is correct |
85 |
Correct |
72 ms |
141548 KB |
Output is correct |
86 |
Correct |
107 ms |
143764 KB |
Output is correct |
87 |
Correct |
108 ms |
143920 KB |
Output is correct |
88 |
Correct |
115 ms |
144224 KB |
Output is correct |
89 |
Correct |
102 ms |
143964 KB |
Output is correct |
90 |
Correct |
100 ms |
143852 KB |
Output is correct |
91 |
Correct |
99 ms |
143844 KB |
Output is correct |
92 |
Correct |
101 ms |
143564 KB |
Output is correct |
93 |
Correct |
494 ms |
185860 KB |
Output is correct |
94 |
Correct |
814 ms |
185992 KB |
Output is correct |
95 |
Correct |
278 ms |
179140 KB |
Output is correct |
96 |
Correct |
423 ms |
182740 KB |
Output is correct |
97 |
Correct |
317 ms |
180520 KB |
Output is correct |
98 |
Correct |
455 ms |
182784 KB |
Output is correct |
99 |
Correct |
148 ms |
177876 KB |
Output is correct |
100 |
Correct |
530 ms |
183892 KB |
Output is correct |
101 |
Correct |
601 ms |
183508 KB |
Output is correct |
102 |
Correct |
461 ms |
182344 KB |
Output is correct |
103 |
Correct |
478 ms |
183136 KB |
Output is correct |
104 |
Correct |
389 ms |
182320 KB |
Output is correct |
105 |
Correct |
515 ms |
183136 KB |
Output is correct |
106 |
Correct |
435 ms |
184572 KB |
Output is correct |
107 |
Correct |
594 ms |
186952 KB |
Output is correct |
108 |
Correct |
303 ms |
179956 KB |
Output is correct |
109 |
Correct |
310 ms |
179980 KB |
Output is correct |
110 |
Correct |
371 ms |
181012 KB |
Output is correct |
111 |
Correct |
367 ms |
180972 KB |
Output is correct |