/*
燒雞
燒雞
燒雞 好想進選訓
燒雞
燒雞
*/
#pragma GCC optimize("O3")
#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);
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct node{
int mx=-1, mn=1e5+5;
int stmx=-1, stmn=1e5+5;
};
const int mxn=1e5+5;
int n,k,m;
node segtree[17][1<<17];
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() {_
//setIO("wayne");
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
Compilation message
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
724 KB |
Output is correct |
2 |
Correct |
8 ms |
724 KB |
Output is correct |
3 |
Correct |
6 ms |
724 KB |
Output is correct |
4 |
Correct |
7 ms |
724 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
5 ms |
724 KB |
Output is correct |
7 |
Correct |
6 ms |
724 KB |
Output is correct |
8 |
Correct |
6 ms |
724 KB |
Output is correct |
9 |
Correct |
6 ms |
724 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
724 KB |
Output is correct |
12 |
Correct |
7 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
724 KB |
Output is correct |
2 |
Correct |
8 ms |
724 KB |
Output is correct |
3 |
Correct |
6 ms |
724 KB |
Output is correct |
4 |
Correct |
7 ms |
724 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
5 ms |
724 KB |
Output is correct |
7 |
Correct |
6 ms |
724 KB |
Output is correct |
8 |
Correct |
6 ms |
724 KB |
Output is correct |
9 |
Correct |
6 ms |
724 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
724 KB |
Output is correct |
12 |
Correct |
7 ms |
724 KB |
Output is correct |
13 |
Correct |
81 ms |
1656 KB |
Output is correct |
14 |
Correct |
78 ms |
1524 KB |
Output is correct |
15 |
Correct |
47 ms |
1524 KB |
Output is correct |
16 |
Correct |
58 ms |
1492 KB |
Output is correct |
17 |
Correct |
75 ms |
1492 KB |
Output is correct |
18 |
Correct |
39 ms |
1492 KB |
Output is correct |
19 |
Correct |
53 ms |
1492 KB |
Output is correct |
20 |
Correct |
43 ms |
1492 KB |
Output is correct |
21 |
Correct |
36 ms |
1492 KB |
Output is correct |
22 |
Correct |
58 ms |
1540 KB |
Output is correct |
23 |
Correct |
65 ms |
1492 KB |
Output is correct |
24 |
Correct |
73 ms |
1524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
56 ms |
71176 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
59 ms |
71160 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
56 ms |
71136 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
724 KB |
Output is correct |
2 |
Correct |
8 ms |
724 KB |
Output is correct |
3 |
Correct |
6 ms |
724 KB |
Output is correct |
4 |
Correct |
7 ms |
724 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
5 ms |
724 KB |
Output is correct |
7 |
Correct |
6 ms |
724 KB |
Output is correct |
8 |
Correct |
6 ms |
724 KB |
Output is correct |
9 |
Correct |
6 ms |
724 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
724 KB |
Output is correct |
12 |
Correct |
7 ms |
724 KB |
Output is correct |
13 |
Correct |
81 ms |
1656 KB |
Output is correct |
14 |
Correct |
78 ms |
1524 KB |
Output is correct |
15 |
Correct |
47 ms |
1524 KB |
Output is correct |
16 |
Correct |
58 ms |
1492 KB |
Output is correct |
17 |
Correct |
75 ms |
1492 KB |
Output is correct |
18 |
Correct |
39 ms |
1492 KB |
Output is correct |
19 |
Correct |
53 ms |
1492 KB |
Output is correct |
20 |
Correct |
43 ms |
1492 KB |
Output is correct |
21 |
Correct |
36 ms |
1492 KB |
Output is correct |
22 |
Correct |
58 ms |
1540 KB |
Output is correct |
23 |
Correct |
65 ms |
1492 KB |
Output is correct |
24 |
Correct |
73 ms |
1524 KB |
Output is correct |
25 |
Runtime error |
56 ms |
71176 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |