#include <bits/stdc++.h>
using namespace std;
void setup()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int n, k, m, q, a, b, c, x, y;
pair<int, int> sp2[20][20][100000], temp;
vector<int> s[100000], e[100000];
multiset<int> ms;
inline pair<int, int> Combine(pair<int, int> ina, pair<int, int> inb)
{
return {min(ina.first, inb.first), max(ina.second, inb.second)};
}
inline pair<int, int> Get(int f, int l, int r)
{
int len = __lg(r - l + 1);
return Combine(sp2[f][len][l], sp2[f][len][r - (1 << len) + 1]);
}
int main()
{
setup();
cin >> n >> k >> m;
while (m--)
{
cin >> a >> b;
a--;
b--;
if (a < b)
{
c = min(a + k - 1, b - 1);
s[a].push_back(b);
e[c].push_back(b);
}
else
{
c = max(b + 1, a - k + 1);
s[c].push_back(b);
e[a].push_back(b);
}
}
for (int i = 0; i < n; ++i)
{
for (auto &j : s[i])
{
ms.insert(j);
}
sp2[0][0][i] = {i, i};
if (!ms.empty())
{
sp2[0][0][i] = {min(i, (*ms.begin())), max(i, (*ms.rbegin()))};
}
for (auto &j : e[i])
{
ms.erase(ms.lower_bound(j));
}
}
for (int i = 1; i <= __lg(n); ++i)
{
for (int j = 0; j + (1 << i) <= n; ++j)
{
sp2[0][i][j] = Combine(sp2[0][i - 1][j], sp2[0][i - 1][j + (1 << (i - 1))]);
}
}
for (int i = 1; i <= __lg(n); ++i)
{
for (int j = 0; j <= __lg(n); ++j)
{
for (int h = 0; h + (1 << j) <= n; ++h)
{
sp2[i][j][h] = Get(i - 1, sp2[i - 1][j][h].first, sp2[i - 1][j][h].second);
}
}
}
cin >> q;
while (q--)
{
cin >> a >> b;
a--;
b--;
x = y = a;
c = 0;
for (int i = __lg(n); i >= 0; --i)
{
temp = Get(i, x, y);
if (b < temp.first || temp.second < b)
{
x = temp.first;
y = temp.second;
c += (1 << i);
}
}
temp = Get(0, x, y);
if (temp.first <= b && b <= temp.second)
{
cout << c + 1 << "\n";
}
else
{
cout << -1 << "\n";
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |