#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
// #define int long long
using namespace std;
using pii = pair<int, int>;
// vector<vector<int>> segl, segr, tagl, tagr;
vector<int> segl[20], segr[20], tagl[20], tagr[20];
inline void pushl(int id, int x, int l, int r)
{
if (tagl[id][x] == 1e9)
return;
segl[id][x] = tagl[id][x];
if (l + 1 != r)
tagl[id][x << 1] = tagl[id][x << 1 | 1] = tagl[id][x];
tagl[id][x] = 1e9;
}
inline void updl(int id, int x, int l, int r, int ql, int qr, int k)
{
pushl(id, x, l, r);
if (qr <= l || r <= ql)
return;
if (ql <= l && r <= qr)
{
tagl[id][x] = k;
pushl(id, x, l, r);
return;
}
int mid = (l + r) >> 1;
updl(id, x << 1, l, mid, ql, qr, k);
updl(id, x << 1 | 1, mid, r, ql, qr, k);
segl[id][x] = min(segl[id][x << 1], segl[id][x << 1 | 1]);
}
inline int qryl(int id, int x, int l, int r, int ql, int qr)
{
pushl(id, x, l, r);
if (qr <= l || r <= ql)
return 1e9;
if (ql <= l && r <= qr)
return segl[id][x];
int mid = (l + r) >> 1;
return min(qryl(id, x << 1, l, mid, ql, qr), qryl(id, x << 1 | 1, mid, r, ql, qr));
}
inline void pushr(int id, int x, int l, int r)
{
if (tagr[id][x] == -1e9)
return;
segr[id][x] = tagr[id][x];
if (l + 1 != r)
tagr[id][x << 1] = tagr[id][x << 1 | 1] = tagr[id][x];
tagr[id][x] = -1e9;
}
inline void updr(int id, int x, int l, int r, int ql, int qr, int k)
{
pushr(id, x, l, r);
if (qr <= l || r <= ql)
return;
if (ql <= l && r <= qr)
{
tagr[id][x] = k;
pushr(id, x, l, r);
return;
}
int mid = (l + r) >> 1;
updr(id, x << 1, l, mid, ql, qr, k);
updr(id, x << 1 | 1, mid, r, ql, qr, k);
segr[id][x] = max(segr[id][x << 1], segr[id][x << 1 | 1]);
}
inline int qryr(int id, int x, int l, int r, int ql, int qr)
{
pushr(id, x, l, r);
if (qr <= l || r <= ql)
return -1e9;
if (ql <= l && r <= qr)
return segr[id][x];
int mid = (l + r) >> 1;
return max(qryr(id, x << 1, l, mid, ql, qr), qryr(id, x << 1 | 1, mid, r, ql, qr));
}
inline void solve()
{
int n, k, m;
cin >> n >> k >> m;
vector<pii> v(m);
for (auto &[a, b] : v)
cin >> a >> b, --a, --b;
segl[0].assign(n * 4 + 5, 1e9);
segr[0].assign(n * 4 + 5, -1e9);
for (int i = 0; i < 20; ++i)
{
tagl[i] = segl[i] = segl[0];
tagr[i] = segr[i] = segr[0];
}
for (int i = 0; i < n; ++i)
{
updl(0, 1, 0, n, i, i + 1, i);
updr(0, 1, 0, n, i, i + 1, i);
}
vector<array<int, 3>> opl, opr;
array<int, 3> tmp;
for (auto [a, b] : v)
{
if (a < b)
opr.emplace_back(tmp = {b, a, min(a + k, b)});
else
opl.emplace_back(tmp = {b, max(a - k, b) + 1, a + 1});
}
sort(opl.begin(), opl.end());
sort(opr.begin(), opr.end());
reverse(opl.begin(), opl.end());
for (auto [a, b, c] : opl)
updl(0, 1, 0, n, b, c, a);
for (auto [a, b, c] : opr)
updr(0, 1, 0, n, b, c, a);
for (int i = 1; i < 20; ++i)
for (int j = 0; j < n; ++j)
{
int mn = qryl(i - 1, 1, 0, n, j, j + 1);
int mx = qryr(i - 1, 1, 0, n, j, j + 1);
updl(i, 1, 0, n, j, j + 1, qryl(i - 1, 1, 0, n, mn, mx + 1));
updr(i, 1, 0, n, j, j + 1, qryr(i - 1, 1, 0, n, mn, mx + 1));
}
int q;
cin >> q;
while (q--)
{
int s, t;
cin >> s >> t;
--s, --t;
if (t < qryl(19, 1, 0, n, s, s + 1) || qryr(19, 1, 0, n, s, s + 1) < t)
{
cout << "-1\n";
continue;
}
int ans = 0, ll = s, rr = s;
for (int msk = 19; msk >= 0; --msk)
{
int mn = qryl(msk, 1, 0, n, ll, rr + 1);
int mx = qryr(msk, 1, 0, n, ll, rr + 1);
if (mn <= t && t <= mx)
continue;
ans += (1 << msk);
ll = mn, rr = mx;
}
cout << ans + 1 << '\n';
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:87:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
87 | for (auto &[a, b] : v)
| ^
Main.cpp:106:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
106 | for (auto [a, b] : v)
| ^
Main.cpp:116:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
116 | for (auto [a, b, c] : opl)
| ^
Main.cpp:118:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
118 | for (auto [a, b, c] : opr)
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
604 KB |
Output is correct |
2 |
Correct |
4 ms |
604 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
716 KB |
Output is correct |
5 |
Correct |
4 ms |
860 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
4 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
604 KB |
Output is correct |
2 |
Correct |
4 ms |
604 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
716 KB |
Output is correct |
5 |
Correct |
4 ms |
860 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
4 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
860 KB |
Output is correct |
13 |
Correct |
31 ms |
2908 KB |
Output is correct |
14 |
Correct |
31 ms |
2904 KB |
Output is correct |
15 |
Correct |
24 ms |
3160 KB |
Output is correct |
16 |
Correct |
20 ms |
2904 KB |
Output is correct |
17 |
Correct |
31 ms |
3056 KB |
Output is correct |
18 |
Correct |
22 ms |
2904 KB |
Output is correct |
19 |
Correct |
29 ms |
2936 KB |
Output is correct |
20 |
Correct |
23 ms |
2908 KB |
Output is correct |
21 |
Correct |
17 ms |
2908 KB |
Output is correct |
22 |
Correct |
28 ms |
2908 KB |
Output is correct |
23 |
Correct |
31 ms |
3160 KB |
Output is correct |
24 |
Correct |
30 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1844 ms |
128372 KB |
Output is correct |
2 |
Correct |
1861 ms |
128344 KB |
Output is correct |
3 |
Correct |
1855 ms |
128876 KB |
Output is correct |
4 |
Correct |
1802 ms |
128272 KB |
Output is correct |
5 |
Correct |
1182 ms |
131448 KB |
Output is correct |
6 |
Correct |
1705 ms |
131136 KB |
Output is correct |
7 |
Correct |
1208 ms |
131220 KB |
Output is correct |
8 |
Correct |
1116 ms |
127588 KB |
Output is correct |
9 |
Correct |
1168 ms |
128676 KB |
Output is correct |
10 |
Correct |
1689 ms |
131148 KB |
Output is correct |
11 |
Correct |
1556 ms |
131148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2039 ms |
128584 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1618 ms |
132296 KB |
Output is correct |
2 |
Execution timed out |
2035 ms |
129224 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
604 KB |
Output is correct |
2 |
Correct |
4 ms |
604 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
716 KB |
Output is correct |
5 |
Correct |
4 ms |
860 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
4 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
860 KB |
Output is correct |
13 |
Correct |
31 ms |
2908 KB |
Output is correct |
14 |
Correct |
31 ms |
2904 KB |
Output is correct |
15 |
Correct |
24 ms |
3160 KB |
Output is correct |
16 |
Correct |
20 ms |
2904 KB |
Output is correct |
17 |
Correct |
31 ms |
3056 KB |
Output is correct |
18 |
Correct |
22 ms |
2904 KB |
Output is correct |
19 |
Correct |
29 ms |
2936 KB |
Output is correct |
20 |
Correct |
23 ms |
2908 KB |
Output is correct |
21 |
Correct |
17 ms |
2908 KB |
Output is correct |
22 |
Correct |
28 ms |
2908 KB |
Output is correct |
23 |
Correct |
31 ms |
3160 KB |
Output is correct |
24 |
Correct |
30 ms |
2908 KB |
Output is correct |
25 |
Correct |
1844 ms |
128372 KB |
Output is correct |
26 |
Correct |
1861 ms |
128344 KB |
Output is correct |
27 |
Correct |
1855 ms |
128876 KB |
Output is correct |
28 |
Correct |
1802 ms |
128272 KB |
Output is correct |
29 |
Correct |
1182 ms |
131448 KB |
Output is correct |
30 |
Correct |
1705 ms |
131136 KB |
Output is correct |
31 |
Correct |
1208 ms |
131220 KB |
Output is correct |
32 |
Correct |
1116 ms |
127588 KB |
Output is correct |
33 |
Correct |
1168 ms |
128676 KB |
Output is correct |
34 |
Correct |
1689 ms |
131148 KB |
Output is correct |
35 |
Correct |
1556 ms |
131148 KB |
Output is correct |
36 |
Execution timed out |
2039 ms |
128584 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |