#include <bits/stdc++.h>
using namespace std;
// #undef _GLIBCXX_DEBUG // disable run-time bound checking, etc
// #pragma GCC optimize("Ofast,inline") // Ofast = O3,fast-math,allow-store-data-races,no-protect-parens
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("bmi,bmi2,lzcnt,popcnt") // bit manipulation
// #pragma GCC target("movbe") // byte swap
// #pragma GCC target("aes,pclmul,rdrnd") // encryption
// #pragma GCC target("avx,avx2,f16c,fma,sse3,ssse3,sse4.1,sse4.2") // SIMD
// #include <bits/extc++.h>
// using namespace __gnu_pbds;
// template<class T>using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
// template<class T>using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define ll long long
#define INF (ll)1e9+7
#define fo(i, n) for(int i=0;i<((ll)n);i++)
#define deb(x) cout << #x << " = " << x << endl;
#define deb2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define LSOne(S) ((S) & (-S))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
inline int readint(){ int v = 0; char c; while((c = getchar()) != EOF && c != ' ' && c != '\n'){ v *= 10; v += c - '0'; } return v; }
inline int readintsigned() { int v = 0; int sign = 1; char c = getchar(); if (c == '-') { sign = -1; } else { v += c - '0'; } while ((c = getchar()) != EOF && c != ' ' && c != '\n') { v *= 10; v += c - '0'; } return v * sign; }
inline string readstring() { string s; char c; while ((c = getchar()) != EOF && c != '\n' && c != ' ') { s.push_back(c); } return s; }
template <class result_t=std::chrono::milliseconds,class clock_t=std::chrono::steady_clock,class duration_t = std::chrono::milliseconds>
auto since(std::chrono::time_point<clock_t, duration_t> const& start){return std::chrono::duration_cast<result_t>(clock_t::now() - start);}
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vpii> vvpii;
typedef vector<vpl> vvpl;
struct Node{
Node *lnode = 0, *rnode;
int l, r;
pl val = {INF, -1};
pl val2 = {INF, -1};
Node(int l, int r) : l(l), r(r){}
pl query(int lo, int hi, pl &exclude){
if(r <= lo || hi <= l) return {INF, -1};
if(r <= hi && lo <= l) return (val == exclude?val2:val);
push();
return min(lnode->query(lo, hi, exclude), rnode->query(lo, hi, exclude));
}
void update(int x, int start, int index){
if(r<=x || l>x) return;
if(r-l == 1) {
val2 = min(val2, {start, index});
if(val>val2)swap(val, val2);
return;
}
push();
lnode->update(x, start, index);
rnode->update(x, start, index);
val = min(lnode->val, rnode->val);
val2 = min(max(lnode->val, rnode->val), min(lnode->val2, rnode->val2));
}
void push(){
if(!lnode){
int mid = (l+r) / 2;
lnode = new Node(l, mid);
rnode = new Node(mid, r);
}
}
};
int main() {
// cout << fixed << setprecision(20);
// auto start = std::chrono::steady_clock::now(); // since(start).count()
cin.tie(0)->sync_with_stdio(0);
ll n, q, from, to;
cin >> n >> q;
Node st(0, INF);
vpl event;
fo(i, n){
cin >> from >> to;
event.pb({from, to});
st.update(to, from, i);
}
vvl bl(n, vl(25, -1));
fo(i, n){
bl[i][0] = i;
pl current = {event[i].F, i};
pl res = st.query(event[i].F, event[i].S+1, current);
if(res.S != -1) bl[i][0] = res.S;
}
fo(j, 23){
fo(i, n)
bl[i][j+1] = bl[bl[i][j]][j];
}
fo(x, q){
cin >> from >> to;
from--;
to--;
if(from == to){
cout << "0\n";
continue;
}
if(event[from].S<=event[to].S && event[from].S>=event[to].F){
cout << "1\n";
continue;
}
int ans = 2;
for(int i = 21; i>=0; i--){
if(event[bl[to][i]].F<=event[from].S) continue;
ans+=(1<<i);
to = bl[to][i];
}
to = bl[to][0];
if(event[from].S<=event[to].S && event[from].S>=event[to].F)
cout << ans << "\n";
else cout << "impossible\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
381 ms |
185440 KB |
Output is correct |
3 |
Correct |
398 ms |
190904 KB |
Output is correct |
4 |
Correct |
483 ms |
194420 KB |
Output is correct |
5 |
Correct |
425 ms |
207540 KB |
Output is correct |
6 |
Correct |
425 ms |
217076 KB |
Output is correct |
7 |
Correct |
428 ms |
212280 KB |
Output is correct |
8 |
Correct |
418 ms |
283980 KB |
Output is correct |
9 |
Correct |
462 ms |
327448 KB |
Output is correct |
10 |
Correct |
487 ms |
182280 KB |
Output is correct |
11 |
Correct |
429 ms |
219188 KB |
Output is correct |
12 |
Correct |
118 ms |
50132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
2900 KB |
Output is correct |
4 |
Correct |
3 ms |
2900 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
1748 KB |
Output is correct |
7 |
Correct |
5 ms |
5204 KB |
Output is correct |
8 |
Correct |
5 ms |
5196 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
2900 KB |
Output is correct |
4 |
Correct |
3 ms |
2900 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
1748 KB |
Output is correct |
7 |
Correct |
5 ms |
5204 KB |
Output is correct |
8 |
Correct |
5 ms |
5196 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
2900 KB |
Output is correct |
13 |
Correct |
3 ms |
2900 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
1748 KB |
Output is correct |
16 |
Correct |
5 ms |
5196 KB |
Output is correct |
17 |
Correct |
5 ms |
5092 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
35 ms |
3748 KB |
Output is correct |
20 |
Correct |
40 ms |
13256 KB |
Output is correct |
21 |
Correct |
35 ms |
3824 KB |
Output is correct |
22 |
Correct |
32 ms |
8736 KB |
Output is correct |
23 |
Correct |
43 ms |
23500 KB |
Output is correct |
24 |
Correct |
43 ms |
23536 KB |
Output is correct |
25 |
Correct |
46 ms |
23112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
2900 KB |
Output is correct |
4 |
Correct |
3 ms |
2900 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
1748 KB |
Output is correct |
7 |
Correct |
5 ms |
5204 KB |
Output is correct |
8 |
Correct |
5 ms |
5196 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
2900 KB |
Output is correct |
13 |
Correct |
3 ms |
2900 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
3 ms |
1748 KB |
Output is correct |
16 |
Correct |
5 ms |
5196 KB |
Output is correct |
17 |
Correct |
6 ms |
5204 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
365 ms |
185356 KB |
Output is correct |
20 |
Correct |
143 ms |
38200 KB |
Output is correct |
21 |
Correct |
344 ms |
106748 KB |
Output is correct |
22 |
Correct |
365 ms |
202592 KB |
Output is correct |
23 |
Correct |
501 ms |
325412 KB |
Output is correct |
24 |
Correct |
494 ms |
325176 KB |
Output is correct |
25 |
Correct |
94 ms |
25864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
395 ms |
185400 KB |
Output is correct |
2 |
Correct |
433 ms |
191044 KB |
Output is correct |
3 |
Correct |
496 ms |
194320 KB |
Output is correct |
4 |
Correct |
445 ms |
327508 KB |
Output is correct |
5 |
Correct |
424 ms |
182400 KB |
Output is correct |
6 |
Correct |
595 ms |
327280 KB |
Output is correct |
7 |
Correct |
609 ms |
326988 KB |
Output is correct |
8 |
Correct |
537 ms |
327468 KB |
Output is correct |
9 |
Correct |
494 ms |
325112 KB |
Output is correct |
10 |
Correct |
558 ms |
326780 KB |
Output is correct |
11 |
Correct |
553 ms |
326740 KB |
Output is correct |
12 |
Correct |
549 ms |
326712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
381 ms |
185440 KB |
Output is correct |
3 |
Correct |
398 ms |
190904 KB |
Output is correct |
4 |
Correct |
483 ms |
194420 KB |
Output is correct |
5 |
Correct |
425 ms |
207540 KB |
Output is correct |
6 |
Correct |
425 ms |
217076 KB |
Output is correct |
7 |
Correct |
428 ms |
212280 KB |
Output is correct |
8 |
Correct |
418 ms |
283980 KB |
Output is correct |
9 |
Correct |
462 ms |
327448 KB |
Output is correct |
10 |
Correct |
487 ms |
182280 KB |
Output is correct |
11 |
Correct |
429 ms |
219188 KB |
Output is correct |
12 |
Correct |
118 ms |
50132 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
3 ms |
2900 KB |
Output is correct |
16 |
Correct |
3 ms |
2900 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
3 ms |
1748 KB |
Output is correct |
19 |
Correct |
5 ms |
5204 KB |
Output is correct |
20 |
Correct |
5 ms |
5196 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
3 ms |
2900 KB |
Output is correct |
25 |
Correct |
3 ms |
2900 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
3 ms |
1748 KB |
Output is correct |
28 |
Correct |
5 ms |
5196 KB |
Output is correct |
29 |
Correct |
5 ms |
5092 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
35 ms |
3748 KB |
Output is correct |
32 |
Correct |
40 ms |
13256 KB |
Output is correct |
33 |
Correct |
35 ms |
3824 KB |
Output is correct |
34 |
Correct |
32 ms |
8736 KB |
Output is correct |
35 |
Correct |
43 ms |
23500 KB |
Output is correct |
36 |
Correct |
43 ms |
23536 KB |
Output is correct |
37 |
Correct |
46 ms |
23112 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
3 ms |
2900 KB |
Output is correct |
41 |
Correct |
3 ms |
2900 KB |
Output is correct |
42 |
Correct |
1 ms |
640 KB |
Output is correct |
43 |
Correct |
3 ms |
1748 KB |
Output is correct |
44 |
Correct |
5 ms |
5196 KB |
Output is correct |
45 |
Correct |
6 ms |
5204 KB |
Output is correct |
46 |
Correct |
1 ms |
592 KB |
Output is correct |
47 |
Correct |
365 ms |
185356 KB |
Output is correct |
48 |
Correct |
143 ms |
38200 KB |
Output is correct |
49 |
Correct |
344 ms |
106748 KB |
Output is correct |
50 |
Correct |
365 ms |
202592 KB |
Output is correct |
51 |
Correct |
501 ms |
325412 KB |
Output is correct |
52 |
Correct |
494 ms |
325176 KB |
Output is correct |
53 |
Correct |
94 ms |
25864 KB |
Output is correct |
54 |
Correct |
395 ms |
185400 KB |
Output is correct |
55 |
Correct |
433 ms |
191044 KB |
Output is correct |
56 |
Correct |
496 ms |
194320 KB |
Output is correct |
57 |
Correct |
445 ms |
327508 KB |
Output is correct |
58 |
Correct |
424 ms |
182400 KB |
Output is correct |
59 |
Correct |
595 ms |
327280 KB |
Output is correct |
60 |
Correct |
609 ms |
326988 KB |
Output is correct |
61 |
Correct |
537 ms |
327468 KB |
Output is correct |
62 |
Correct |
494 ms |
325112 KB |
Output is correct |
63 |
Correct |
558 ms |
326780 KB |
Output is correct |
64 |
Correct |
553 ms |
326740 KB |
Output is correct |
65 |
Correct |
549 ms |
326712 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
393 ms |
185336 KB |
Output is correct |
68 |
Correct |
422 ms |
190884 KB |
Output is correct |
69 |
Correct |
464 ms |
194352 KB |
Output is correct |
70 |
Correct |
459 ms |
207556 KB |
Output is correct |
71 |
Correct |
477 ms |
217048 KB |
Output is correct |
72 |
Correct |
432 ms |
212280 KB |
Output is correct |
73 |
Correct |
429 ms |
284152 KB |
Output is correct |
74 |
Correct |
473 ms |
327424 KB |
Output is correct |
75 |
Correct |
474 ms |
182320 KB |
Output is correct |
76 |
Correct |
433 ms |
219192 KB |
Output is correct |
77 |
Correct |
118 ms |
50104 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
4 ms |
2900 KB |
Output is correct |
80 |
Correct |
4 ms |
2900 KB |
Output is correct |
81 |
Correct |
1 ms |
596 KB |
Output is correct |
82 |
Correct |
4 ms |
1748 KB |
Output is correct |
83 |
Correct |
6 ms |
5196 KB |
Output is correct |
84 |
Correct |
5 ms |
5204 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
31 ms |
3788 KB |
Output is correct |
87 |
Correct |
41 ms |
13232 KB |
Output is correct |
88 |
Correct |
37 ms |
3992 KB |
Output is correct |
89 |
Correct |
31 ms |
8652 KB |
Output is correct |
90 |
Correct |
43 ms |
23500 KB |
Output is correct |
91 |
Correct |
42 ms |
23552 KB |
Output is correct |
92 |
Correct |
46 ms |
23084 KB |
Output is correct |
93 |
Correct |
364 ms |
185468 KB |
Output is correct |
94 |
Correct |
150 ms |
38256 KB |
Output is correct |
95 |
Correct |
368 ms |
106800 KB |
Output is correct |
96 |
Correct |
369 ms |
202660 KB |
Output is correct |
97 |
Correct |
511 ms |
325208 KB |
Output is correct |
98 |
Correct |
496 ms |
325108 KB |
Output is correct |
99 |
Correct |
87 ms |
25788 KB |
Output is correct |
100 |
Correct |
612 ms |
327188 KB |
Output is correct |
101 |
Correct |
614 ms |
326952 KB |
Output is correct |
102 |
Correct |
567 ms |
327564 KB |
Output is correct |
103 |
Correct |
522 ms |
326804 KB |
Output is correct |
104 |
Correct |
553 ms |
326648 KB |
Output is correct |
105 |
Correct |
535 ms |
326744 KB |
Output is correct |
106 |
Correct |
198 ms |
37104 KB |
Output is correct |
107 |
Correct |
244 ms |
40116 KB |
Output is correct |
108 |
Correct |
384 ms |
108740 KB |
Output is correct |
109 |
Correct |
377 ms |
108940 KB |
Output is correct |
110 |
Correct |
558 ms |
326972 KB |
Output is correct |
111 |
Correct |
615 ms |
327084 KB |
Output is correct |