#include<bits/stdc++.h>
typedef long long ll;
#define fastio cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
using namespace std;
#define pii pair<int, int>
#define F first
#define S second
inline pii operator+(pii p1, pii p2){
return {min(p1.F, p2.F), max(p1.S, p2.S)};
}
/*inline ostream & operator<<(ostream &out, pii p){
out << '{' << p.F << ", " << p.S << '}';
return out;
}*/
vector<vector<vector<pii>>> table; // __lg(#train), __lg(range), range_left
void build(int n, int k){
int m, a, b;
vector<vector<int>> l(n+1), r(n+1);
for(cin >> m; m; m--){
cin >> a >> b;
if(a<b){
l[a].push_back(b);
r[min(a+k-1, b-1)].push_back(b);
}
else{
l[max(a-k+1, b+1)].push_back(b);
r[a].push_back(b);
}
}
multiset<int> s;
for(int i=1; i<=n; i++){
for(const int &st: l[i]) s.insert(st);
if(!s.empty()) table[0][0][i] = {min(i, *s.begin()), max(i, *s.rbegin())};
else table[0][0][i] = {i, i};
for(const int &st: r[i]) s.erase(s.find(st));
}
}
inline pii get(int t, pii p){
p.S++;
int tmp = __lg(p.S-p.F);
return table[t][tmp][p.F]+table[t][tmp][p.S-(1<<tmp)];
}
inline bool out(int t, pii p){
return p.F>t || t>p.S;
}
int main(){
fastio
int n, k, y, q, s, t;
cin >> n >> k;
y = __lg(n)+1;
table.assign(y+1, vector<vector<pii>>(y, vector<pii>(n+1, {n, 1})));
build(n, k);
//sprase table
for(int t=0; t<=y; t++){
if(t){
for(int i=1; i<=n; i++)
table[t][0][i] = get(t-1, table[t-1][0][i]);
}
for(int j=0; j<y-1; j++){
for(int i=1; i<=n-(1<<j); i++)
table[t][j+1][i] = table[t][j][i]+table[t][j][i+(1<<j)];
}
}
for(cin >> q; q; q--){
cin >> s >> t;
if(out(t, table[y][0][s])){
cout << "-1\n";
continue;
}
int ans = 0;
pii cur = {s, s};
for(int tt=y-1; tt>-1; tt--){
pii tmp = get(tt, cur);
if(out(t, tmp)){
ans ^= 1<<tt;
cur = tmp;
}
}
cout << ans+1 << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
3 ms |
2712 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2612 KB |
Output is correct |
19 |
Correct |
3 ms |
2396 KB |
Output is correct |
20 |
Correct |
3 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2720 KB |
Output is correct |
22 |
Correct |
3 ms |
2648 KB |
Output is correct |
23 |
Correct |
3 ms |
2648 KB |
Output is correct |
24 |
Correct |
2 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
176 ms |
255212 KB |
Output is correct |
2 |
Correct |
162 ms |
254800 KB |
Output is correct |
3 |
Correct |
166 ms |
254800 KB |
Output is correct |
4 |
Correct |
161 ms |
254808 KB |
Output is correct |
5 |
Correct |
246 ms |
254768 KB |
Output is correct |
6 |
Correct |
194 ms |
254800 KB |
Output is correct |
7 |
Correct |
231 ms |
257580 KB |
Output is correct |
8 |
Correct |
193 ms |
252240 KB |
Output is correct |
9 |
Correct |
196 ms |
254928 KB |
Output is correct |
10 |
Correct |
223 ms |
254876 KB |
Output is correct |
11 |
Correct |
247 ms |
254796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
209 ms |
254804 KB |
Output is correct |
2 |
Correct |
253 ms |
255388 KB |
Output is correct |
3 |
Correct |
195 ms |
254804 KB |
Output is correct |
4 |
Correct |
235 ms |
259352 KB |
Output is correct |
5 |
Correct |
299 ms |
255156 KB |
Output is correct |
6 |
Correct |
262 ms |
257556 KB |
Output is correct |
7 |
Correct |
260 ms |
254808 KB |
Output is correct |
8 |
Correct |
240 ms |
254804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
271 ms |
255048 KB |
Output is correct |
2 |
Correct |
244 ms |
254972 KB |
Output is correct |
3 |
Correct |
238 ms |
254800 KB |
Output is correct |
4 |
Correct |
219 ms |
255012 KB |
Output is correct |
5 |
Correct |
172 ms |
254908 KB |
Output is correct |
6 |
Correct |
173 ms |
254972 KB |
Output is correct |
7 |
Correct |
211 ms |
255064 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
254 ms |
256204 KB |
Output is correct |
11 |
Correct |
271 ms |
257776 KB |
Output is correct |
12 |
Correct |
270 ms |
253524 KB |
Output is correct |
13 |
Correct |
270 ms |
257732 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
3 ms |
2652 KB |
Output is correct |
16 |
Correct |
229 ms |
254980 KB |
Output is correct |
17 |
Correct |
270 ms |
254804 KB |
Output is correct |
18 |
Correct |
278 ms |
254804 KB |
Output is correct |
19 |
Correct |
242 ms |
254804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
3 ms |
2712 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2612 KB |
Output is correct |
19 |
Correct |
3 ms |
2396 KB |
Output is correct |
20 |
Correct |
3 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2720 KB |
Output is correct |
22 |
Correct |
3 ms |
2648 KB |
Output is correct |
23 |
Correct |
3 ms |
2648 KB |
Output is correct |
24 |
Correct |
2 ms |
2648 KB |
Output is correct |
25 |
Correct |
176 ms |
255212 KB |
Output is correct |
26 |
Correct |
162 ms |
254800 KB |
Output is correct |
27 |
Correct |
166 ms |
254800 KB |
Output is correct |
28 |
Correct |
161 ms |
254808 KB |
Output is correct |
29 |
Correct |
246 ms |
254768 KB |
Output is correct |
30 |
Correct |
194 ms |
254800 KB |
Output is correct |
31 |
Correct |
231 ms |
257580 KB |
Output is correct |
32 |
Correct |
193 ms |
252240 KB |
Output is correct |
33 |
Correct |
196 ms |
254928 KB |
Output is correct |
34 |
Correct |
223 ms |
254876 KB |
Output is correct |
35 |
Correct |
247 ms |
254796 KB |
Output is correct |
36 |
Correct |
209 ms |
254804 KB |
Output is correct |
37 |
Correct |
253 ms |
255388 KB |
Output is correct |
38 |
Correct |
195 ms |
254804 KB |
Output is correct |
39 |
Correct |
235 ms |
259352 KB |
Output is correct |
40 |
Correct |
299 ms |
255156 KB |
Output is correct |
41 |
Correct |
262 ms |
257556 KB |
Output is correct |
42 |
Correct |
260 ms |
254808 KB |
Output is correct |
43 |
Correct |
240 ms |
254804 KB |
Output is correct |
44 |
Correct |
271 ms |
255048 KB |
Output is correct |
45 |
Correct |
244 ms |
254972 KB |
Output is correct |
46 |
Correct |
238 ms |
254800 KB |
Output is correct |
47 |
Correct |
219 ms |
255012 KB |
Output is correct |
48 |
Correct |
172 ms |
254908 KB |
Output is correct |
49 |
Correct |
173 ms |
254972 KB |
Output is correct |
50 |
Correct |
211 ms |
255064 KB |
Output is correct |
51 |
Correct |
1 ms |
604 KB |
Output is correct |
52 |
Correct |
3 ms |
2652 KB |
Output is correct |
53 |
Correct |
254 ms |
256204 KB |
Output is correct |
54 |
Correct |
271 ms |
257776 KB |
Output is correct |
55 |
Correct |
270 ms |
253524 KB |
Output is correct |
56 |
Correct |
270 ms |
257732 KB |
Output is correct |
57 |
Correct |
1 ms |
600 KB |
Output is correct |
58 |
Correct |
3 ms |
2652 KB |
Output is correct |
59 |
Correct |
229 ms |
254980 KB |
Output is correct |
60 |
Correct |
270 ms |
254804 KB |
Output is correct |
61 |
Correct |
278 ms |
254804 KB |
Output is correct |
62 |
Correct |
242 ms |
254804 KB |
Output is correct |
63 |
Correct |
209 ms |
254980 KB |
Output is correct |
64 |
Correct |
200 ms |
254956 KB |
Output is correct |
65 |
Correct |
206 ms |
254804 KB |
Output is correct |
66 |
Correct |
254 ms |
255260 KB |
Output is correct |
67 |
Correct |
229 ms |
254816 KB |
Output is correct |
68 |
Correct |
267 ms |
252224 KB |
Output is correct |
69 |
Correct |
284 ms |
257956 KB |
Output is correct |
70 |
Correct |
230 ms |
254900 KB |
Output is correct |
71 |
Correct |
267 ms |
254804 KB |
Output is correct |