#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int ma[100005],mi[100005],n,m,k;
const int mx=100003;
vector<int> chma[100005][2],chmi[100005][2];
multiset<int> smi;
multiset<int,greater<int>> sma;
pii merge(pii a, pii b) {return {min(a.first,b.first),max(a.second,b.second)};}
pii s[1<<18][18];
void build(int l, int r, int idx, int tree) {
if (l==r) {
s[idx][tree]=pii(mi[l],ma[l]);
} else {
int mid=(l+r)/2;
build(l,mid,2*idx,tree);
build(mid+1,r,2*idx+1,tree);
s[idx][tree]=merge(s[2*idx][tree],s[2*idx+1][tree]);
}
}
void update(int l, int r, int idx, int x, int mmax, int mmin, int tree) {
if (x>r || x<l) return;
if (l==r) s[idx][tree]=merge(s[idx][tree],pii(mmin,mmax));
else {
int mid=(l+r)/2;
update(l,mid,2*idx,x,mmax,mmin,tree);
update(mid+1,r,2*idx+1,x,mmax,mmin,tree);
s[idx][tree]=merge(s[2*idx][tree],s[2*idx+1][tree]);
}
}
pii query(int l, int r, int idx, int x, int y, int tree) {
if (x>r || y<l) return pii(INT_MAX,-INT_MAX);
if (x<=l && r<=y) return s[idx][tree];
int mid=(l+r)/2;
return merge(query(l,mid,2*idx,x,y,tree),query(mid+1,r,2*idx+1,x,y,tree));
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
for (int i=0; i<=100003; ++i) for (int j=0; j<18; ++j) s[i][j]=pii(INT_MAX,-INT_MAX);
cin>>n>>k>>m;
for (int i=0; i<m; ++i) {
int a,b; cin>>a>>b;
if (a<b) {
chma[a][0].push_back(b);
chma[min(a+k-1,b)][1].push_back(b);
} else {
chmi[a][0].push_back(b);
chmi[max(a-k+1,b)][1].push_back(b);
}
}
for (int i=1; i<=n; ++i) {
for (auto s : chma[i][0]) sma.insert(s);
if (sma.empty()) ma[i]=i;
else ma[i]=*(sma.begin());
for (auto s : chma[i][1]) sma.erase(sma.find(s));
}
for (int i=n; i>=1; --i) {
for (auto s : chmi[i][0]) smi.insert(s);
if (smi.empty()) mi[i]=i;
else mi[i]=*(smi.begin());
for (auto s : chmi[i][1]) smi.erase(smi.find(s));
}
build(1,n,1,0);
for (int tr=1; tr<18; ++tr) {
for (int i=1; i<=n; ++i) {
pii res=query(1,n,1,mi[i],ma[i],tr-1);
mi[i]=res.first;
ma[i]=res.second;
}
build(1,n,1,tr);
}
int q; cin>>q;
while (q--) {
int st,ed; cin>>st>>ed;
if (ed<mi[st] || ma[st]<ed) cout<<-1<<"\n";
else {
int ans=0,L=st,R=st;
for (int tr=17; tr>=0; --tr) {
pii res=query(1,n,1,L,R,tr);
if (res.first<=ed && ed<=res.second) continue;
ans+=(1<<tr);
L=res.first;
R=res.second;
}
cout<<ans+1<<"\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23920 KB |
Output is correct |
4 |
Correct |
12 ms |
23820 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23816 KB |
Output is correct |
8 |
Correct |
13 ms |
23776 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23772 KB |
Output is correct |
11 |
Correct |
16 ms |
23780 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23920 KB |
Output is correct |
4 |
Correct |
12 ms |
23820 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23816 KB |
Output is correct |
8 |
Correct |
13 ms |
23776 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23772 KB |
Output is correct |
11 |
Correct |
16 ms |
23780 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
20 ms |
23952 KB |
Output is correct |
14 |
Correct |
18 ms |
23960 KB |
Output is correct |
15 |
Correct |
16 ms |
23876 KB |
Output is correct |
16 |
Correct |
14 ms |
23960 KB |
Output is correct |
17 |
Correct |
20 ms |
23960 KB |
Output is correct |
18 |
Correct |
15 ms |
23892 KB |
Output is correct |
19 |
Correct |
18 ms |
23892 KB |
Output is correct |
20 |
Correct |
16 ms |
23980 KB |
Output is correct |
21 |
Correct |
13 ms |
23892 KB |
Output is correct |
22 |
Correct |
18 ms |
23892 KB |
Output is correct |
23 |
Correct |
19 ms |
23976 KB |
Output is correct |
24 |
Correct |
19 ms |
23976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
570 ms |
52512 KB |
Output is correct |
2 |
Correct |
539 ms |
53300 KB |
Output is correct |
3 |
Correct |
617 ms |
54548 KB |
Output is correct |
4 |
Correct |
539 ms |
52916 KB |
Output is correct |
5 |
Correct |
227 ms |
57628 KB |
Output is correct |
6 |
Correct |
623 ms |
62196 KB |
Output is correct |
7 |
Correct |
211 ms |
61140 KB |
Output is correct |
8 |
Correct |
163 ms |
54324 KB |
Output is correct |
9 |
Correct |
167 ms |
54320 KB |
Output is correct |
10 |
Correct |
465 ms |
57712 KB |
Output is correct |
11 |
Correct |
429 ms |
57676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
531 ms |
53876 KB |
Output is correct |
2 |
Correct |
410 ms |
57780 KB |
Output is correct |
3 |
Correct |
811 ms |
55576 KB |
Output is correct |
4 |
Correct |
308 ms |
61152 KB |
Output is correct |
5 |
Correct |
338 ms |
58912 KB |
Output is correct |
6 |
Correct |
346 ms |
58920 KB |
Output is correct |
7 |
Correct |
638 ms |
57552 KB |
Output is correct |
8 |
Correct |
557 ms |
57460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
463 ms |
54924 KB |
Output is correct |
2 |
Correct |
808 ms |
53928 KB |
Output is correct |
3 |
Correct |
763 ms |
50920 KB |
Output is correct |
4 |
Correct |
723 ms |
48924 KB |
Output is correct |
5 |
Correct |
620 ms |
47848 KB |
Output is correct |
6 |
Correct |
653 ms |
47608 KB |
Output is correct |
7 |
Correct |
494 ms |
55740 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
18 ms |
23892 KB |
Output is correct |
10 |
Correct |
467 ms |
57696 KB |
Output is correct |
11 |
Correct |
567 ms |
59180 KB |
Output is correct |
12 |
Correct |
581 ms |
57160 KB |
Output is correct |
13 |
Correct |
560 ms |
58712 KB |
Output is correct |
14 |
Correct |
13 ms |
23840 KB |
Output is correct |
15 |
Correct |
22 ms |
24088 KB |
Output is correct |
16 |
Correct |
515 ms |
54344 KB |
Output is correct |
17 |
Correct |
750 ms |
54228 KB |
Output is correct |
18 |
Correct |
695 ms |
53944 KB |
Output is correct |
19 |
Correct |
1046 ms |
53616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23920 KB |
Output is correct |
4 |
Correct |
12 ms |
23820 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23816 KB |
Output is correct |
8 |
Correct |
13 ms |
23776 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23772 KB |
Output is correct |
11 |
Correct |
16 ms |
23780 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
20 ms |
23952 KB |
Output is correct |
14 |
Correct |
18 ms |
23960 KB |
Output is correct |
15 |
Correct |
16 ms |
23876 KB |
Output is correct |
16 |
Correct |
14 ms |
23960 KB |
Output is correct |
17 |
Correct |
20 ms |
23960 KB |
Output is correct |
18 |
Correct |
15 ms |
23892 KB |
Output is correct |
19 |
Correct |
18 ms |
23892 KB |
Output is correct |
20 |
Correct |
16 ms |
23980 KB |
Output is correct |
21 |
Correct |
13 ms |
23892 KB |
Output is correct |
22 |
Correct |
18 ms |
23892 KB |
Output is correct |
23 |
Correct |
19 ms |
23976 KB |
Output is correct |
24 |
Correct |
19 ms |
23976 KB |
Output is correct |
25 |
Correct |
570 ms |
52512 KB |
Output is correct |
26 |
Correct |
539 ms |
53300 KB |
Output is correct |
27 |
Correct |
617 ms |
54548 KB |
Output is correct |
28 |
Correct |
539 ms |
52916 KB |
Output is correct |
29 |
Correct |
227 ms |
57628 KB |
Output is correct |
30 |
Correct |
623 ms |
62196 KB |
Output is correct |
31 |
Correct |
211 ms |
61140 KB |
Output is correct |
32 |
Correct |
163 ms |
54324 KB |
Output is correct |
33 |
Correct |
167 ms |
54320 KB |
Output is correct |
34 |
Correct |
465 ms |
57712 KB |
Output is correct |
35 |
Correct |
429 ms |
57676 KB |
Output is correct |
36 |
Correct |
531 ms |
53876 KB |
Output is correct |
37 |
Correct |
410 ms |
57780 KB |
Output is correct |
38 |
Correct |
811 ms |
55576 KB |
Output is correct |
39 |
Correct |
308 ms |
61152 KB |
Output is correct |
40 |
Correct |
338 ms |
58912 KB |
Output is correct |
41 |
Correct |
346 ms |
58920 KB |
Output is correct |
42 |
Correct |
638 ms |
57552 KB |
Output is correct |
43 |
Correct |
557 ms |
57460 KB |
Output is correct |
44 |
Correct |
463 ms |
54924 KB |
Output is correct |
45 |
Correct |
808 ms |
53928 KB |
Output is correct |
46 |
Correct |
763 ms |
50920 KB |
Output is correct |
47 |
Correct |
723 ms |
48924 KB |
Output is correct |
48 |
Correct |
620 ms |
47848 KB |
Output is correct |
49 |
Correct |
653 ms |
47608 KB |
Output is correct |
50 |
Correct |
494 ms |
55740 KB |
Output is correct |
51 |
Correct |
12 ms |
23764 KB |
Output is correct |
52 |
Correct |
18 ms |
23892 KB |
Output is correct |
53 |
Correct |
467 ms |
57696 KB |
Output is correct |
54 |
Correct |
567 ms |
59180 KB |
Output is correct |
55 |
Correct |
581 ms |
57160 KB |
Output is correct |
56 |
Correct |
560 ms |
58712 KB |
Output is correct |
57 |
Correct |
13 ms |
23840 KB |
Output is correct |
58 |
Correct |
22 ms |
24088 KB |
Output is correct |
59 |
Correct |
515 ms |
54344 KB |
Output is correct |
60 |
Correct |
750 ms |
54228 KB |
Output is correct |
61 |
Correct |
695 ms |
53944 KB |
Output is correct |
62 |
Correct |
1046 ms |
53616 KB |
Output is correct |
63 |
Correct |
800 ms |
52152 KB |
Output is correct |
64 |
Correct |
893 ms |
53396 KB |
Output is correct |
65 |
Correct |
752 ms |
52496 KB |
Output is correct |
66 |
Correct |
312 ms |
56036 KB |
Output is correct |
67 |
Correct |
893 ms |
60356 KB |
Output is correct |
68 |
Correct |
564 ms |
55360 KB |
Output is correct |
69 |
Correct |
366 ms |
57236 KB |
Output is correct |
70 |
Correct |
555 ms |
55760 KB |
Output is correct |
71 |
Correct |
648 ms |
55676 KB |
Output is correct |