This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vb vector<bool>
#define vii vector<pii>
#define siz(x) (int)x.size()
#define pb push_back
#define st first
#define nd second
#define rep(i,a,b) for(int i=a; i<=b; i++)
using namespace std;
const int inf = 1e9, maxn = 1e5+5, mod = 1e9+7;
pii seg[maxn][20];
int base = (1<<17);
int n,k,m;
class segment_tree{
vi mini;
vi maxi;
public:
void init(int p){
mini.resize(base*2,inf);
maxi.resize(base*2,-inf);
rep(i,1,n){
mini[i+base] = seg[i][p].st;
maxi[i+base] = seg[i][p].nd;
}
for(int i=base-1; i>=1; i--){
mini[i] = min(mini[i*2],mini[i*2+1]);
maxi[i] = max(maxi[i*2],maxi[i*2+1]);
}
}
int query_min(int a, int b){
a+=base-1;b+=base+1;
int ans = inf;
while(a/2!=b/2){
if(!(a&1))ans = min(ans,mini[a+1]);
if(b&1)ans = min(ans,mini[b-1]);
a/=2,b/=2;
}
return ans;
}
int query_max(int a, int b){
a+=base-1;b+=base+1;
int ans = -inf;
while(a/2!=b/2){
if(!(a&1))ans = max(ans,maxi[a+1]);
if(b&1)ans = max(ans,maxi[b-1]);
a/=2,b/=2;
}
return ans;
}
};
class segment_tree_max{
vi tree;
public:
segment_tree_max(){
tree.resize(2*base,-inf);
}
void add(int a, int b, int v){
a+=base-1;b+=base+1;
while(a/2!=b/2){
if(!(a&1))tree[a+1] = max(tree[a+1],v);
if(b&1)tree[b-1] = max(tree[b-1],v);
a/=2,b/=2;
}
}
int query(int a){
int ans = a;
a+=base;
while(a){
ans = max(ans,tree[a]);
a/=2;
}
return ans;
}
};
class segment_tree_min{
vi tree;
public:
segment_tree_min(){
tree.resize(2*base,inf);
}
void add(int a, int b, int v){
a+=base-1;b+=base+1;
while(a/2!=b/2){
if(!(a&1))tree[a+1] = min(tree[a+1],v);
if(b&1)tree[b-1] = min(tree[b-1],v);
a/=2,b/=2;
}
}
int query(int a){
int ans = a;
a+=base;
while(a){
ans = min(ans,tree[a]);
a/=2;
}
return ans;
}
};
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>k>>m;
vii linie;
rep(i,1,m){
int a,b;cin>>a>>b;
linie.pb({a,b});
}
segment_tree_max MX;
for(auto [a,b] : linie){
if(a<b)
MX.add(a,min(a+k-1,n),b);
}
rep(i,1,n)seg[i][0].nd = MX.query(i);
segment_tree_min MN;
for(auto [a,b] : linie){
if(a>b)
MN.add(max(1ll,a-k+1),a,b);
}
rep(i,1,n)seg[i][0].st = MN.query(i);
segment_tree tree[20];
rep(i,1,19){
tree[i-1].init(i-1);
rep(j,1,n){
seg[j][i].st = tree[i-1].query_min(seg[j][i-1].st,seg[j][i-1].nd);
seg[j][i].nd = tree[i-1].query_max(seg[j][i-1].st,seg[j][i-1].nd);
}
}
/* rep(i,1,n)cout<<seg[i][2].st<<','<<seg[i][2].nd<<' ';cout<<'\n';
rep(i,1,n)cout<<seg[i][3].st<<','<<seg[i][3].nd<<' ';cout<<'\n';
return 0;
*/
int q;cin>>q;
rep(i,1,q){
int a,b;cin>>a>>b;
int ans = 0;
pii naj = {a,a};
for(int j=18; j>=0; j--){
pii akt;
akt.st = tree[j].query_min(naj.st,naj.nd);
akt.nd = tree[j].query_max(naj.st,naj.nd);
if(b<akt.st || b>akt.nd){
ans+=(1<<j);
naj = akt;
}
}
ans++;
naj.st = tree[0].query_min(naj.st,naj.nd);
naj.nd = tree[0].query_max(naj.st,naj.nd);
if(naj.st<=b && naj.nd>=b)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... |