//JOI 2022 Final Round
#include<bits/stdc++.h>
using namespace std;
const int L=25;
const int INF=1e9+7;
struct segtree{
vector<pair<int,int> > tree;
segtree(int n){
tree.assign(n*4, {INF, -1});
}
pair<int,int> merge(pair<int,int> a, pair<int,int> b){
pair<int,int> ans;
ans.first = min(a.first, b.first);
ans.second = max(a.second, b.second);
return ans;
}
pair<int,int> update(int l, int r, int v, int x, pair<int,int> val){
if(x<l || r<=x) return tree[v];
if(l+1==r){
return tree[v]=val;
}
int m=l+(r-l)/2;
return tree[v] = merge(update(l, m, v*2+1, x, val), update(m, r, v*2+2, x, val));
}
pair<int,int> query(int l, int r, int v, int ql, int qr){
if(qr<=l || r<=ql) return {INF, -1};
if(ql<=l && r<=qr){
return tree[v];
}
int m=l+(r-l)/2;
return merge(query(l, m, v*2+1, ql, qr), query(m, r, v*2+2, ql, qr));
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
int m;
cin >> m;
vector<int> val(m);
vector<pair<int, pair<int, int> > >events;
for(int i=0; i<m; i++){
int a, b;
cin >> a >> b;
if(a < b){
events.push_back({a, {1, i}});
events.push_back({min(a+k, b), {2, i}});
val[i] = b;
}
else{
swap(a, b);
events.push_back({max(b-k+1, a), {1, i}});
events.push_back({b+1, {2, i}});
val[i] = a;
}
}
sort(events.begin(), events.end());
/*for(auto [a, x]: events){
auto [b, c] = x;
cout << a << " " << b << " " << c << "=> " << val[c] << "\n";
}*/
vector<segtree> range(L, n+1);
int ind=0;
set<pair<int,int> > active;
for(int i=1; i<=n; i++){
while(ind < 2*m && events[ind].first<=i){
auto [x, j] = events[ind].second;
if(x==1){
active.insert({val[j], j});
}
else{
active.erase({val[j], j});
}
ind++;
}
int l =i;
int r =i;
if(!active.empty()){
r = max(i, (*prev(active.end())).first);
l = min(i, (*active.begin()).first);
}
range[0].update(0, n+1, 0, i, {l, r});
}
for(int k=1; k<L; k++){
//cout << k << "\n";
for(int i=1; i<=n; i++){
auto [l, r] = range[k-1].query(0, n+1, 0, i, i+1);
auto [l2, r2] = range[k-1].query(0, n+1, 0, l, r+1);
//cout << i << ": " << l2 << " " << r2 << "\n";
range[k].update(0, n+1, 0, i, {l2, r2});
}
}
int q;
cin >> q;
for(int i=0; i<q; i++){
int s, t;
cin >> s >> t;
auto rangebig = range[L-1].query(0, n+1, 0, s, s+1);
//cout << rangebig.first << " " << rangebig.second << "\n";
if(t < rangebig.first || rangebig.second < t){
cout << -1 << "\n";
continue;
}
if(s==t){
cout << 0 << "\n";
continue;
}
pair<int,int> r={s, s};
int ans=0;
for(int k=L-1; k>=0; k--){
auto rangenext = range[k].query(0, n+1, 0, r.first, r.second+1);
if(t < rangenext.first || rangenext.second < t){
r=rangenext;
ans += (1<<k);
}
}
cout << ans+1 << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
576 KB |
Output is correct |
4 |
Correct |
2 ms |
576 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
576 KB |
Output is correct |
4 |
Correct |
2 ms |
576 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
576 KB |
Output is correct |
13 |
Correct |
16 ms |
2004 KB |
Output is correct |
14 |
Correct |
18 ms |
2080 KB |
Output is correct |
15 |
Correct |
12 ms |
2004 KB |
Output is correct |
16 |
Correct |
11 ms |
2004 KB |
Output is correct |
17 |
Correct |
15 ms |
2004 KB |
Output is correct |
18 |
Correct |
13 ms |
2088 KB |
Output is correct |
19 |
Correct |
12 ms |
1996 KB |
Output is correct |
20 |
Correct |
13 ms |
2080 KB |
Output is correct |
21 |
Correct |
11 ms |
2076 KB |
Output is correct |
22 |
Correct |
13 ms |
2004 KB |
Output is correct |
23 |
Correct |
17 ms |
2084 KB |
Output is correct |
24 |
Correct |
14 ms |
2084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
848 ms |
85032 KB |
Output is correct |
2 |
Correct |
836 ms |
84920 KB |
Output is correct |
3 |
Correct |
863 ms |
85668 KB |
Output is correct |
4 |
Correct |
820 ms |
84708 KB |
Output is correct |
5 |
Correct |
909 ms |
89620 KB |
Output is correct |
6 |
Correct |
864 ms |
89644 KB |
Output is correct |
7 |
Correct |
794 ms |
90896 KB |
Output is correct |
8 |
Correct |
725 ms |
84956 KB |
Output is correct |
9 |
Correct |
735 ms |
85728 KB |
Output is correct |
10 |
Correct |
803 ms |
89684 KB |
Output is correct |
11 |
Correct |
821 ms |
89648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1069 ms |
85300 KB |
Output is correct |
2 |
Correct |
822 ms |
89944 KB |
Output is correct |
3 |
Correct |
1109 ms |
86000 KB |
Output is correct |
4 |
Correct |
917 ms |
91396 KB |
Output is correct |
5 |
Correct |
931 ms |
90952 KB |
Output is correct |
6 |
Correct |
897 ms |
91612 KB |
Output is correct |
7 |
Correct |
1018 ms |
89908 KB |
Output is correct |
8 |
Correct |
1043 ms |
89904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
942 ms |
89904 KB |
Output is correct |
2 |
Correct |
1218 ms |
86000 KB |
Output is correct |
3 |
Correct |
1169 ms |
83940 KB |
Output is correct |
4 |
Correct |
1146 ms |
82840 KB |
Output is correct |
5 |
Correct |
989 ms |
82216 KB |
Output is correct |
6 |
Correct |
1041 ms |
81836 KB |
Output is correct |
7 |
Correct |
932 ms |
87892 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
14 ms |
2088 KB |
Output is correct |
10 |
Correct |
875 ms |
89752 KB |
Output is correct |
11 |
Correct |
939 ms |
91672 KB |
Output is correct |
12 |
Correct |
979 ms |
89124 KB |
Output is correct |
13 |
Correct |
962 ms |
91612 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
16 ms |
2004 KB |
Output is correct |
16 |
Correct |
964 ms |
89644 KB |
Output is correct |
17 |
Correct |
1201 ms |
89860 KB |
Output is correct |
18 |
Correct |
1122 ms |
89960 KB |
Output is correct |
19 |
Correct |
1059 ms |
89920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
576 KB |
Output is correct |
4 |
Correct |
2 ms |
576 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
576 KB |
Output is correct |
13 |
Correct |
16 ms |
2004 KB |
Output is correct |
14 |
Correct |
18 ms |
2080 KB |
Output is correct |
15 |
Correct |
12 ms |
2004 KB |
Output is correct |
16 |
Correct |
11 ms |
2004 KB |
Output is correct |
17 |
Correct |
15 ms |
2004 KB |
Output is correct |
18 |
Correct |
13 ms |
2088 KB |
Output is correct |
19 |
Correct |
12 ms |
1996 KB |
Output is correct |
20 |
Correct |
13 ms |
2080 KB |
Output is correct |
21 |
Correct |
11 ms |
2076 KB |
Output is correct |
22 |
Correct |
13 ms |
2004 KB |
Output is correct |
23 |
Correct |
17 ms |
2084 KB |
Output is correct |
24 |
Correct |
14 ms |
2084 KB |
Output is correct |
25 |
Correct |
848 ms |
85032 KB |
Output is correct |
26 |
Correct |
836 ms |
84920 KB |
Output is correct |
27 |
Correct |
863 ms |
85668 KB |
Output is correct |
28 |
Correct |
820 ms |
84708 KB |
Output is correct |
29 |
Correct |
909 ms |
89620 KB |
Output is correct |
30 |
Correct |
864 ms |
89644 KB |
Output is correct |
31 |
Correct |
794 ms |
90896 KB |
Output is correct |
32 |
Correct |
725 ms |
84956 KB |
Output is correct |
33 |
Correct |
735 ms |
85728 KB |
Output is correct |
34 |
Correct |
803 ms |
89684 KB |
Output is correct |
35 |
Correct |
821 ms |
89648 KB |
Output is correct |
36 |
Correct |
1069 ms |
85300 KB |
Output is correct |
37 |
Correct |
822 ms |
89944 KB |
Output is correct |
38 |
Correct |
1109 ms |
86000 KB |
Output is correct |
39 |
Correct |
917 ms |
91396 KB |
Output is correct |
40 |
Correct |
931 ms |
90952 KB |
Output is correct |
41 |
Correct |
897 ms |
91612 KB |
Output is correct |
42 |
Correct |
1018 ms |
89908 KB |
Output is correct |
43 |
Correct |
1043 ms |
89904 KB |
Output is correct |
44 |
Correct |
942 ms |
89904 KB |
Output is correct |
45 |
Correct |
1218 ms |
86000 KB |
Output is correct |
46 |
Correct |
1169 ms |
83940 KB |
Output is correct |
47 |
Correct |
1146 ms |
82840 KB |
Output is correct |
48 |
Correct |
989 ms |
82216 KB |
Output is correct |
49 |
Correct |
1041 ms |
81836 KB |
Output is correct |
50 |
Correct |
932 ms |
87892 KB |
Output is correct |
51 |
Correct |
2 ms |
468 KB |
Output is correct |
52 |
Correct |
14 ms |
2088 KB |
Output is correct |
53 |
Correct |
875 ms |
89752 KB |
Output is correct |
54 |
Correct |
939 ms |
91672 KB |
Output is correct |
55 |
Correct |
979 ms |
89124 KB |
Output is correct |
56 |
Correct |
962 ms |
91612 KB |
Output is correct |
57 |
Correct |
2 ms |
468 KB |
Output is correct |
58 |
Correct |
16 ms |
2004 KB |
Output is correct |
59 |
Correct |
964 ms |
89644 KB |
Output is correct |
60 |
Correct |
1201 ms |
89860 KB |
Output is correct |
61 |
Correct |
1122 ms |
89960 KB |
Output is correct |
62 |
Correct |
1059 ms |
89920 KB |
Output is correct |
63 |
Correct |
1104 ms |
84900 KB |
Output is correct |
64 |
Correct |
1181 ms |
85532 KB |
Output is correct |
65 |
Correct |
1141 ms |
84928 KB |
Output is correct |
66 |
Correct |
888 ms |
89892 KB |
Output is correct |
67 |
Correct |
1140 ms |
89888 KB |
Output is correct |
68 |
Correct |
1157 ms |
89136 KB |
Output is correct |
69 |
Correct |
918 ms |
91780 KB |
Output is correct |
70 |
Correct |
1006 ms |
89892 KB |
Output is correct |
71 |
Correct |
1097 ms |
89904 KB |
Output is correct |