#pragma GCC optimize("O3","unroll-loops","Ofast")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
namespace{
const int INF=1e9;
#define pii pair<int,int>
#define endl '\n'
vector<pii> tmp;
inline pii comb(pii a,pii b){
return {min(a.first,b.first),max(a.second,b.second)};
}
struct seg{
int l,m,r;
int maxval=-INF,minval=INF;
int maxlz=-INF,minlz=INF;
seg *lc,*rc;
int rmax(){
return max(maxlz,maxval);
}
int rmin(){
return min(minlz,minval);
}
void pull(){
maxval=max(lc->rmax(),rc->rmax());
minval=min(lc->rmin(),rc->rmin());
}
void push(){
lc->maxlz=max(lc->maxlz,maxlz);
rc->maxlz=max(rc->maxlz,maxlz);
lc->minlz=min(lc->minlz,minlz);
rc->minlz=min(rc->minlz,minlz);
}
seg(int l1,int r1){
l=l1,r=r1;
m=(l+r)>>1;
if(r-l==1){
maxval=minval=maxlz=minlz=l1;
return;
}
lc=new seg(l,m);
rc=new seg(m,r);
pull();
}
void add(int a,int b,int x){
if(a<=l&&b>=r){
maxlz=max(maxlz,x);
minlz=min(minlz,x);
return;
}
push();
if(a<m){
lc->add(a,b,x);
}
if(b>m){
rc->add(a,b,x);
}
pull();
}
pii ask(int a,int b){
if(a<=l&&b>=r){
return {rmin(),rmax()};
}
pii ans={INF,-INF};
push();
if(a<m){
ans=comb(ans,lc->ask(a,b));
}
if(b>m){
ans=comb(ans,rc->ask(a,b));
}
pull();
return ans;
}
};
int main1(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,k;cin>>n>>k;
vector<seg*> v(18);
for(int i=0;i<18;i++){
v[i]=new seg(0,n);
}
vector<vector<pii>> dp(18,vector<pii>(n));
int m;cin>>m;
for(int i=0;i<m;i++){
int a,b;cin>>a>>b;a--;b--;
if(a<b){
int c=min(a+k,b);
v[0]->add(a,c,b);
}
else{
int c=max(b+1,a-k+1);
v[0]->add(c,a+1,b);
}
}
for(int j=0;j<n;j++){
dp[0][j]=v[0]->ask(j,j+1);
}
for(int i=1;i<18;i++){
for(int j=0;j<n;j++){
v[i]->add(j,j+1,dp[i-1][j].first);
v[i]->add(j,j+1,dp[i-1][j].second);
}
for(int j=0;j<n;j++){
int a,b;tie(a,b)=dp[i-1][j];
dp[i][j]=v[i]->ask(a,b+1);
}
}
int q;cin>>q;
for(int i=0;i<q;i++){
int a,b;cin>>a>>b;a--;b--;
if(a<b){
int ans=0;
int l=a,r=a;
bool flag=0;
for(int j=17;j>=0;j--){
int c,d;
tie(c,d)=v[j]->ask(l,r+1);
if(d<b){
ans+=(1<<(j-1));
l=c,r=d;
}
else{
flag=1;
}
}
if(flag==0){
cout<<-1<<endl;
}
else{
cout<<ans+1<<endl;
}
}
else{
int ans=0;
int l=a,r=a;
bool flag=0;
for(int j=17;j>=0;j--){
int c,d;
tie(c,d)=v[j]->ask(l,r+1);
if(c>b){
ans+=(1<<(j-1));
l=c,r=d;
}
else{
flag=1;
}
}
if(flag==0){
cout<<-1<<endl;
}
else{
cout<<ans+1<<endl;
}
}
}
return 0;
}
}
int32_t main(){
main1();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1116 KB |
Output is correct |
2 |
Correct |
2 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Correct |
2 ms |
1080 KB |
Output is correct |
5 |
Correct |
2 ms |
1116 KB |
Output is correct |
6 |
Correct |
2 ms |
1116 KB |
Output is correct |
7 |
Correct |
2 ms |
1116 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
2 ms |
1116 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1116 KB |
Output is correct |
2 |
Correct |
2 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Correct |
2 ms |
1080 KB |
Output is correct |
5 |
Correct |
2 ms |
1116 KB |
Output is correct |
6 |
Correct |
2 ms |
1116 KB |
Output is correct |
7 |
Correct |
2 ms |
1116 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
2 ms |
1116 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
19 ms |
5224 KB |
Output is correct |
14 |
Correct |
18 ms |
5268 KB |
Output is correct |
15 |
Correct |
13 ms |
5212 KB |
Output is correct |
16 |
Correct |
13 ms |
5208 KB |
Output is correct |
17 |
Correct |
18 ms |
5212 KB |
Output is correct |
18 |
Correct |
13 ms |
5212 KB |
Output is correct |
19 |
Correct |
16 ms |
4956 KB |
Output is correct |
20 |
Correct |
13 ms |
5268 KB |
Output is correct |
21 |
Correct |
10 ms |
5212 KB |
Output is correct |
22 |
Correct |
17 ms |
5212 KB |
Output is correct |
23 |
Correct |
17 ms |
5264 KB |
Output is correct |
24 |
Correct |
18 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
960 ms |
240716 KB |
Output is correct |
2 |
Correct |
936 ms |
240956 KB |
Output is correct |
3 |
Correct |
1000 ms |
240640 KB |
Output is correct |
4 |
Correct |
915 ms |
240640 KB |
Output is correct |
5 |
Correct |
733 ms |
240720 KB |
Output is correct |
6 |
Correct |
971 ms |
240592 KB |
Output is correct |
7 |
Correct |
708 ms |
240728 KB |
Output is correct |
8 |
Correct |
626 ms |
238420 KB |
Output is correct |
9 |
Correct |
639 ms |
240752 KB |
Output is correct |
10 |
Correct |
937 ms |
240696 KB |
Output is correct |
11 |
Correct |
911 ms |
240724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1434 ms |
240724 KB |
Output is correct |
2 |
Correct |
1113 ms |
240720 KB |
Output is correct |
3 |
Correct |
1529 ms |
240668 KB |
Output is correct |
4 |
Correct |
976 ms |
240724 KB |
Output is correct |
5 |
Correct |
912 ms |
238392 KB |
Output is correct |
6 |
Correct |
875 ms |
240728 KB |
Output is correct |
7 |
Correct |
1414 ms |
240728 KB |
Output is correct |
8 |
Correct |
1372 ms |
240724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1422 ms |
240592 KB |
Output is correct |
2 |
Correct |
1939 ms |
240636 KB |
Output is correct |
3 |
Correct |
1844 ms |
241212 KB |
Output is correct |
4 |
Correct |
1717 ms |
241172 KB |
Output is correct |
5 |
Correct |
1471 ms |
240936 KB |
Output is correct |
6 |
Correct |
1391 ms |
241024 KB |
Output is correct |
7 |
Correct |
1106 ms |
241852 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
16 ms |
5292 KB |
Output is correct |
10 |
Correct |
900 ms |
242336 KB |
Output is correct |
11 |
Correct |
1074 ms |
243008 KB |
Output is correct |
12 |
Correct |
1095 ms |
240428 KB |
Output is correct |
13 |
Correct |
1140 ms |
242700 KB |
Output is correct |
14 |
Correct |
3 ms |
1112 KB |
Output is correct |
15 |
Correct |
19 ms |
5212 KB |
Output is correct |
16 |
Correct |
978 ms |
242336 KB |
Output is correct |
17 |
Correct |
1753 ms |
243088 KB |
Output is correct |
18 |
Correct |
1603 ms |
242836 KB |
Output is correct |
19 |
Correct |
1421 ms |
243012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1116 KB |
Output is correct |
2 |
Correct |
2 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Correct |
2 ms |
1080 KB |
Output is correct |
5 |
Correct |
2 ms |
1116 KB |
Output is correct |
6 |
Correct |
2 ms |
1116 KB |
Output is correct |
7 |
Correct |
2 ms |
1116 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
2 ms |
1116 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
19 ms |
5224 KB |
Output is correct |
14 |
Correct |
18 ms |
5268 KB |
Output is correct |
15 |
Correct |
13 ms |
5212 KB |
Output is correct |
16 |
Correct |
13 ms |
5208 KB |
Output is correct |
17 |
Correct |
18 ms |
5212 KB |
Output is correct |
18 |
Correct |
13 ms |
5212 KB |
Output is correct |
19 |
Correct |
16 ms |
4956 KB |
Output is correct |
20 |
Correct |
13 ms |
5268 KB |
Output is correct |
21 |
Correct |
10 ms |
5212 KB |
Output is correct |
22 |
Correct |
17 ms |
5212 KB |
Output is correct |
23 |
Correct |
17 ms |
5264 KB |
Output is correct |
24 |
Correct |
18 ms |
5212 KB |
Output is correct |
25 |
Correct |
960 ms |
240716 KB |
Output is correct |
26 |
Correct |
936 ms |
240956 KB |
Output is correct |
27 |
Correct |
1000 ms |
240640 KB |
Output is correct |
28 |
Correct |
915 ms |
240640 KB |
Output is correct |
29 |
Correct |
733 ms |
240720 KB |
Output is correct |
30 |
Correct |
971 ms |
240592 KB |
Output is correct |
31 |
Correct |
708 ms |
240728 KB |
Output is correct |
32 |
Correct |
626 ms |
238420 KB |
Output is correct |
33 |
Correct |
639 ms |
240752 KB |
Output is correct |
34 |
Correct |
937 ms |
240696 KB |
Output is correct |
35 |
Correct |
911 ms |
240724 KB |
Output is correct |
36 |
Correct |
1434 ms |
240724 KB |
Output is correct |
37 |
Correct |
1113 ms |
240720 KB |
Output is correct |
38 |
Correct |
1529 ms |
240668 KB |
Output is correct |
39 |
Correct |
976 ms |
240724 KB |
Output is correct |
40 |
Correct |
912 ms |
238392 KB |
Output is correct |
41 |
Correct |
875 ms |
240728 KB |
Output is correct |
42 |
Correct |
1414 ms |
240728 KB |
Output is correct |
43 |
Correct |
1372 ms |
240724 KB |
Output is correct |
44 |
Correct |
1422 ms |
240592 KB |
Output is correct |
45 |
Correct |
1939 ms |
240636 KB |
Output is correct |
46 |
Correct |
1844 ms |
241212 KB |
Output is correct |
47 |
Correct |
1717 ms |
241172 KB |
Output is correct |
48 |
Correct |
1471 ms |
240936 KB |
Output is correct |
49 |
Correct |
1391 ms |
241024 KB |
Output is correct |
50 |
Correct |
1106 ms |
241852 KB |
Output is correct |
51 |
Correct |
2 ms |
1116 KB |
Output is correct |
52 |
Correct |
16 ms |
5292 KB |
Output is correct |
53 |
Correct |
900 ms |
242336 KB |
Output is correct |
54 |
Correct |
1074 ms |
243008 KB |
Output is correct |
55 |
Correct |
1095 ms |
240428 KB |
Output is correct |
56 |
Correct |
1140 ms |
242700 KB |
Output is correct |
57 |
Correct |
3 ms |
1112 KB |
Output is correct |
58 |
Correct |
19 ms |
5212 KB |
Output is correct |
59 |
Correct |
978 ms |
242336 KB |
Output is correct |
60 |
Correct |
1753 ms |
243088 KB |
Output is correct |
61 |
Correct |
1603 ms |
242836 KB |
Output is correct |
62 |
Correct |
1421 ms |
243012 KB |
Output is correct |
63 |
Correct |
1672 ms |
241908 KB |
Output is correct |
64 |
Correct |
1863 ms |
241804 KB |
Output is correct |
65 |
Correct |
1702 ms |
242244 KB |
Output is correct |
66 |
Correct |
761 ms |
242828 KB |
Output is correct |
67 |
Correct |
1553 ms |
243160 KB |
Output is correct |
68 |
Correct |
1240 ms |
240672 KB |
Output is correct |
69 |
Correct |
899 ms |
243008 KB |
Output is correct |
70 |
Correct |
1425 ms |
243008 KB |
Output is correct |
71 |
Correct |
1732 ms |
243084 KB |
Output is correct |