#include<bits/stdc++.h>
using namespace std;
int const N = 1e5+10;
struct interval {
int s, e;
interval() = default;
interval(int a, int b) : s(a), e(b) {}
bool operator< (const interval& o) const {
if (s == o.s) return e > o.e;
return s < o.s;
}
} IT[N];
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct node {
interval it;
int idx, meta, prior;
node *l, *r;
node() = default;
node(int i) : it(IT[i]), idx(i), meta(i), prior(rng()), l(0), r(0) {}
};
using pnode = node*;
pnode rt = nullptr;
void update(pnode u) {
if (!u) return;
u->meta = u->idx;
if (u->l && IT[u->l->meta].e > IT[u->meta].e) u->meta = u->l->meta;
if (u->r && IT[u->r->meta].e > IT[u->meta].e) u->meta = u->r->meta;
}
void split(pnode rt, pnode& l, pnode& r, int v) {
if (!rt) return (void)(l=r=nullptr);
if (v < rt->it.s) split(rt->l, l, rt->l, v), r = rt;
else split(rt->r, rt->r, r, v), l = rt;
update(rt);
}
void split(pnode rt, pnode& l, pnode& r, interval it) {
if (!rt) return (void)(l=r=nullptr);
if (it < rt->it) split(rt->l, l, rt->l, it), r = rt;
else split(rt->r, rt->r, r, it), l = rt;
update(rt);
}
void join(pnode& rt, pnode l, pnode r) {
if (!l || !r) rt = l?:r;
else if (l->prior > r->prior) join(l->r, l->r, r), rt = l;
else join(r->l, l, r->l), rt = r;
update(rt);
}
void add(int idx) {
if (rt == nullptr) rt = new node(idx);
else {
pnode nn = new node(idx);
pnode lv, rv;
split(rt, lv, rv, nn->it);
join(rt, lv, nn); join(rt, rt, rv);
}
}
int sparse[17][N];
void dfs(pnode r) {
if (!r) return;
dfs(r->l);
cerr << "[" << r->it.s << ", " << r->it.e << "] ";
dfs(r->r);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, q; cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> IT[i].s >> IT[i].e;
add(i);
}
//dfs(rt); cerr << '\n';
for (int i = 1; i <= n; ++i) {
pnode lv, rv; split(rt, lv, rv, IT[i].e);
//dfs(lv);
//cerr << lv->meta << ' ';
sparse[0][i] = lv->meta;
join(rt, lv, rv);
}
//cerr << '\n';
for (int b = 1; b < 17; ++b) {
for (int i = 1; i <= n; ++i) sparse[b][i] = sparse[b-1][sparse[b-1][i]];
}
for (int i = 0; i < q; ++i) {
int a, b; cin >> a >> b;
if (a == b) {
cout << 0 << '\n';
continue;
}
if (IT[a].e > IT[b].e) {
cout << "impossible\n";
}
else {
int st = 0;
for (int bb = 16; ~bb; --bb) {
int idx = sparse[bb][a];
if (IT[idx].e < IT[b].e) {
st += 1<<bb;
a = idx;
}
}
if (IT[a].e >= IT[b].s) {
++st;
cout << st << '\n';
}
else {
++st; a = sparse[0][a];
if (IT[a].e != IT[b].e) {
cout << "impossible\n";
}
else {
cout << (st+1) << '\n';
}
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
171 ms |
16084 KB |
Output is correct |
3 |
Correct |
206 ms |
16204 KB |
Output is correct |
4 |
Correct |
196 ms |
16080 KB |
Output is correct |
5 |
Correct |
214 ms |
16248 KB |
Output is correct |
6 |
Correct |
212 ms |
16212 KB |
Output is correct |
7 |
Correct |
213 ms |
16288 KB |
Output is correct |
8 |
Correct |
195 ms |
16724 KB |
Output is correct |
9 |
Correct |
166 ms |
16748 KB |
Output is correct |
10 |
Correct |
187 ms |
16400 KB |
Output is correct |
11 |
Correct |
209 ms |
16648 KB |
Output is correct |
12 |
Correct |
70 ms |
15948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
548 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
548 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
548 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
16092 KB |
Output is correct |
2 |
Correct |
177 ms |
16212 KB |
Output is correct |
3 |
Correct |
183 ms |
16184 KB |
Output is correct |
4 |
Correct |
170 ms |
16720 KB |
Output is correct |
5 |
Correct |
184 ms |
16468 KB |
Output is correct |
6 |
Correct |
189 ms |
16212 KB |
Output is correct |
7 |
Correct |
185 ms |
16208 KB |
Output is correct |
8 |
Correct |
176 ms |
16464 KB |
Output is correct |
9 |
Correct |
90 ms |
14420 KB |
Output is correct |
10 |
Correct |
177 ms |
16000 KB |
Output is correct |
11 |
Correct |
153 ms |
15820 KB |
Output is correct |
12 |
Correct |
194 ms |
15820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
171 ms |
16084 KB |
Output is correct |
3 |
Correct |
206 ms |
16204 KB |
Output is correct |
4 |
Correct |
196 ms |
16080 KB |
Output is correct |
5 |
Correct |
214 ms |
16248 KB |
Output is correct |
6 |
Correct |
212 ms |
16212 KB |
Output is correct |
7 |
Correct |
213 ms |
16288 KB |
Output is correct |
8 |
Correct |
195 ms |
16724 KB |
Output is correct |
9 |
Correct |
166 ms |
16748 KB |
Output is correct |
10 |
Correct |
187 ms |
16400 KB |
Output is correct |
11 |
Correct |
209 ms |
16648 KB |
Output is correct |
12 |
Correct |
70 ms |
15948 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
548 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |