#include <bits/stdc++.h>
#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, a, b) for (int i = (a); i >= (b); --i)
#define fe(x, y) for (auto& x : y)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pw(x) (1LL << (x))
using namespace std;
mt19937_64 rng(228);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key
template <typename T>
bool umn(T& a, T b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
bool umx(T& a, T b) {
return a < b ? a = b, 1 : 0;
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;
const int N = 1e5 + 5;
int n, q;
pii a[N];
ve<int> order;
int to[N];
int d[N];
ve<pii> Q[N];
int ans[N];
//bool subtask1 = 1;
pii get(int v) {
if (to[v] == v) {
return {v, 0};
} else {
pii p = get(to[v]);
p.se += d[v];
to[v] = p.fi;
d[v] = p.se;
return {p.fi, p.se};
}
}
bool TL() {
return (double)clock() / CLOCKS_PER_SEC > 1.4;
}
ve<int> st;
bool have[N];
bool subtask = 1;
bool subtask1;
int go[N][20];
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios::sync_with_stdio(0);
cin.tie(0);
#endif
cin >> n >> q;
// subtask1 &= n > 5000 || q > 100;
ve<int> u;
fr (i, 1, n) {
cin >> a[i].fi >> a[i].se;
order.pb(i);
u.pb(a[i].se);
}
sort(all(u));
{
sort(all(order), [](int i, int j) {
return a[i].fi < a[j].fi || (a[i].fi == a[j].fi && a[i].se > a[j].se);
});
fe (i, order) {
if (sz(st) && a[st.back()].se >= a[i].se) {
to[i] = st.back();
continue;
}
st.pb(i);
have[i] = 1;
}
fr (i, 1, sz(st) - 1) {
assert(a[st[i]].fi > a[st[i - 1]].fi);
assert(a[st[i]].se > a[st[i - 1]].se);
}
{
int mx = -1e9;
int mxI = -1;
int ptr = 0;
fe (i, st) {
while (ptr < sz(st) && a[st[ptr]].fi <= a[i].se) {
if (umx(mx, a[st[ptr]].se)) {
mxI = st[ptr];
}
ptr++;
}
if (mx >= a[i].se) {
to[i] = mxI;
} else {
assert(0);
to[i] = -1;
}
}
}
subtask &= sz(st) == n;
}
if (subtask) {
fr (i, 1, n) {
go[i][0] = to[i];
}
fr (p, 1, 19) {
fr (i, 1, n) {
go[i][p] = go[go[i][p - 1]][p - 1];
}
}
fr (i, 1, q) {
int s, e;
cin >> s >> e;
if (s == e) {
cout << "0\n";
continue;
}
if (a[s].se > a[e].se) {
cout << "impossible\n";
continue;
}
int v = s;
int ans = 0;
rf (p, 19, 0) {
if (!go[v][p]) continue;
int nxt = go[v][p];
if (a[nxt].se < a[e].se) {
v = go[v][p];
ans += pw(p);
}
}
while (1) {
if (a[to[v]].se == a[v].se || a[to[v]].se > a[e].se || a[v].se >= a[e].fi) {
break;
}
v = to[v];
ans++;
}
if (a[v].se >= a[e].fi) {
ans++;
cout << ans << "\n";
} else {
cout << "impossible\n";
}
}
return 0;
}
// fr (i, 1, n) {
// int p1 = lower_bound(all(u), a[i].fi) - u.begin();
// int p2 = upper_bound(all(u), a[i].se) - u.begin() - 1;
// if (max(0, p2 - p1 + 1) > 2) {
// assert(0);
// }
// }
// fr (i, 1, n) {
// int cnt = 0;
// fr (j, 1, n) {
// if (i == j) continue;
//
// if (a[j].fi <= a[i].se && a[i].se <= a[j].se) {
// cnt++;
// }
// }
// assert(cnt <= 1);
// }
fr (i, 1, q) {
int s, e;
cin >> s >> e;
if (s == e) {
ans[i] = 0;
continue;
}
if (a[s].se >= a[e].fi && a[s].se <= a[e].se) {
ans[i] = 1;
continue;
}
if (a[s].se > a[e].se) {
ans[i] = -1;
continue;
}
Q[e].pb({s, i});
}
sort(all(order), [](int i, int j) {
return a[i].se < a[j].se || (a[i].se == a[j].se && a[i].fi < a[j].fi);
});
fr (i, 1, n) {
to[i] = i;
d[i] = 0;
}
// subtask1 = 1;
if (q > 100 && n > 5000) {
subtask1 = 1;
}
ve<pii> add, del;
fr (idx, 0, sz(order) - 1) {
int i = order[idx];
int l = 0;
int r = idx - 1;
while (l < r) {
int mid = l + ((r - l) >> 1);
int j = order[mid];
if (a[j].se < a[i].fi) {
l = mid + 1;
} else {
r = mid;
}
}
if (a[order[l]].se < a[i].fi) {
l++;
}
if (l <= idx - 1) {
if (subtask1) {
rf (ptr, idx - 1, l) {
int j = order[ptr];
to[j] = i;
d[j] = 1;
}
} else {
add.pb({l, idx});
del.pb({idx, idx});
}
}
// rf (ptr, idx - 1, 0) {
// int j = order[ptr];
// if (a[j].se < a[i].fi) {
// break;
// }
//
// subtask1 &= ptr == idx - 1;
// to[j] = i;
// d[j] = 1;
// }
if (!subtask1 && sz(Q[i])) {
sort(all(add));
sort(all(del));
//
int ptr1 = 0;
int ptr2 = 0;
set<int> s;
fr (cur, 0, idx) {
while (ptr1 < sz(add) && add[ptr1].fi <= cur) {
s.insert(add[ptr1].se);
ptr1++;
}
while (ptr2 < sz(del) && del[ptr2].fi <= cur) {
s.erase(del[ptr2].se);
ptr2++;
}
int i = order[cur];
if (sz(s)) {
d[i] = 1;
// assert(to[i] == order[*s.rbegin()]);
to[i] = order[*s.rbegin()];
}
}
add.clear();
del.clear();
}
fe (cur, Q[i]) {
if (subtask1) {
auto p = get(cur.fi);
if (p.fi == i) {
ans[cur.se] = p.se;
} else {
ans[cur.se] = -1;
}
} else {
int v = cur.fi;
int sum = 0;
while (to[v] != v) {
sum += d[v];
v = to[v];
}
if (v == i) {
ans[cur.se] = sum;
} else {
ans[cur.se] = -1;
}
}
}
}
fr (i, 1, q) {
if (ans[i] == -1) {
cout << "impossible\n";
} else {
cout << ans[i] << "\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
72 ms |
13688 KB |
Output is correct |
3 |
Correct |
79 ms |
13636 KB |
Output is correct |
4 |
Correct |
123 ms |
13632 KB |
Output is correct |
5 |
Correct |
91 ms |
10452 KB |
Output is correct |
6 |
Correct |
71 ms |
10112 KB |
Output is correct |
7 |
Correct |
74 ms |
9972 KB |
Output is correct |
8 |
Correct |
68 ms |
7564 KB |
Output is correct |
9 |
Correct |
70 ms |
14084 KB |
Output is correct |
10 |
Correct |
86 ms |
13964 KB |
Output is correct |
11 |
Correct |
77 ms |
7816 KB |
Output is correct |
12 |
Correct |
44 ms |
8116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 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 |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
454 ms |
5056 KB |
Output is correct |
20 |
Correct |
32 ms |
3716 KB |
Output is correct |
21 |
Correct |
297 ms |
4680 KB |
Output is correct |
22 |
Correct |
68 ms |
4440 KB |
Output is correct |
23 |
Correct |
80 ms |
4084 KB |
Output is correct |
24 |
Correct |
23 ms |
3908 KB |
Output is correct |
25 |
Correct |
27 ms |
3532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2768 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
51 ms |
13048 KB |
Output is correct |
20 |
Correct |
84 ms |
5312 KB |
Output is correct |
21 |
Correct |
91 ms |
5252 KB |
Output is correct |
22 |
Correct |
66 ms |
5520 KB |
Output is correct |
23 |
Correct |
54 ms |
12992 KB |
Output is correct |
24 |
Correct |
47 ms |
13056 KB |
Output is correct |
25 |
Correct |
28 ms |
7356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
13596 KB |
Output is correct |
2 |
Correct |
82 ms |
13760 KB |
Output is correct |
3 |
Correct |
111 ms |
13620 KB |
Output is correct |
4 |
Correct |
72 ms |
14084 KB |
Output is correct |
5 |
Correct |
87 ms |
14020 KB |
Output is correct |
6 |
Correct |
91 ms |
13804 KB |
Output is correct |
7 |
Correct |
96 ms |
13760 KB |
Output is correct |
8 |
Correct |
90 ms |
13940 KB |
Output is correct |
9 |
Correct |
48 ms |
13056 KB |
Output is correct |
10 |
Correct |
86 ms |
13440 KB |
Output is correct |
11 |
Correct |
70 ms |
13196 KB |
Output is correct |
12 |
Correct |
83 ms |
13644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
72 ms |
13688 KB |
Output is correct |
3 |
Correct |
79 ms |
13636 KB |
Output is correct |
4 |
Correct |
123 ms |
13632 KB |
Output is correct |
5 |
Correct |
91 ms |
10452 KB |
Output is correct |
6 |
Correct |
71 ms |
10112 KB |
Output is correct |
7 |
Correct |
74 ms |
9972 KB |
Output is correct |
8 |
Correct |
68 ms |
7564 KB |
Output is correct |
9 |
Correct |
70 ms |
14084 KB |
Output is correct |
10 |
Correct |
86 ms |
13964 KB |
Output is correct |
11 |
Correct |
77 ms |
7816 KB |
Output is correct |
12 |
Correct |
44 ms |
8116 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
3 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
454 ms |
5056 KB |
Output is correct |
32 |
Correct |
32 ms |
3716 KB |
Output is correct |
33 |
Correct |
297 ms |
4680 KB |
Output is correct |
34 |
Correct |
68 ms |
4440 KB |
Output is correct |
35 |
Correct |
80 ms |
4084 KB |
Output is correct |
36 |
Correct |
23 ms |
3908 KB |
Output is correct |
37 |
Correct |
27 ms |
3532 KB |
Output is correct |
38 |
Correct |
2 ms |
2768 KB |
Output is correct |
39 |
Correct |
1 ms |
2644 KB |
Output is correct |
40 |
Correct |
2 ms |
2772 KB |
Output is correct |
41 |
Correct |
2 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2644 KB |
Output is correct |
43 |
Correct |
2 ms |
2644 KB |
Output is correct |
44 |
Correct |
2 ms |
2644 KB |
Output is correct |
45 |
Correct |
2 ms |
2772 KB |
Output is correct |
46 |
Correct |
2 ms |
2644 KB |
Output is correct |
47 |
Correct |
51 ms |
13048 KB |
Output is correct |
48 |
Correct |
84 ms |
5312 KB |
Output is correct |
49 |
Correct |
91 ms |
5252 KB |
Output is correct |
50 |
Correct |
66 ms |
5520 KB |
Output is correct |
51 |
Correct |
54 ms |
12992 KB |
Output is correct |
52 |
Correct |
47 ms |
13056 KB |
Output is correct |
53 |
Correct |
28 ms |
7356 KB |
Output is correct |
54 |
Correct |
80 ms |
13596 KB |
Output is correct |
55 |
Correct |
82 ms |
13760 KB |
Output is correct |
56 |
Correct |
111 ms |
13620 KB |
Output is correct |
57 |
Correct |
72 ms |
14084 KB |
Output is correct |
58 |
Correct |
87 ms |
14020 KB |
Output is correct |
59 |
Correct |
91 ms |
13804 KB |
Output is correct |
60 |
Correct |
96 ms |
13760 KB |
Output is correct |
61 |
Correct |
90 ms |
13940 KB |
Output is correct |
62 |
Correct |
48 ms |
13056 KB |
Output is correct |
63 |
Correct |
86 ms |
13440 KB |
Output is correct |
64 |
Correct |
70 ms |
13196 KB |
Output is correct |
65 |
Correct |
83 ms |
13644 KB |
Output is correct |
66 |
Correct |
1 ms |
2644 KB |
Output is correct |
67 |
Correct |
94 ms |
16768 KB |
Output is correct |
68 |
Correct |
86 ms |
16696 KB |
Output is correct |
69 |
Correct |
132 ms |
16772 KB |
Output is correct |
70 |
Correct |
80 ms |
13564 KB |
Output is correct |
71 |
Correct |
88 ms |
13160 KB |
Output is correct |
72 |
Correct |
73 ms |
13084 KB |
Output is correct |
73 |
Correct |
63 ms |
10628 KB |
Output is correct |
74 |
Correct |
72 ms |
17264 KB |
Output is correct |
75 |
Correct |
103 ms |
17132 KB |
Output is correct |
76 |
Correct |
77 ms |
10932 KB |
Output is correct |
77 |
Correct |
46 ms |
10444 KB |
Output is correct |
78 |
Correct |
2 ms |
2644 KB |
Output is correct |
79 |
Correct |
2 ms |
2772 KB |
Output is correct |
80 |
Correct |
2 ms |
2800 KB |
Output is correct |
81 |
Correct |
2 ms |
2644 KB |
Output is correct |
82 |
Correct |
2 ms |
2644 KB |
Output is correct |
83 |
Correct |
2 ms |
2772 KB |
Output is correct |
84 |
Correct |
2 ms |
2772 KB |
Output is correct |
85 |
Correct |
2 ms |
2772 KB |
Output is correct |
86 |
Correct |
445 ms |
5948 KB |
Output is correct |
87 |
Correct |
29 ms |
4712 KB |
Output is correct |
88 |
Correct |
301 ms |
5644 KB |
Output is correct |
89 |
Correct |
76 ms |
5424 KB |
Output is correct |
90 |
Correct |
86 ms |
5120 KB |
Output is correct |
91 |
Correct |
26 ms |
4928 KB |
Output is correct |
92 |
Correct |
27 ms |
4504 KB |
Output is correct |
93 |
Correct |
54 ms |
15200 KB |
Output is correct |
94 |
Correct |
86 ms |
6456 KB |
Output is correct |
95 |
Correct |
92 ms |
7100 KB |
Output is correct |
96 |
Correct |
68 ms |
7532 KB |
Output is correct |
97 |
Correct |
52 ms |
15020 KB |
Output is correct |
98 |
Correct |
48 ms |
14908 KB |
Output is correct |
99 |
Correct |
31 ms |
8696 KB |
Output is correct |
100 |
Correct |
94 ms |
16864 KB |
Output is correct |
101 |
Correct |
87 ms |
16900 KB |
Output is correct |
102 |
Correct |
86 ms |
16968 KB |
Output is correct |
103 |
Correct |
95 ms |
16484 KB |
Output is correct |
104 |
Correct |
94 ms |
16316 KB |
Output is correct |
105 |
Correct |
96 ms |
16536 KB |
Output is correct |
106 |
Execution timed out |
1563 ms |
8528 KB |
Time limit exceeded |
107 |
Halted |
0 ms |
0 KB |
- |