/*input
6 5
4
3 1
2 4
5 3
4 6
5
1 5
3 2
2 6
6 3
5 4
*/
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-6;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
ll sub(ll a,ll b){
ll x=a-b;
while(x<0) x+=MOD;
while(x>MOD) x-=MOD;
return x;
}
ll mult(ll a,ll b){
return (a*b)%MOD;
}
ll mypow(ll a,ll b){
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1) res=(res*a)%MOD;
a=(a*a)%MOD;
b>>=1;
}
return res;
}
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
out<<P.f<<' '<<P.s;
return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
REP(i,sz(V)) out<<(V[i])<<((i!=sz(V)-1)?" ":"\n");
return out;
}
const ll maxn=1e5+5;
const ll maxlg=__lg(maxn)+2;
pii table[maxn][17];
int L[maxn],R[maxn];
pii arr[maxn];
struct seg{
int mx[4*maxn],mn[4*maxn];
void build(int idx,int l,int r){
if(l==r){
mn[idx]=arr[l].f,mx[idx]=arr[l].s;
return;
}
int mid=(l+r)/2;
build(idx*2,l,mid),build(idx*2+1,mid+1,r);
mx[idx]=max(mx[idx*2],mx[idx*2+1]);
mn[idx]=min(mn[idx*2],mn[idx*2+1]);
}
int qmn(int idx,int l,int r,int ql,int qr){
if(l>qr or r<ql) return INF;
if(ql<=l and r<=qr) return mn[idx];
int mid=(l+r)/2;
return min(qmn(idx*2,l,mid,ql,qr),qmn(idx*2+1,mid+1,r,ql,qr));
}
int qmx(int idx,int l,int r,int ql,int qr){
if(l>qr or r<ql) return 0;
if(ql<=l and r<=qr) return mx[idx];
int mid=(l+r)/2;
return max(qmx(idx*2,l,mid,ql,qr),qmx(idx*2+1,mid+1,r,ql,qr));
}
}seg[20];
int main(){
int n,k,m,q;
cin>>n>>k>>m;
vector<pii> l,r;
REP(i,m){
int a,b;
cin>>a>>b;
--a,--b;
if(a<b) r.pb({a,b});
else l.pb({a,b});
}
sort(ALL(r));
int p=0,p2=0;
multiset<int> s;
REP(i,n){
while(p2<sz(r) and r[p2].f<=i) s.insert(r[p2++].s);
while(p<sz(r) and r[p].f+k<=i) s.erase(s.find(r[p++].s));
if(!sz(s)) R[i]=i;
else R[i]=*prev(s.end());
}
s.clear();
sort(ALL(l));
reverse(ALL(l));
p=p2=0;
for(int i=n-1;i>=0;i--){
while(p2<sz(l) and l[p2].f>=i) s.insert(l[p2++].s);
while(p<sz(l) and l[p].f-k>=i) s.erase(s.find(l[p++].s));
if(!sz(s)) L[i]=i;
else L[i]=*s.begin();
}
REP(i,n) table[i][0]={L[i],R[i]};
REP1(j,16){
REP(i,n){
arr[i]=table[i][j-1];
}
seg[j-1].build(1,0,n-1);
REP(i,n){
table[i][j]={seg[j-1].qmn(1,0,n-1,table[i][j-1].f,table[i][j-1].s),seg[j-1].qmx(1,0,n-1,table[i][j-1].f,table[i][j-1].s)};
//cout<<table[i][j]<<'\n';
}
}
int j=16;
REP(i,n){
arr[i]=table[i][j];
}
seg[j].build(1,0,n-1);
cin>>q;
while(q--){
int a,b;
cin>>a>>b;
--a,--b;
pii z={a,a};
int ans=1;
for(int j=16;j>=0;j--){
pii nw={seg[j].qmn(1,0,n-1,z.f,z.s),seg[j].qmx(1,0,n-1,z.f,z.s)};
if(!(nw.f<=b and b<=nw.s)) ans+=(1<<j),z=nw;
}
if(ans==(1<<17)) ans=-1;
cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Correct |
4 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
3 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
692 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Incorrect |
1 ms |
696 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Correct |
4 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
3 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
692 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Incorrect |
1 ms |
696 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
636 ms |
51956 KB |
Output is correct |
2 |
Correct |
655 ms |
51928 KB |
Output is correct |
3 |
Correct |
675 ms |
52224 KB |
Output is correct |
4 |
Correct |
607 ms |
52012 KB |
Output is correct |
5 |
Correct |
265 ms |
56408 KB |
Output is correct |
6 |
Correct |
633 ms |
54352 KB |
Output is correct |
7 |
Correct |
204 ms |
58500 KB |
Output is correct |
8 |
Correct |
170 ms |
53584 KB |
Output is correct |
9 |
Correct |
142 ms |
54432 KB |
Output is correct |
10 |
Correct |
512 ms |
54244 KB |
Output is correct |
11 |
Correct |
484 ms |
58824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
655 ms |
54912 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
930 ms |
58124 KB |
Output is correct |
2 |
Correct |
1281 ms |
53124 KB |
Output is correct |
3 |
Correct |
1278 ms |
52276 KB |
Output is correct |
4 |
Correct |
1170 ms |
51420 KB |
Output is correct |
5 |
Correct |
932 ms |
51148 KB |
Output is correct |
6 |
Correct |
906 ms |
50828 KB |
Output is correct |
7 |
Correct |
626 ms |
57584 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
12 ms |
1492 KB |
Output is correct |
10 |
Correct |
534 ms |
57776 KB |
Output is correct |
11 |
Correct |
672 ms |
64024 KB |
Output is correct |
12 |
Correct |
710 ms |
57740 KB |
Output is correct |
13 |
Correct |
780 ms |
62752 KB |
Output is correct |
14 |
Correct |
3 ms |
696 KB |
Output is correct |
15 |
Correct |
18 ms |
1404 KB |
Output is correct |
16 |
Correct |
665 ms |
54204 KB |
Output is correct |
17 |
Correct |
1256 ms |
64360 KB |
Output is correct |
18 |
Correct |
1129 ms |
55040 KB |
Output is correct |
19 |
Correct |
1036 ms |
54924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Correct |
4 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
3 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
692 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Incorrect |
1 ms |
696 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |