#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
struct node{
int mx=-1, mn=1e5+5;
int stmx=-1,stmn=1e5+5;
};
const int mxn=1e5+5;
pair<int,int> jump[17][mxn];
int n,k,m;
node segtree[17][mxn+mxn/2];
void push(int id,int v){
segtree[id][v*2].mn=min(segtree[id][v*2].mn,segtree[id][v].stmn);
segtree[id][v*2+1].mn=min(segtree[id][v*2+1].mn,segtree[id][v].stmn);
segtree[id][v*2].mx=max(segtree[id][v*2].mx,segtree[id][v].stmx);
segtree[id][v*2+1].mx=max(segtree[id][v*2+1].mx,segtree[id][v].stmx);
segtree[id][v*2].stmn=min(segtree[id][v*2].stmn,segtree[id][v].stmn);
segtree[id][v*2+1].stmn=min(segtree[id][v*2+1].stmn,segtree[id][v].stmn);
segtree[id][v*2].stmx=max(segtree[id][v*2].stmx,segtree[id][v].stmx);
segtree[id][v*2+1].stmx=max(segtree[id][v*2+1].stmx,segtree[id][v].stmx);
segtree[id][v].stmn=1e5+5;
segtree[id][v].stmx=-1;
}
void init(int id,int l=1,int r=n,int v=1){
segtree[id][v].mn=l,segtree[id][v].mx=r;
if(l==r){
return;
}
int mid=(l+r)/2;
init(id,l,mid,v*2);
init(id,mid+1,r,v*2+1);
}
void update1(int id,int tl,int tr,int val,int l=1,int r=n,int v=1){
if(tl>tr){
return;
}
if(tl<=l and r<=tr){
segtree[id][v].stmx=max(segtree[id][v].stmx,val);
segtree[id][v].mx=max(segtree[id][v].mx,val);
return;
}
push(id,v);
int mid=(l+r)/2;
update1(id,tl,min(mid,tr),val,l,mid,v*2);
update1(id,max(mid+1,tl),tr,val,mid+1,r,v*2+1);
segtree[id][v].mx=max(segtree[id][v*2].mx,segtree[id][v*2+1].mx);
segtree[id][v].mn=min(segtree[id][v*2].mn,segtree[id][v*2+1].mn);
}
void update2(int id,int tl,int tr,int val,int l=1,int r=n,int v=1){
if(tl>tr){
return;
}
if(tl<=l and r<=tr){
segtree[id][v].stmn=min(segtree[id][v].stmn,val);
segtree[id][v].mn=min(segtree[id][v].mn,val);
return;
}
push(id,v);
int mid=(l+r)/2;
update2(id,tl,min(mid,tr),val,l,mid,v*2);
update2(id,max(mid+1,tl),tr,val,mid+1,r,v*2+1);
segtree[id][v].mn=min(segtree[id][v*2].mn,segtree[id][v*2+1].mn);
segtree[id][v].mx=max(segtree[id][v*2].mx,segtree[id][v*2+1].mx);
}
pair<int,int> query(int id,int tl,int tr,int l=1,int r=n,int v=1){
if(tl>tr){
return {n+1,-1};
}
if(tl<=l and r<=tr){
return {segtree[id][v].mn,segtree[id][v].mx};
}
push(id,v);
int mid=(l+r)/2;
pair<int,int> x=query(id,tl,min(mid,tr),l,mid,v*2);
pair<int,int> y=query(id,max(mid+1,tl),tr,mid+1,r,v*2+1);
return {min(x.f,y.f),max(x.s,y.s)};
}
bool ok(int a,int b,int k){
pair<int,int> cur={a,a};
for(int i=0;i<17;i++){
if(k&(1<<i)){
cur=query(i,cur.f,cur.s);
}
}
return (cur.f<=b and b<=cur.s);
}
int main() {_
cin>>n>>k;
cin>>m;
for(int i=0;i<17;i++){
init(i);
}
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
if(a<b){
int tmp=min(a+k-1,b);
update1(0,a,tmp,b);
}
else{
int tmp=max(a-k+1,b);
update2(0,tmp,a,b);
}
}
for(int i=1;i<17;i++){
for(int j=1;j<=n;j++){
update1(i,j,j,query(i-1,query(i-1,j,j).f,query(i-1,j,j).s).s);
update2(i,j,j,query(i-1,query(i-1,j,j).f,query(i-1,j,j).s).f);
}
}
int q;
cin>>q;
for(int i=0;i<q;i++){
int a,b;
cin>>a>>b;
//binary search ans
int l=0,r=n+1;
while(l<r){
int mid=(l+r)/2;
if(ok(a,b,mid)){
r=mid;
}
else{
l=mid+1;
}
}
cout<<(l==n+1?-1:l)<<'\n';
}
return 0;
}
//maybe its multiset not set
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
724 KB |
Output is correct |
2 |
Correct |
9 ms |
848 KB |
Output is correct |
3 |
Correct |
6 ms |
724 KB |
Output is correct |
4 |
Correct |
8 ms |
724 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
7 ms |
724 KB |
Output is correct |
7 |
Correct |
7 ms |
724 KB |
Output is correct |
8 |
Correct |
6 ms |
724 KB |
Output is correct |
9 |
Correct |
7 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
724 KB |
Output is correct |
12 |
Correct |
8 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
724 KB |
Output is correct |
2 |
Correct |
9 ms |
848 KB |
Output is correct |
3 |
Correct |
6 ms |
724 KB |
Output is correct |
4 |
Correct |
8 ms |
724 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
7 ms |
724 KB |
Output is correct |
7 |
Correct |
7 ms |
724 KB |
Output is correct |
8 |
Correct |
6 ms |
724 KB |
Output is correct |
9 |
Correct |
7 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
724 KB |
Output is correct |
12 |
Correct |
8 ms |
724 KB |
Output is correct |
13 |
Correct |
84 ms |
1616 KB |
Output is correct |
14 |
Correct |
97 ms |
1568 KB |
Output is correct |
15 |
Correct |
47 ms |
1492 KB |
Output is correct |
16 |
Correct |
58 ms |
1492 KB |
Output is correct |
17 |
Correct |
81 ms |
1628 KB |
Output is correct |
18 |
Correct |
41 ms |
1492 KB |
Output is correct |
19 |
Correct |
56 ms |
1564 KB |
Output is correct |
20 |
Correct |
48 ms |
1688 KB |
Output is correct |
21 |
Correct |
38 ms |
1492 KB |
Output is correct |
22 |
Correct |
62 ms |
1560 KB |
Output is correct |
23 |
Correct |
69 ms |
1616 KB |
Output is correct |
24 |
Correct |
78 ms |
1564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
62 ms |
84936 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
60 ms |
84936 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
61 ms |
84948 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
724 KB |
Output is correct |
2 |
Correct |
9 ms |
848 KB |
Output is correct |
3 |
Correct |
6 ms |
724 KB |
Output is correct |
4 |
Correct |
8 ms |
724 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
7 ms |
724 KB |
Output is correct |
7 |
Correct |
7 ms |
724 KB |
Output is correct |
8 |
Correct |
6 ms |
724 KB |
Output is correct |
9 |
Correct |
7 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
724 KB |
Output is correct |
12 |
Correct |
8 ms |
724 KB |
Output is correct |
13 |
Correct |
84 ms |
1616 KB |
Output is correct |
14 |
Correct |
97 ms |
1568 KB |
Output is correct |
15 |
Correct |
47 ms |
1492 KB |
Output is correct |
16 |
Correct |
58 ms |
1492 KB |
Output is correct |
17 |
Correct |
81 ms |
1628 KB |
Output is correct |
18 |
Correct |
41 ms |
1492 KB |
Output is correct |
19 |
Correct |
56 ms |
1564 KB |
Output is correct |
20 |
Correct |
48 ms |
1688 KB |
Output is correct |
21 |
Correct |
38 ms |
1492 KB |
Output is correct |
22 |
Correct |
62 ms |
1560 KB |
Output is correct |
23 |
Correct |
69 ms |
1616 KB |
Output is correct |
24 |
Correct |
78 ms |
1564 KB |
Output is correct |
25 |
Runtime error |
62 ms |
84936 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |