//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;
constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007, LOG = 18;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;
random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());
template<typename T>
bool assign_max(T& a, T b) {
if (b > a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool assign_min(T& a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
T square(T a) {
return a * a;
}
template<>
struct std::hash<pair<ll, ll>> {
ll operator() (pair<ll, ll> p) const {
return ((__int128)p.first * MOD + p.second) % INF64;
}
};
struct segment_tree_min {
vector<ll> tree;
ll size;
segment_tree_min(vector<ll> arr) {
size = 1;
while (size < arr.size()) {
size *= 2;
}
tree.resize(size * 2, 0);
arr.resize(size, 0);
build(1, 0, size, arr);
}
void build(ll v, ll l, ll r, vector<ll>& arr) {
if (r - l == 1) {
tree[v] = arr[l];
return;
}
ll mid = (l + r) / 2;
build(v * 2, l, mid, arr);
build(v * 2 + 1, mid, r, arr);
tree[v] = min(tree[v * 2], tree[v * 2 + 1]);
}
ll get(ll l, ll r) {
r++;
return get(1, 0, size, l, r);
}
ll get(ll v, ll l, ll r, ll ql, ll qr) {
if (ql <= l && r <= qr) {
return tree[v];
}
if (qr <= l || r <= ql) {
return INF32;
}
ll mid = (l + r) / 2;
return min(get(v * 2, l, mid, ql, qr), get(v * 2 + 1, mid, r, ql, qr));
}
};
struct segment_tree_max {
vector<ll> tree;
ll size;
segment_tree_max(vector<ll> arr) {
size = 1;
while (size < arr.size()) {
size *= 2;
}
tree.resize(size * 2, 0);
arr.resize(size, 0);
build(1, 0, size, arr);
}
void build(ll v, ll l, ll r, vector<ll>& arr) {
if (r - l == 1) {
tree[v] = arr[l];
return;
}
ll mid = (l + r) / 2;
build(v * 2, l, mid, arr);
build(v * 2 + 1, mid, r, arr);
tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
}
ll get(ll l, ll r) {
r++;
return get(1, 0, size, l, r);
}
ll get(ll v, ll l, ll r, ll ql, ll qr) {
if (ql <= l && r <= qr) {
return tree[v];
}
if (qr <= l || r <= ql) {
return 0;
}
ll mid = (l + r) / 2;
return max(get(v * 2, l, mid, ql, qr), get(v * 2 + 1, mid, r, ql, qr));
}
};
void solve() {
ll n, k, m;
cin >> n >> k >> m;
vector<vector<pair<ll, bool>>> ami(n + 1), ama(n + 1);
vector<pair<ll, ll>> arr(n);
for (ll i = 0; i < m; i++) {
cin >> arr[i].first >> arr[i].second;
arr[i].first--;
arr[i].second--;
if (arr[i].first < arr[i].second) {
ama[arr[i].first].emplace_back(arr[i].second, true);
ama[min(arr[i].second, arr[i].first + k)].emplace_back(arr[i].second, false);
} else {
ami[max(arr[i].second, arr[i].first - k + 1)].emplace_back(arr[i].second, true);
ami[arr[i].first + 1].emplace_back(arr[i].second, false);
}
}
multiset<ll> mi, ma;
vector<ll> ls, rs;
for (ll i = 0; i < n; i++) {
mi.insert(i);
ma.insert(i);
for (auto[j, b] : ama[i]) {
if (b) {
ma.insert(j);
} else {
ma.erase(ma.find(j));
}
}
for (auto[j, b] : ami[i]) {
if (b) {
mi.insert(j);
} else {
mi.erase(mi.find(j));
}
}
ls.push_back(*mi.begin());
rs.push_back(*ma.rbegin());
mi.erase(mi.find(i));
ma.erase(ma.find(i));
}
vector<ll> p2l = ls, p2r = rs;
vector<segment_tree_min> p2mi(1, segment_tree_min(ls));
vector<segment_tree_max> p2ma(1, segment_tree_max(rs));
for (ll i = 0; i < LOG; i++) {
vector<ll> nl, nr;
for (ll j = 0; j < n; j++) {
nl.push_back(p2mi.back().get(p2l[j], p2r[j]));
nr.push_back(p2ma.back().get(p2l[j], p2r[j]));
}
p2l = nl;
p2r = nr;
p2mi.emplace_back(nl);
p2ma.emplace_back(nr);
}
ll q;
cin >> q;
for (ll i = 0; i < q; i++) {
ll ans = 1;
ll s, t;
cin >> s >> t;
s--;
t--;
pair<ll, ll> no = make_pair(s, s);
for (ll j = LOG; j >= 0; j--) {
if (p2mi[j].get(no.first, no.second) > t || p2ma[j].get(no.first, no.second) < t) {
ans += (1 << j);
no = make_pair(p2mi[j].get(no.first, no.second), p2ma[j].get(no.first, no.second));
}
}
if (ans > n) {
cout << -1 << '\n';
} else {
cout << ans << '\n';
}
}
}
int main() {
if (IS_FILE) {
freopen("", "r", stdin);
freopen("", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
if (IS_TEST_CASES) {
cin >> t;
}
for (ll i = 0; i < t; i++) {
solve();
}
}
Compilation message
Main.cpp:12:22: warning: overflow in conversion from 'long int' to 'll' {aka 'int'} changes value from '9000000000000000000' to '-494665728' [-Woverflow]
12 | constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007, LOG = 18;
| ^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In constructor 'segment_tree_min::segment_tree_min(std::vector<int>)':
Main.cpp:55:29: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | while (size < arr.size()) {
| ~~~~~^~~~~~~~~~~~
Main.cpp: In constructor 'segment_tree_max::segment_tree_max(std::vector<int>)':
Main.cpp:93:29: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | while (size < arr.size()) {
| ~~~~~^~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:206:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
206 | freopen("", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
Main.cpp:207:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
207 | freopen("", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
604 ms |
52636 KB |
Output is correct |
2 |
Correct |
553 ms |
52588 KB |
Output is correct |
3 |
Correct |
602 ms |
53024 KB |
Output is correct |
4 |
Correct |
547 ms |
52556 KB |
Output is correct |
5 |
Runtime error |
43 ms |
21560 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
711 ms |
55540 KB |
Output is correct |
2 |
Runtime error |
44 ms |
21548 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
32 ms |
20084 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |