#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll, ll>
#define f first
#define s second
#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
#define FOR(i,a,b) for (int i = a; i<b; ++i)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T,typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
const int maxn = 2e5+5;
vector<pii> to[maxn]; // {lpos,id}
vector<pii> ask[maxn]; // for each seg id, the left pos and the queryid
int ans[maxn];
int fa[18][maxn];
struct seg{
int l,r,i;
bool operator < (seg o) {
return r < o.r;
}
};
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
bug(1,2);
int n,q; cin>>n>>q;
vector<pii>v(n);
vector<ll>xpos;
REP(i,n) {
cin>>v[i].f>>v[i].s;
xpos.pb(v[i].f);
xpos.pb(v[i].s);
}
sort(ALL(xpos)); xpos.resize(unique(ALL(xpos)) - xpos.begin());
auto getx = [&](ll x) {return lower_bound(ALL(xpos), x)-xpos.begin();};
REP(i,n) {
v[i].f = getx(v[i].f);
v[i].s = getx(v[i].s);
to[v[i].s].pb({v[i].f,i});
}
REP(i,q) {
int s,e; cin>>s>>e; --s; --e;
if (s == e) {
ans[i] = 0; continue;
}
ask[e].pb({v[s].s,i});
}
vector<seg> now;
REP(r, maxn) {
sort(ALL(to[r]));
for (pii T : to[r]) {
int l= T.f, i =T.s;
while (SZ(now) && l <= now[SZ(now)-1].l) now.pop_back();
if (SZ(now) && now.back().r >= l) {
fa[0][i] = lower_bound(ALL(now), seg{l,l,i})->i;
}else{
fa[0][i] = i;
}
for (int j = 1; j<18; ++j) {
fa[j][i] = fa[j-1][fa[j-1][i]];
}
now.pb({l,r,i});
for (pii qq : ask[i]) {
if (qq.f > r) {
ans[qq.s] = -1; continue;
}
int steps =0;
int at = i;
for (int j = 17; j>=0; --j) {
if (v[fa[j][at]].f > qq.f) {
steps += (1<<j);
at = fa[j][at];
}
}
if (v[at].f > qq.f) {
at = fa[0][at]; ++steps;
}
if (v[at].f > qq.f) {
ans[qq.s] = -1;
}else{
ans[qq.s] = steps+1;
}
}
}
}
REP(i,q) {
if (ans[i] == -1) cout<<"impossible"<<endl;
else cout<<ans[i]<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9820 KB |
Output is correct |
2 |
Correct |
161 ms |
28292 KB |
Output is correct |
3 |
Correct |
142 ms |
29028 KB |
Output is correct |
4 |
Correct |
173 ms |
29120 KB |
Output is correct |
5 |
Correct |
126 ms |
28868 KB |
Output is correct |
6 |
Correct |
148 ms |
28772 KB |
Output is correct |
7 |
Correct |
153 ms |
28836 KB |
Output is correct |
8 |
Correct |
126 ms |
29508 KB |
Output is correct |
9 |
Correct |
120 ms |
29500 KB |
Output is correct |
10 |
Correct |
181 ms |
29796 KB |
Output is correct |
11 |
Correct |
188 ms |
28532 KB |
Output is correct |
12 |
Correct |
75 ms |
26720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
6 ms |
9940 KB |
Output is correct |
5 |
Correct |
6 ms |
9940 KB |
Output is correct |
6 |
Correct |
6 ms |
9940 KB |
Output is correct |
7 |
Correct |
7 ms |
9940 KB |
Output is correct |
8 |
Correct |
7 ms |
9940 KB |
Output is correct |
9 |
Correct |
7 ms |
9872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
6 ms |
9940 KB |
Output is correct |
5 |
Correct |
6 ms |
9940 KB |
Output is correct |
6 |
Correct |
6 ms |
9940 KB |
Output is correct |
7 |
Correct |
7 ms |
9940 KB |
Output is correct |
8 |
Correct |
7 ms |
9940 KB |
Output is correct |
9 |
Correct |
7 ms |
9872 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
7 ms |
9812 KB |
Output is correct |
12 |
Correct |
6 ms |
9940 KB |
Output is correct |
13 |
Correct |
6 ms |
9980 KB |
Output is correct |
14 |
Correct |
7 ms |
9940 KB |
Output is correct |
15 |
Correct |
7 ms |
9956 KB |
Output is correct |
16 |
Correct |
6 ms |
9940 KB |
Output is correct |
17 |
Correct |
6 ms |
9940 KB |
Output is correct |
18 |
Correct |
7 ms |
9940 KB |
Output is correct |
19 |
Correct |
34 ms |
13544 KB |
Output is correct |
20 |
Correct |
41 ms |
13644 KB |
Output is correct |
21 |
Correct |
50 ms |
14416 KB |
Output is correct |
22 |
Correct |
51 ms |
14348 KB |
Output is correct |
23 |
Correct |
34 ms |
14164 KB |
Output is correct |
24 |
Correct |
34 ms |
14476 KB |
Output is correct |
25 |
Correct |
34 ms |
13516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
6 ms |
9940 KB |
Output is correct |
5 |
Correct |
6 ms |
9940 KB |
Output is correct |
6 |
Correct |
6 ms |
9940 KB |
Output is correct |
7 |
Correct |
7 ms |
9940 KB |
Output is correct |
8 |
Correct |
7 ms |
9940 KB |
Output is correct |
9 |
Correct |
7 ms |
9872 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
6 ms |
9940 KB |
Output is correct |
13 |
Correct |
7 ms |
9940 KB |
Output is correct |
14 |
Correct |
6 ms |
9940 KB |
Output is correct |
15 |
Correct |
8 ms |
9940 KB |
Output is correct |
16 |
Correct |
7 ms |
9940 KB |
Output is correct |
17 |
Correct |
7 ms |
9940 KB |
Output is correct |
18 |
Correct |
6 ms |
9956 KB |
Output is correct |
19 |
Correct |
129 ms |
25792 KB |
Output is correct |
20 |
Correct |
102 ms |
25792 KB |
Output is correct |
21 |
Correct |
98 ms |
22612 KB |
Output is correct |
22 |
Correct |
109 ms |
24608 KB |
Output is correct |
23 |
Correct |
131 ms |
26024 KB |
Output is correct |
24 |
Correct |
122 ms |
25820 KB |
Output is correct |
25 |
Correct |
37 ms |
21576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
28340 KB |
Output is correct |
2 |
Correct |
140 ms |
28952 KB |
Output is correct |
3 |
Correct |
173 ms |
29188 KB |
Output is correct |
4 |
Correct |
145 ms |
29508 KB |
Output is correct |
5 |
Correct |
173 ms |
29744 KB |
Output is correct |
6 |
Correct |
169 ms |
29508 KB |
Output is correct |
7 |
Correct |
203 ms |
29464 KB |
Output is correct |
8 |
Correct |
172 ms |
29652 KB |
Output is correct |
9 |
Correct |
106 ms |
25796 KB |
Output is correct |
10 |
Correct |
145 ms |
28736 KB |
Output is correct |
11 |
Correct |
132 ms |
28564 KB |
Output is correct |
12 |
Correct |
171 ms |
28744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9820 KB |
Output is correct |
2 |
Correct |
161 ms |
28292 KB |
Output is correct |
3 |
Correct |
142 ms |
29028 KB |
Output is correct |
4 |
Correct |
173 ms |
29120 KB |
Output is correct |
5 |
Correct |
126 ms |
28868 KB |
Output is correct |
6 |
Correct |
148 ms |
28772 KB |
Output is correct |
7 |
Correct |
153 ms |
28836 KB |
Output is correct |
8 |
Correct |
126 ms |
29508 KB |
Output is correct |
9 |
Correct |
120 ms |
29500 KB |
Output is correct |
10 |
Correct |
181 ms |
29796 KB |
Output is correct |
11 |
Correct |
188 ms |
28532 KB |
Output is correct |
12 |
Correct |
75 ms |
26720 KB |
Output is correct |
13 |
Correct |
6 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
15 |
Correct |
6 ms |
9940 KB |
Output is correct |
16 |
Correct |
6 ms |
9940 KB |
Output is correct |
17 |
Correct |
6 ms |
9940 KB |
Output is correct |
18 |
Correct |
6 ms |
9940 KB |
Output is correct |
19 |
Correct |
7 ms |
9940 KB |
Output is correct |
20 |
Correct |
7 ms |
9940 KB |
Output is correct |
21 |
Correct |
7 ms |
9872 KB |
Output is correct |
22 |
Correct |
6 ms |
9812 KB |
Output is correct |
23 |
Correct |
7 ms |
9812 KB |
Output is correct |
24 |
Correct |
6 ms |
9940 KB |
Output is correct |
25 |
Correct |
6 ms |
9980 KB |
Output is correct |
26 |
Correct |
7 ms |
9940 KB |
Output is correct |
27 |
Correct |
7 ms |
9956 KB |
Output is correct |
28 |
Correct |
6 ms |
9940 KB |
Output is correct |
29 |
Correct |
6 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
9940 KB |
Output is correct |
31 |
Correct |
34 ms |
13544 KB |
Output is correct |
32 |
Correct |
41 ms |
13644 KB |
Output is correct |
33 |
Correct |
50 ms |
14416 KB |
Output is correct |
34 |
Correct |
51 ms |
14348 KB |
Output is correct |
35 |
Correct |
34 ms |
14164 KB |
Output is correct |
36 |
Correct |
34 ms |
14476 KB |
Output is correct |
37 |
Correct |
34 ms |
13516 KB |
Output is correct |
38 |
Correct |
6 ms |
9812 KB |
Output is correct |
39 |
Correct |
6 ms |
9812 KB |
Output is correct |
40 |
Correct |
6 ms |
9940 KB |
Output is correct |
41 |
Correct |
7 ms |
9940 KB |
Output is correct |
42 |
Correct |
6 ms |
9940 KB |
Output is correct |
43 |
Correct |
8 ms |
9940 KB |
Output is correct |
44 |
Correct |
7 ms |
9940 KB |
Output is correct |
45 |
Correct |
7 ms |
9940 KB |
Output is correct |
46 |
Correct |
6 ms |
9956 KB |
Output is correct |
47 |
Correct |
129 ms |
25792 KB |
Output is correct |
48 |
Correct |
102 ms |
25792 KB |
Output is correct |
49 |
Correct |
98 ms |
22612 KB |
Output is correct |
50 |
Correct |
109 ms |
24608 KB |
Output is correct |
51 |
Correct |
131 ms |
26024 KB |
Output is correct |
52 |
Correct |
122 ms |
25820 KB |
Output is correct |
53 |
Correct |
37 ms |
21576 KB |
Output is correct |
54 |
Correct |
140 ms |
28340 KB |
Output is correct |
55 |
Correct |
140 ms |
28952 KB |
Output is correct |
56 |
Correct |
173 ms |
29188 KB |
Output is correct |
57 |
Correct |
145 ms |
29508 KB |
Output is correct |
58 |
Correct |
173 ms |
29744 KB |
Output is correct |
59 |
Correct |
169 ms |
29508 KB |
Output is correct |
60 |
Correct |
203 ms |
29464 KB |
Output is correct |
61 |
Correct |
172 ms |
29652 KB |
Output is correct |
62 |
Correct |
106 ms |
25796 KB |
Output is correct |
63 |
Correct |
145 ms |
28736 KB |
Output is correct |
64 |
Correct |
132 ms |
28564 KB |
Output is correct |
65 |
Correct |
171 ms |
28744 KB |
Output is correct |
66 |
Correct |
6 ms |
9812 KB |
Output is correct |
67 |
Correct |
148 ms |
28460 KB |
Output is correct |
68 |
Correct |
157 ms |
29076 KB |
Output is correct |
69 |
Correct |
237 ms |
29204 KB |
Output is correct |
70 |
Correct |
154 ms |
28868 KB |
Output is correct |
71 |
Correct |
131 ms |
28852 KB |
Output is correct |
72 |
Correct |
147 ms |
28908 KB |
Output is correct |
73 |
Correct |
146 ms |
29500 KB |
Output is correct |
74 |
Correct |
154 ms |
29588 KB |
Output is correct |
75 |
Correct |
178 ms |
30028 KB |
Output is correct |
76 |
Correct |
163 ms |
28792 KB |
Output is correct |
77 |
Correct |
96 ms |
26656 KB |
Output is correct |
78 |
Correct |
6 ms |
9812 KB |
Output is correct |
79 |
Correct |
6 ms |
9892 KB |
Output is correct |
80 |
Correct |
6 ms |
9940 KB |
Output is correct |
81 |
Correct |
6 ms |
9952 KB |
Output is correct |
82 |
Correct |
6 ms |
9940 KB |
Output is correct |
83 |
Correct |
7 ms |
9940 KB |
Output is correct |
84 |
Correct |
6 ms |
9940 KB |
Output is correct |
85 |
Correct |
7 ms |
9940 KB |
Output is correct |
86 |
Correct |
35 ms |
13644 KB |
Output is correct |
87 |
Correct |
39 ms |
13668 KB |
Output is correct |
88 |
Correct |
44 ms |
14444 KB |
Output is correct |
89 |
Correct |
36 ms |
14348 KB |
Output is correct |
90 |
Correct |
34 ms |
14144 KB |
Output is correct |
91 |
Correct |
36 ms |
14272 KB |
Output is correct |
92 |
Correct |
36 ms |
13436 KB |
Output is correct |
93 |
Correct |
112 ms |
25792 KB |
Output is correct |
94 |
Correct |
131 ms |
25832 KB |
Output is correct |
95 |
Correct |
100 ms |
22596 KB |
Output is correct |
96 |
Correct |
115 ms |
24752 KB |
Output is correct |
97 |
Correct |
110 ms |
25904 KB |
Output is correct |
98 |
Correct |
109 ms |
25792 KB |
Output is correct |
99 |
Correct |
41 ms |
21584 KB |
Output is correct |
100 |
Correct |
173 ms |
29536 KB |
Output is correct |
101 |
Correct |
181 ms |
29548 KB |
Output is correct |
102 |
Correct |
183 ms |
29612 KB |
Output is correct |
103 |
Correct |
143 ms |
28744 KB |
Output is correct |
104 |
Correct |
143 ms |
28688 KB |
Output is correct |
105 |
Correct |
143 ms |
28740 KB |
Output is correct |
106 |
Correct |
186 ms |
28816 KB |
Output is correct |
107 |
Correct |
216 ms |
29648 KB |
Output is correct |
108 |
Correct |
140 ms |
26324 KB |
Output is correct |
109 |
Correct |
141 ms |
26332 KB |
Output is correct |
110 |
Correct |
162 ms |
26808 KB |
Output is correct |
111 |
Correct |
168 ms |
26736 KB |
Output is correct |