#include <bits/stdc++.h>
using namespace std;
struct node {
int mn, mx, lazymn, lazymx;
bool hv;
};
vector<node> tr[20];
vector<pair<int, int>> B;
void build(int lay, int p, int l, int r) {
if (l == r) {
tr[lay][p].mn = B[l].first;
tr[lay][p].mx = B[l].second;
tr[lay][p].lazymn = tr[lay][p].lazymx = -1;
tr[lay][p].hv = false;
return;
}
tr[lay][p].hv = true;
int mid = (l + r) / 2;
build(lay, p * 2, l, mid);
build(lay, p * 2 + 1, mid + 1, r);
tr[lay][p].mn = min(tr[lay][p * 2].mn, tr[lay][p * 2 + 1].mn);
tr[lay][p].mx = max(tr[lay][p * 2].mx, tr[lay][p * 2 + 1].mx);
tr[lay][p].lazymn = tr[lay][p].lazymx = -1;
return;
}
void push(int lay, int p) {
if (tr[lay][p].lazymn != -1) {
int me = tr[lay][p].lazymn;
if (tr[lay][p].hv) {
if (tr[lay][p * 2].lazymn == -1) tr[lay][p * 2].lazymn = me;
else tr[lay][p * 2].lazymn = min(tr[lay][p * 2].lazymn, me);
tr[lay][p * 2].mn = min(tr[lay][p * 2].mn, me);
if (tr[lay][p * 2 + 1].lazymn == -1) tr[lay][p * 2 + 1].lazymn = me;
else tr[lay][p * 2 + 1].lazymn = min(tr[lay][p * 2 + 1].lazymn, me);
tr[lay][p * 2 + 1].mn = min(tr[lay][p * 2 + 1].mn, me);
}
tr[lay][p].lazymn = -1;
}
if (tr[lay][p].lazymx != -1) {
int me = tr[lay][p].lazymx;
if (tr[lay][p].hv) {
if (tr[lay][p * 2].lazymx == -1) tr[lay][p * 2].lazymx = me;
else tr[lay][p * 2].lazymx = max(tr[lay][p * 2].lazymx, me);
tr[lay][p * 2].mx = max(tr[lay][p * 2].mx, me);
if (tr[lay][p * 2 + 1].lazymx == -1) tr[lay][p * 2 + 1].lazymx = me;
else tr[lay][p * 2 + 1].lazymx = max(tr[lay][p * 2 + 1].lazymx, me);
tr[lay][p * 2 + 1].mx = max(tr[lay][p * 2 + 1].mx, me);
}
tr[lay][p].lazymx = -1;
}
return;
}
void up(int lay, int p, int l, int r, int x, int y, int num, int mode) {
// cout << "up lay=" << lay << " l=" << l << " r=" << r << " x=" << x << " y=" << y << " num=" << num << " mode=" << mode << "\n";
if (x > r || y < l) return;
push(lay, p);
if (x <= l && y >= r) {
if (mode == -1) {
if (tr[lay][p].lazymn == -1) tr[lay][p].lazymn = num;
else tr[lay][p].lazymn = min(tr[lay][p].lazymn, num);
tr[lay][p].mn = min(tr[lay][p].mn, num);
}
else {
if (tr[lay][p].lazymx == -1) tr[lay][p].lazymx = num;
else tr[lay][p].lazymx = max(tr[lay][p].lazymx, num);
tr[lay][p].mx = max(tr[lay][p].mx, num);
}
// cout << "l=" << l << " r=" << r << " mn=" << tr[lay][p].mn << " mx=" << tr[lay][p].mx << "\n";
return;
}
int mid = (l + r) / 2;
up(lay, p * 2, l, mid, x, y, num, mode);
up(lay, p * 2 + 1, mid + 1, r, x, y, num, mode);
tr[lay][p].mn = min(tr[lay][p * 2].mn, tr[lay][p * 2 + 1].mn);
tr[lay][p].mx = max(tr[lay][p * 2].mx, tr[lay][p * 2 + 1].mx);
push(lay, p);
return;
}
pair<int, int> qu(int lay, int p, int l, int r, int x, int y) {
if (x > r || y < l) return {1e9, -1};
push(lay, p);
if (x <= l && y >= r) {
return {tr[lay][p].mn, tr[lay][p].mx};
}
int mid = (l + r) / 2;
auto pr1 = qu(lay, p * 2, l, mid, x, y);
auto pr2 = qu(lay, p * 2 + 1, mid + 1, r, x, y);
pair<int, int> ans;
ans.first = min(pr1.first, pr2.first);
ans.second = max(pr1.second, pr2.second);
return ans;
}
int main() {
ios::sync_with_stdio(false);
int n, m, k, q, r1, r2;
cin >> n >> k >> m;
vector<pair<int, int>> A(m + 1);
for (int i = 1; i <= m; i++) {
cin >> r1 >> r2;
A[i] = {r1, r2};
}
cin >> q;
vector<pair<int, int>> Q(q + 1);
for (int i = 1; i <= q; i++) {
cin >> r1 >> r2;
Q[i] = {r1, r2};
}
int two = 2, ct = 1;
while (ct < n) {
ct *= 2;
two++;
}
for (int i = 0; i <= two; i++) {
tr[i].resize(4 * n + 10);
}
B.reserve(n + 1);
for (int i = 1; i <= n; i++) {
B[i] = {i, i};
}
build(0, 1, 1, n);
for (int i = 1; i <= m; i++) {
int sl, sr;
if (A[i].first < A[i].second) {
sl = A[i].first;
sr = min(A[i].first + k - 1, A[i].second);
up(0, 1, 1, n, sl, sr, A[i].second, 1);
}
else {
sl = max(A[i].first - k + 1, A[i].second);
sr = A[i].first;
up(0, 1, 1, n, sl, sr, A[i].second, -1);
}
}
for (int i = 1; i <= two; i++) {
int last = i - 1;
for (int j = 1; j <= n; j++) {
int l, r;
auto pr = qu(last, 1, 1, n, j, j);
l = pr.first;
r = pr.second;
pr = qu(last, 1, 1, n, l, r);
B[j] = pr;
}
build(i, 1, 1, n);
}
for (int i = 1; i <= q; i++) {
int ans = 0;
int l, r;
l = r = Q[i].first;
auto pr = qu(two, 1, 1, n, l, r);
if (Q[i].second < pr.first || Q[i].second > pr.second) {
ans = -1;
}
else {
for (int j = two - 1; j >= 0; j--) {
pr = qu(j, 1, 1, n, l, r);
if (Q[i].second < pr.first || Q[i].second > pr.second) {
ans += (1 << j);
l = pr.first;
r = pr.second;
}
}
ans++;
}
cout << ans << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
15 ms |
2636 KB |
Output is correct |
14 |
Correct |
15 ms |
2648 KB |
Output is correct |
15 |
Correct |
10 ms |
2504 KB |
Output is correct |
16 |
Correct |
10 ms |
2644 KB |
Output is correct |
17 |
Correct |
14 ms |
2636 KB |
Output is correct |
18 |
Correct |
12 ms |
2644 KB |
Output is correct |
19 |
Correct |
14 ms |
2516 KB |
Output is correct |
20 |
Correct |
11 ms |
2660 KB |
Output is correct |
21 |
Correct |
7 ms |
2516 KB |
Output is correct |
22 |
Correct |
16 ms |
2644 KB |
Output is correct |
23 |
Correct |
15 ms |
2660 KB |
Output is correct |
24 |
Correct |
15 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1080 ms |
158360 KB |
Output is correct |
2 |
Correct |
993 ms |
158344 KB |
Output is correct |
3 |
Correct |
1093 ms |
158484 KB |
Output is correct |
4 |
Correct |
1000 ms |
158312 KB |
Output is correct |
5 |
Correct |
828 ms |
159284 KB |
Output is correct |
6 |
Correct |
1018 ms |
159204 KB |
Output is correct |
7 |
Correct |
641 ms |
159288 KB |
Output is correct |
8 |
Correct |
623 ms |
156896 KB |
Output is correct |
9 |
Correct |
645 ms |
158508 KB |
Output is correct |
10 |
Correct |
1122 ms |
159288 KB |
Output is correct |
11 |
Correct |
981 ms |
161612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1317 ms |
158868 KB |
Output is correct |
2 |
Correct |
1154 ms |
162764 KB |
Output is correct |
3 |
Correct |
1611 ms |
160720 KB |
Output is correct |
4 |
Correct |
869 ms |
162276 KB |
Output is correct |
5 |
Correct |
997 ms |
161072 KB |
Output is correct |
6 |
Correct |
980 ms |
162484 KB |
Output is correct |
7 |
Correct |
1477 ms |
162600 KB |
Output is correct |
8 |
Correct |
1267 ms |
162648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1060 ms |
159812 KB |
Output is correct |
2 |
Correct |
1586 ms |
159156 KB |
Output is correct |
3 |
Correct |
1525 ms |
158772 KB |
Output is correct |
4 |
Correct |
1490 ms |
158532 KB |
Output is correct |
5 |
Correct |
1269 ms |
158284 KB |
Output is correct |
6 |
Correct |
1384 ms |
158340 KB |
Output is correct |
7 |
Correct |
1115 ms |
158904 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
19 ms |
2644 KB |
Output is correct |
10 |
Correct |
1059 ms |
161608 KB |
Output is correct |
11 |
Correct |
1283 ms |
162468 KB |
Output is correct |
12 |
Correct |
1279 ms |
160864 KB |
Output is correct |
13 |
Correct |
1311 ms |
162456 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
15 ms |
2632 KB |
Output is correct |
16 |
Correct |
1172 ms |
161624 KB |
Output is correct |
17 |
Correct |
1622 ms |
162764 KB |
Output is correct |
18 |
Correct |
1600 ms |
162632 KB |
Output is correct |
19 |
Correct |
1480 ms |
162604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
15 ms |
2636 KB |
Output is correct |
14 |
Correct |
15 ms |
2648 KB |
Output is correct |
15 |
Correct |
10 ms |
2504 KB |
Output is correct |
16 |
Correct |
10 ms |
2644 KB |
Output is correct |
17 |
Correct |
14 ms |
2636 KB |
Output is correct |
18 |
Correct |
12 ms |
2644 KB |
Output is correct |
19 |
Correct |
14 ms |
2516 KB |
Output is correct |
20 |
Correct |
11 ms |
2660 KB |
Output is correct |
21 |
Correct |
7 ms |
2516 KB |
Output is correct |
22 |
Correct |
16 ms |
2644 KB |
Output is correct |
23 |
Correct |
15 ms |
2660 KB |
Output is correct |
24 |
Correct |
15 ms |
2644 KB |
Output is correct |
25 |
Correct |
1080 ms |
158360 KB |
Output is correct |
26 |
Correct |
993 ms |
158344 KB |
Output is correct |
27 |
Correct |
1093 ms |
158484 KB |
Output is correct |
28 |
Correct |
1000 ms |
158312 KB |
Output is correct |
29 |
Correct |
828 ms |
159284 KB |
Output is correct |
30 |
Correct |
1018 ms |
159204 KB |
Output is correct |
31 |
Correct |
641 ms |
159288 KB |
Output is correct |
32 |
Correct |
623 ms |
156896 KB |
Output is correct |
33 |
Correct |
645 ms |
158508 KB |
Output is correct |
34 |
Correct |
1122 ms |
159288 KB |
Output is correct |
35 |
Correct |
981 ms |
161612 KB |
Output is correct |
36 |
Correct |
1317 ms |
158868 KB |
Output is correct |
37 |
Correct |
1154 ms |
162764 KB |
Output is correct |
38 |
Correct |
1611 ms |
160720 KB |
Output is correct |
39 |
Correct |
869 ms |
162276 KB |
Output is correct |
40 |
Correct |
997 ms |
161072 KB |
Output is correct |
41 |
Correct |
980 ms |
162484 KB |
Output is correct |
42 |
Correct |
1477 ms |
162600 KB |
Output is correct |
43 |
Correct |
1267 ms |
162648 KB |
Output is correct |
44 |
Correct |
1060 ms |
159812 KB |
Output is correct |
45 |
Correct |
1586 ms |
159156 KB |
Output is correct |
46 |
Correct |
1525 ms |
158772 KB |
Output is correct |
47 |
Correct |
1490 ms |
158532 KB |
Output is correct |
48 |
Correct |
1269 ms |
158284 KB |
Output is correct |
49 |
Correct |
1384 ms |
158340 KB |
Output is correct |
50 |
Correct |
1115 ms |
158904 KB |
Output is correct |
51 |
Correct |
2 ms |
596 KB |
Output is correct |
52 |
Correct |
19 ms |
2644 KB |
Output is correct |
53 |
Correct |
1059 ms |
161608 KB |
Output is correct |
54 |
Correct |
1283 ms |
162468 KB |
Output is correct |
55 |
Correct |
1279 ms |
160864 KB |
Output is correct |
56 |
Correct |
1311 ms |
162456 KB |
Output is correct |
57 |
Correct |
2 ms |
596 KB |
Output is correct |
58 |
Correct |
15 ms |
2632 KB |
Output is correct |
59 |
Correct |
1172 ms |
161624 KB |
Output is correct |
60 |
Correct |
1622 ms |
162764 KB |
Output is correct |
61 |
Correct |
1600 ms |
162632 KB |
Output is correct |
62 |
Correct |
1480 ms |
162604 KB |
Output is correct |
63 |
Correct |
1506 ms |
160312 KB |
Output is correct |
64 |
Execution timed out |
2027 ms |
160600 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |