#include<bits/stdc++.h>
using namespace std;
#define ll int
#define fi first
#define se second
#define p(x,y) pair<ll,ll>(x,y)
#define BIT(i,x) ((x>>i)&1)
#define MASK(x) (1<<x)
#define ld long double
#define __builtin_popcount __builtin_popcountll
#define pll pair<ll,ll>
template<class T1,class T2>
bool maximize(T1 &x,const T2 &y)
{
if(x<y)
{
x=y;
return 1;
}
return 0;
}
template<class T1,class T2>
bool minimize(T1 &x,const T2 &y)
{
if(x>y)
{
x=y;
return 1;
}
return 0;
}
void online()
{
std::ios_base::sync_with_stdio(0);
cin.tie(0);
}
const ll N=2e5+10,M=998244353;
struct Range
{
ll l,r;
Range(ll _l=0,ll _r=00)
{
l=_l;
r=_r;
}
Range operator |=(const Range &o)
{
minimize(l,o.l);
maximize(r,o.r);
return *this;
}
Range operator |(const Range &o)
{
Range res=*this;
minimize(res.l,o.l);
maximize(res.r,o.r);
return res;
}
};
struct SegmentTree
{
vector<Range> Tree;
vector<Range> Lazy;
ll n;
void build(ll i,ll l,ll r)
{
Lazy[i].l=n;
Lazy[i].r=1;
if(l==r)
{
Tree[i].l=Tree[i].r=l;
return;
}
build(i*2,l,(l+r)/2);
build(i*2+1,(l+r)/2+1,r);
Tree[i].l=min(Tree[i*2].l,Tree[i*2+1].l);
Tree[i].r=max(Tree[i*2].r,Tree[i*2+1].r);
}
SegmentTree(ll _n=0)
{
n=_n;
Tree.resize(4*n);
Lazy.resize(4*n);
if(n!=0)
build(1,1,n);
}
void down(ll i,ll l,ll r)
{
ll x=Lazy[i].l;
Lazy[i].l=n;
if(x!=n)
{
minimize(Tree[i*2].l,x);
minimize(Lazy[i*2].l,x);
minimize(Tree[i*2+1].l,x);
minimize(Lazy[i*2+1].l,x);
}
x=Lazy[i].r;
Lazy[i].r=1;
if(x!=1)
{
maximize(Tree[i*2].r,x);
maximize(Tree[i*2+1].r,x);
maximize(Lazy[i*2].r,x);
maximize(Lazy[i*2+1].r,x);
}
}
void updatel(ll i,ll l,ll r,ll l1,ll r1,ll vl)
{
if(r<l1||r1<l) return;
if(l1<=l&&r<=r1)
{
minimize(Tree[i].l,vl);
minimize(Lazy[i].l,vl);
return;
}
down(i,l,r);
updatel(i*2,l,(l+r)/2,l1,r1,vl);
updatel(i*2+1,(l+r)/2+1,r,l1,r1,vl);
Tree[i].l=min(Tree[i*2].l,Tree[i*2+1].l);
Tree[i].r=max(Tree[i*2].r,Tree[i*2+1].r);
}
void updater(ll i,ll l,ll r,ll l1,ll r1,ll vl)
{
if(r<l1||r1<l) return;
if(l1<=l&&r<=r1)
{
maximize(Tree[i].r,vl);
maximize(Lazy[i].r,vl);
return;
}
down(i,l,r);
updater(i*2,l,(l+r)/2,l1,r1,vl);
updater(i*2+1,(l+r)/2+1,r,l1,r1,vl);
Tree[i].l=min(Tree[i*2].l,Tree[i*2+1].l);
Tree[i].r=max(Tree[i*2].r,Tree[i*2+1].r);
}
void update(ll i,ll l,ll r,ll pos,Range vl)
{
if(r<pos||pos<l) return;
if(l==r)
{
Tree[i]|=vl;
return;
}
down(i,l,r);
update(i*2,l,(l+r)/2,pos,vl);
update(i*2+1,(l+r)/2+1,r,pos,vl);
Tree[i]=Tree[i*2]|Tree[i*2+1];
}
Range query(ll i,ll l,ll r,ll l1,ll r1)
{
if(r<l1||r1<l) return Range(n,1);
if(l1<=l&&r<=r1) return Tree[i];
down(i,l,r);
return query(i*2,l,(l+r)/2,l1,r1)|query(i*2+1,(l+r)/2+1,r,l1,r1);
}
}S[18];
ll n,k,m;
int main()
{
online();
cin>>n>>k>>m;
for(int i=0;i<=17;i++) S[i]=SegmentTree(n);
for(int i=1;i<=m;i++)
{
ll s,t;
cin>>s>>t;
if(s<t)
{
S[0].updater(1,1,n,s,min(s+k-1,t),t);
}
if(s>t)
{
S[0].updatel(1,1,n,max(s-k+1,t),s,t);
}
}
for(int j=1;j<=17;j++)
{
for(int i=1;i<=n;i++)
{
Range x=S[j-1].query(1,1,n,i,i);
x=S[j-1].query(1,1,n,x.l,x.r);
S[j].update(1,1,n,i,x);
}
}
ll q;
cin>>q;
while(q--)
{
ll fr,to;
cin>>fr>>to;
Range x(fr,fr);
ll ans=0,c=(1<<18);
bool flag=0;
for(int i=17;i>=0;i--)
{
c/=2;
Range y=S[i].query(1,1,n,x.l,x.r);
if(y.l<=to&&to<=y.r)
{
flag=1;
continue;
}
x=y;
ans+=c;
}
if(!flag)
{
cout<<"-1\n";
}
else cout<<ans+1<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
16 ms |
2516 KB |
Output is correct |
14 |
Correct |
16 ms |
2516 KB |
Output is correct |
15 |
Correct |
12 ms |
2516 KB |
Output is correct |
16 |
Correct |
15 ms |
2516 KB |
Output is correct |
17 |
Correct |
17 ms |
2516 KB |
Output is correct |
18 |
Correct |
12 ms |
2516 KB |
Output is correct |
19 |
Correct |
15 ms |
2460 KB |
Output is correct |
20 |
Correct |
12 ms |
2516 KB |
Output is correct |
21 |
Correct |
9 ms |
2516 KB |
Output is correct |
22 |
Correct |
15 ms |
2516 KB |
Output is correct |
23 |
Correct |
15 ms |
2516 KB |
Output is correct |
24 |
Correct |
17 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
949 ms |
113144 KB |
Output is correct |
2 |
Correct |
879 ms |
113144 KB |
Output is correct |
3 |
Correct |
922 ms |
113252 KB |
Output is correct |
4 |
Correct |
876 ms |
113140 KB |
Output is correct |
5 |
Correct |
699 ms |
113228 KB |
Output is correct |
6 |
Correct |
887 ms |
113144 KB |
Output is correct |
7 |
Correct |
619 ms |
113140 KB |
Output is correct |
8 |
Correct |
600 ms |
111988 KB |
Output is correct |
9 |
Correct |
603 ms |
113140 KB |
Output is correct |
10 |
Correct |
890 ms |
113168 KB |
Output is correct |
11 |
Correct |
832 ms |
113148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1047 ms |
113264 KB |
Output is correct |
2 |
Correct |
1040 ms |
113272 KB |
Output is correct |
3 |
Correct |
1221 ms |
113484 KB |
Output is correct |
4 |
Correct |
782 ms |
113144 KB |
Output is correct |
5 |
Correct |
853 ms |
112036 KB |
Output is correct |
6 |
Correct |
824 ms |
113176 KB |
Output is correct |
7 |
Correct |
1128 ms |
113268 KB |
Output is correct |
8 |
Correct |
1045 ms |
113392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1072 ms |
113400 KB |
Output is correct |
2 |
Correct |
1300 ms |
113424 KB |
Output is correct |
3 |
Correct |
1291 ms |
113400 KB |
Output is correct |
4 |
Correct |
1236 ms |
113356 KB |
Output is correct |
5 |
Correct |
1102 ms |
113264 KB |
Output is correct |
6 |
Correct |
1138 ms |
113272 KB |
Output is correct |
7 |
Correct |
925 ms |
113140 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
15 ms |
2516 KB |
Output is correct |
10 |
Correct |
950 ms |
113136 KB |
Output is correct |
11 |
Correct |
1012 ms |
113140 KB |
Output is correct |
12 |
Correct |
1000 ms |
111988 KB |
Output is correct |
13 |
Correct |
1016 ms |
113300 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
17 ms |
2516 KB |
Output is correct |
16 |
Correct |
905 ms |
113140 KB |
Output is correct |
17 |
Correct |
1245 ms |
113268 KB |
Output is correct |
18 |
Correct |
1190 ms |
113268 KB |
Output is correct |
19 |
Correct |
1209 ms |
113384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
16 ms |
2516 KB |
Output is correct |
14 |
Correct |
16 ms |
2516 KB |
Output is correct |
15 |
Correct |
12 ms |
2516 KB |
Output is correct |
16 |
Correct |
15 ms |
2516 KB |
Output is correct |
17 |
Correct |
17 ms |
2516 KB |
Output is correct |
18 |
Correct |
12 ms |
2516 KB |
Output is correct |
19 |
Correct |
15 ms |
2460 KB |
Output is correct |
20 |
Correct |
12 ms |
2516 KB |
Output is correct |
21 |
Correct |
9 ms |
2516 KB |
Output is correct |
22 |
Correct |
15 ms |
2516 KB |
Output is correct |
23 |
Correct |
15 ms |
2516 KB |
Output is correct |
24 |
Correct |
17 ms |
2516 KB |
Output is correct |
25 |
Correct |
949 ms |
113144 KB |
Output is correct |
26 |
Correct |
879 ms |
113144 KB |
Output is correct |
27 |
Correct |
922 ms |
113252 KB |
Output is correct |
28 |
Correct |
876 ms |
113140 KB |
Output is correct |
29 |
Correct |
699 ms |
113228 KB |
Output is correct |
30 |
Correct |
887 ms |
113144 KB |
Output is correct |
31 |
Correct |
619 ms |
113140 KB |
Output is correct |
32 |
Correct |
600 ms |
111988 KB |
Output is correct |
33 |
Correct |
603 ms |
113140 KB |
Output is correct |
34 |
Correct |
890 ms |
113168 KB |
Output is correct |
35 |
Correct |
832 ms |
113148 KB |
Output is correct |
36 |
Correct |
1047 ms |
113264 KB |
Output is correct |
37 |
Correct |
1040 ms |
113272 KB |
Output is correct |
38 |
Correct |
1221 ms |
113484 KB |
Output is correct |
39 |
Correct |
782 ms |
113144 KB |
Output is correct |
40 |
Correct |
853 ms |
112036 KB |
Output is correct |
41 |
Correct |
824 ms |
113176 KB |
Output is correct |
42 |
Correct |
1128 ms |
113268 KB |
Output is correct |
43 |
Correct |
1045 ms |
113392 KB |
Output is correct |
44 |
Correct |
1072 ms |
113400 KB |
Output is correct |
45 |
Correct |
1300 ms |
113424 KB |
Output is correct |
46 |
Correct |
1291 ms |
113400 KB |
Output is correct |
47 |
Correct |
1236 ms |
113356 KB |
Output is correct |
48 |
Correct |
1102 ms |
113264 KB |
Output is correct |
49 |
Correct |
1138 ms |
113272 KB |
Output is correct |
50 |
Correct |
925 ms |
113140 KB |
Output is correct |
51 |
Correct |
2 ms |
596 KB |
Output is correct |
52 |
Correct |
15 ms |
2516 KB |
Output is correct |
53 |
Correct |
950 ms |
113136 KB |
Output is correct |
54 |
Correct |
1012 ms |
113140 KB |
Output is correct |
55 |
Correct |
1000 ms |
111988 KB |
Output is correct |
56 |
Correct |
1016 ms |
113300 KB |
Output is correct |
57 |
Correct |
2 ms |
596 KB |
Output is correct |
58 |
Correct |
17 ms |
2516 KB |
Output is correct |
59 |
Correct |
905 ms |
113140 KB |
Output is correct |
60 |
Correct |
1245 ms |
113268 KB |
Output is correct |
61 |
Correct |
1190 ms |
113268 KB |
Output is correct |
62 |
Correct |
1209 ms |
113384 KB |
Output is correct |
63 |
Correct |
1176 ms |
113264 KB |
Output is correct |
64 |
Correct |
1289 ms |
113392 KB |
Output is correct |
65 |
Correct |
1195 ms |
113276 KB |
Output is correct |
66 |
Correct |
722 ms |
113264 KB |
Output is correct |
67 |
Correct |
1141 ms |
113428 KB |
Output is correct |
68 |
Correct |
1070 ms |
112188 KB |
Output is correct |
69 |
Correct |
857 ms |
113136 KB |
Output is correct |
70 |
Correct |
1163 ms |
113300 KB |
Output is correct |
71 |
Correct |
1232 ms |
113400 KB |
Output is correct |