#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
const int tree_siz = 1024*256-1;
struct seg_tree
{
pii drzewo[tree_siz+1];
seg_tree()
{
rep2(i,1,tree_siz) drzewo[i] = {0,0};
}
pii get_jp(int akt, int p1, int p2, int s1, int s2)
{
if(p2 < s1 || p1 > s2) return {-1,1e9};
if(p1 >= s1 && p2 <= s2)return drzewo[akt];
pii w1 = get_jp(akt*2,p1,(p1+p2)/2,s1,s2);
pii w2 = get_jp(akt*2+1,(p1+p2)/2+1,p2,s1,s2);
return {max(w1.ff,w2.ff),min(w1.ss,w2.ss)};
}
void upd(int v)
{
drzewo[v].ff = max(drzewo[v*2].ff,drzewo[v*2+1].ff);
drzewo[v].ss = min(drzewo[v*2].ss,drzewo[v*2+1].ss);
if(v != 1) upd(v/2);
}
void change(int v, pii w)
{
drzewo[tree_siz/2+1+v] = w;
upd((tree_siz/2+v+1)/2);
}
};
vector<pii> event_fd[100'001];
vector<pii> event_bk[100'001];
seg_tree trees[17];
pii jump[100'001][17];
pii merge_jumps(int l, int r, int bit)
{
return trees[bit].get_jp(1,0,tree_siz/2,l,r);
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random_start();
int n,k,m;
cin >> n >> k >> m;
rep2(i,1,n) jump[i][0] = {i,i};
rep(i,m)
{
int a,b;
cin >> a >> b;
if(a <= b)
{
event_fd[a].pb({1,b});
event_fd[min(a+k,b)].pb({-1,b});
}
else
{
swap(a,b);
event_bk[b].pb({1,a});
event_bk[max(b-k,a)].pb({-1,a});
}
}
multiset<int> s;
rep2(i,1,n)
{
forall(it,event_fd[i])
{
if(it.ff == 1)
{
s.insert(it.ss);
}
else
{
s.erase(s.find(it.ss));
}
}
if(siz(s) != 0)
{
auto it = s.end();
it--;
jump[i][0].ff = *it;
}
}
s = {};
for(int i = n; i >= 1; i--)
{
forall(it,event_bk[i])
{
if(it.ff == 1)
{
s.insert(it.ss);
}
else
{
s.erase(s.find(it.ss));
}
}
if(siz(s) != 0)
{
jump[i][0].ss = *s.begin();
}
}
s = {};
rep2(i,1,n) trees[0].change(i,jump[i][0]);
rep2(bit,1,16)
{
rep2(i,1,n)
{
jump[i][bit] = merge_jumps(jump[i][bit-1].ss,jump[i][bit-1].ff,bit-1);
trees[bit].change(i,jump[i][bit]);
}
}
int q;
cin >> q;
rep(qq,q)
{
int a,b;
cin >> a >> b;
int ans = 0;
int cur_l = a;
int cur_r = a;
for(int bit = 16; bit >= 0; bit--)
{
pii new_j = merge_jumps(cur_l,cur_r,bit);
if(b < new_j.ss || b > new_j.ff)
{
ans += (1 << bit);
cur_l = new_j.ss;
cur_r = new_j.ff;
}
}
ans++;
if(ans != (1 << 17)) cout << ans << "\n";
else cout << "-1\n";
}
}
# | 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... |