#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"
const ld PI = 3.1415926535;
const int N = 2e5+10;
const int M = 7e6 + 1;
int mod = 1e9 + 7;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
const int P = 467743;
int mult(int a, int b) {
return a * 1LL * b % mod;
}
int sum(int a, int b) {
if(a + b >= mod)
return a + b - mod;
if(a + b < 0)
return a + b + mod;
return a + b;
}
int binpow(int a, ll n) {
if (n == 0)
return 1;
if (n % 2 == 1) {
return mult(binpow(a, n - 1), a);
} else {
int b = binpow(a, n / 2);
return mult(b, b);
}
}
const int K = 20;
int n, st[N], en[N], up[N][K], q;
pii mnl[N];
struct sparset{
pii st[N][K];
void init(int n){
for(int i = 1; i <= n; i++)
st[i][0] = mnl[i];
for(int k = 1; k < K; k++){
for(int i = 1; i + (1 << k) - 1 <= n; i++)
st[i][k] = min(st[i][k - 1], st[i + (1 << (k - 1))][k - 1]);
}
}
int get(int l, int r){
int k = __lg(r - l + 1);
return min(st[l][k], st[r - (1 << k) + 1][k]).S;
}
};
void solve(){
cin >> n >> q;
vector<int> vs = {};
for(int i = 1; i <= n; i++){
cin >> st[i] >> en[i];
vs.pb(st[i]);
vs.pb(en[i]);
}
sort(all(vs));
vs.resize(unique(all(vs)) - vs.begin());
for(int i = 1; i <= sz(vs); i++){
mnl[i] = {infl, 0};
}
for(int i = 1; i <= n; i++){
int ps = upper_bound(all(vs), en[i]) - vs.begin();
mnl[ps] = min(mnl[ps], {upper_bound(all(vs), st[i]) - vs.begin(), i});
}
sparset spr;
spr.init(sz(vs));
for(int i = 1; i <= n; i++){
int l = upper_bound(all(vs), st[i]) - vs.begin(), r = upper_bound(all(vs), en[i]) - vs.begin();
int ml = spr.get(l, r);
up[i][0] = ml;
}
for(int k = 1; k < K; k++)
for(int i = 1; i <= n; i++)
up[i][k] = up[up[i][k - 1]][k - 1];
while (q--){
int s, e;
cin >> s >> e;
if(s == e){
cout << 0 << '\n';
continue;
}
if(st[e] <= en[s] && en[s] <= en[e]){
cout << 1 << '\n';
continue;;
}
if(en[s] > en[e]){
cout << "impossible\n";
continue;
}
int sm = 0;
for(int k = K - 1; k >= 0; k--){
if(st[up[e][k]] > en[s]){
e = up[e][k];
sm += (1 << k);
}
}
e = up[e][0];
sm++;
if(st[e] <= en[s] && en[s] <= en[e]){
cout << 1 + sm << '\n';
}else{
cout << "impossible\n";
}
}
}
signed main() {
mispertion;
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
69200 KB |
Output is correct |
2 |
Correct |
155 ms |
89936 KB |
Output is correct |
3 |
Correct |
136 ms |
89980 KB |
Output is correct |
4 |
Correct |
140 ms |
90052 KB |
Output is correct |
5 |
Correct |
141 ms |
91080 KB |
Output is correct |
6 |
Correct |
147 ms |
93128 KB |
Output is correct |
7 |
Correct |
141 ms |
93388 KB |
Output is correct |
8 |
Correct |
152 ms |
93584 KB |
Output is correct |
9 |
Correct |
151 ms |
93536 KB |
Output is correct |
10 |
Correct |
147 ms |
93664 KB |
Output is correct |
11 |
Correct |
141 ms |
93668 KB |
Output is correct |
12 |
Correct |
102 ms |
92812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
69212 KB |
Output is correct |
2 |
Correct |
35 ms |
69204 KB |
Output is correct |
3 |
Correct |
36 ms |
69292 KB |
Output is correct |
4 |
Correct |
32 ms |
69200 KB |
Output is correct |
5 |
Correct |
36 ms |
69204 KB |
Output is correct |
6 |
Correct |
33 ms |
69108 KB |
Output is correct |
7 |
Correct |
34 ms |
69212 KB |
Output is correct |
8 |
Correct |
32 ms |
69076 KB |
Output is correct |
9 |
Correct |
31 ms |
69464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
69212 KB |
Output is correct |
2 |
Correct |
35 ms |
69204 KB |
Output is correct |
3 |
Correct |
36 ms |
69292 KB |
Output is correct |
4 |
Correct |
32 ms |
69200 KB |
Output is correct |
5 |
Correct |
36 ms |
69204 KB |
Output is correct |
6 |
Correct |
33 ms |
69108 KB |
Output is correct |
7 |
Correct |
34 ms |
69212 KB |
Output is correct |
8 |
Correct |
32 ms |
69076 KB |
Output is correct |
9 |
Correct |
31 ms |
69464 KB |
Output is correct |
10 |
Correct |
32 ms |
69212 KB |
Output is correct |
11 |
Correct |
34 ms |
69188 KB |
Output is correct |
12 |
Correct |
34 ms |
69204 KB |
Output is correct |
13 |
Correct |
32 ms |
69212 KB |
Output is correct |
14 |
Correct |
33 ms |
69172 KB |
Output is correct |
15 |
Correct |
32 ms |
69464 KB |
Output is correct |
16 |
Correct |
32 ms |
69276 KB |
Output is correct |
17 |
Correct |
41 ms |
69260 KB |
Output is correct |
18 |
Correct |
32 ms |
69212 KB |
Output is correct |
19 |
Correct |
51 ms |
69972 KB |
Output is correct |
20 |
Correct |
58 ms |
70020 KB |
Output is correct |
21 |
Correct |
54 ms |
70224 KB |
Output is correct |
22 |
Correct |
47 ms |
70224 KB |
Output is correct |
23 |
Correct |
46 ms |
70228 KB |
Output is correct |
24 |
Correct |
47 ms |
70240 KB |
Output is correct |
25 |
Correct |
49 ms |
69716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
69212 KB |
Output is correct |
2 |
Correct |
35 ms |
69204 KB |
Output is correct |
3 |
Correct |
36 ms |
69292 KB |
Output is correct |
4 |
Correct |
32 ms |
69200 KB |
Output is correct |
5 |
Correct |
36 ms |
69204 KB |
Output is correct |
6 |
Correct |
33 ms |
69108 KB |
Output is correct |
7 |
Correct |
34 ms |
69212 KB |
Output is correct |
8 |
Correct |
32 ms |
69076 KB |
Output is correct |
9 |
Correct |
31 ms |
69464 KB |
Output is correct |
10 |
Correct |
31 ms |
69024 KB |
Output is correct |
11 |
Correct |
32 ms |
69200 KB |
Output is correct |
12 |
Correct |
38 ms |
69204 KB |
Output is correct |
13 |
Correct |
32 ms |
69132 KB |
Output is correct |
14 |
Correct |
34 ms |
69204 KB |
Output is correct |
15 |
Correct |
41 ms |
69208 KB |
Output is correct |
16 |
Correct |
33 ms |
69280 KB |
Output is correct |
17 |
Correct |
31 ms |
69100 KB |
Output is correct |
18 |
Correct |
31 ms |
69172 KB |
Output is correct |
19 |
Correct |
99 ms |
89548 KB |
Output is correct |
20 |
Correct |
106 ms |
89564 KB |
Output is correct |
21 |
Correct |
125 ms |
89480 KB |
Output is correct |
22 |
Correct |
129 ms |
91332 KB |
Output is correct |
23 |
Correct |
142 ms |
91332 KB |
Output is correct |
24 |
Correct |
134 ms |
91336 KB |
Output is correct |
25 |
Correct |
57 ms |
88524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
90056 KB |
Output is correct |
2 |
Correct |
127 ms |
89984 KB |
Output is correct |
3 |
Correct |
152 ms |
90072 KB |
Output is correct |
4 |
Correct |
147 ms |
91612 KB |
Output is correct |
5 |
Correct |
134 ms |
93500 KB |
Output is correct |
6 |
Correct |
161 ms |
93196 KB |
Output is correct |
7 |
Correct |
166 ms |
93380 KB |
Output is correct |
8 |
Correct |
150 ms |
93508 KB |
Output is correct |
9 |
Correct |
136 ms |
91336 KB |
Output is correct |
10 |
Correct |
156 ms |
92872 KB |
Output is correct |
11 |
Correct |
151 ms |
92656 KB |
Output is correct |
12 |
Correct |
159 ms |
92872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
69200 KB |
Output is correct |
2 |
Correct |
155 ms |
89936 KB |
Output is correct |
3 |
Correct |
136 ms |
89980 KB |
Output is correct |
4 |
Correct |
140 ms |
90052 KB |
Output is correct |
5 |
Correct |
141 ms |
91080 KB |
Output is correct |
6 |
Correct |
147 ms |
93128 KB |
Output is correct |
7 |
Correct |
141 ms |
93388 KB |
Output is correct |
8 |
Correct |
152 ms |
93584 KB |
Output is correct |
9 |
Correct |
151 ms |
93536 KB |
Output is correct |
10 |
Correct |
147 ms |
93664 KB |
Output is correct |
11 |
Correct |
141 ms |
93668 KB |
Output is correct |
12 |
Correct |
102 ms |
92812 KB |
Output is correct |
13 |
Correct |
31 ms |
69212 KB |
Output is correct |
14 |
Correct |
35 ms |
69204 KB |
Output is correct |
15 |
Correct |
36 ms |
69292 KB |
Output is correct |
16 |
Correct |
32 ms |
69200 KB |
Output is correct |
17 |
Correct |
36 ms |
69204 KB |
Output is correct |
18 |
Correct |
33 ms |
69108 KB |
Output is correct |
19 |
Correct |
34 ms |
69212 KB |
Output is correct |
20 |
Correct |
32 ms |
69076 KB |
Output is correct |
21 |
Correct |
31 ms |
69464 KB |
Output is correct |
22 |
Correct |
32 ms |
69212 KB |
Output is correct |
23 |
Correct |
34 ms |
69188 KB |
Output is correct |
24 |
Correct |
34 ms |
69204 KB |
Output is correct |
25 |
Correct |
32 ms |
69212 KB |
Output is correct |
26 |
Correct |
33 ms |
69172 KB |
Output is correct |
27 |
Correct |
32 ms |
69464 KB |
Output is correct |
28 |
Correct |
32 ms |
69276 KB |
Output is correct |
29 |
Correct |
41 ms |
69260 KB |
Output is correct |
30 |
Correct |
32 ms |
69212 KB |
Output is correct |
31 |
Correct |
51 ms |
69972 KB |
Output is correct |
32 |
Correct |
58 ms |
70020 KB |
Output is correct |
33 |
Correct |
54 ms |
70224 KB |
Output is correct |
34 |
Correct |
47 ms |
70224 KB |
Output is correct |
35 |
Correct |
46 ms |
70228 KB |
Output is correct |
36 |
Correct |
47 ms |
70240 KB |
Output is correct |
37 |
Correct |
49 ms |
69716 KB |
Output is correct |
38 |
Correct |
31 ms |
69024 KB |
Output is correct |
39 |
Correct |
32 ms |
69200 KB |
Output is correct |
40 |
Correct |
38 ms |
69204 KB |
Output is correct |
41 |
Correct |
32 ms |
69132 KB |
Output is correct |
42 |
Correct |
34 ms |
69204 KB |
Output is correct |
43 |
Correct |
41 ms |
69208 KB |
Output is correct |
44 |
Correct |
33 ms |
69280 KB |
Output is correct |
45 |
Correct |
31 ms |
69100 KB |
Output is correct |
46 |
Correct |
31 ms |
69172 KB |
Output is correct |
47 |
Correct |
99 ms |
89548 KB |
Output is correct |
48 |
Correct |
106 ms |
89564 KB |
Output is correct |
49 |
Correct |
125 ms |
89480 KB |
Output is correct |
50 |
Correct |
129 ms |
91332 KB |
Output is correct |
51 |
Correct |
142 ms |
91332 KB |
Output is correct |
52 |
Correct |
134 ms |
91336 KB |
Output is correct |
53 |
Correct |
57 ms |
88524 KB |
Output is correct |
54 |
Correct |
133 ms |
90056 KB |
Output is correct |
55 |
Correct |
127 ms |
89984 KB |
Output is correct |
56 |
Correct |
152 ms |
90072 KB |
Output is correct |
57 |
Correct |
147 ms |
91612 KB |
Output is correct |
58 |
Correct |
134 ms |
93500 KB |
Output is correct |
59 |
Correct |
161 ms |
93196 KB |
Output is correct |
60 |
Correct |
166 ms |
93380 KB |
Output is correct |
61 |
Correct |
150 ms |
93508 KB |
Output is correct |
62 |
Correct |
136 ms |
91336 KB |
Output is correct |
63 |
Correct |
156 ms |
92872 KB |
Output is correct |
64 |
Correct |
151 ms |
92656 KB |
Output is correct |
65 |
Correct |
159 ms |
92872 KB |
Output is correct |
66 |
Correct |
37 ms |
68992 KB |
Output is correct |
67 |
Correct |
134 ms |
93380 KB |
Output is correct |
68 |
Correct |
139 ms |
93220 KB |
Output is correct |
69 |
Correct |
147 ms |
93128 KB |
Output is correct |
70 |
Correct |
147 ms |
93336 KB |
Output is correct |
71 |
Correct |
150 ms |
93128 KB |
Output is correct |
72 |
Correct |
149 ms |
93284 KB |
Output is correct |
73 |
Correct |
160 ms |
93640 KB |
Output is correct |
74 |
Correct |
148 ms |
93636 KB |
Output is correct |
75 |
Correct |
137 ms |
93636 KB |
Output is correct |
76 |
Correct |
175 ms |
93640 KB |
Output is correct |
77 |
Correct |
95 ms |
92984 KB |
Output is correct |
78 |
Correct |
32 ms |
69200 KB |
Output is correct |
79 |
Correct |
33 ms |
69204 KB |
Output is correct |
80 |
Correct |
32 ms |
69200 KB |
Output is correct |
81 |
Correct |
32 ms |
69212 KB |
Output is correct |
82 |
Correct |
34 ms |
69132 KB |
Output is correct |
83 |
Correct |
35 ms |
69088 KB |
Output is correct |
84 |
Correct |
33 ms |
69240 KB |
Output is correct |
85 |
Correct |
36 ms |
69204 KB |
Output is correct |
86 |
Correct |
51 ms |
70996 KB |
Output is correct |
87 |
Correct |
55 ms |
70992 KB |
Output is correct |
88 |
Correct |
59 ms |
71248 KB |
Output is correct |
89 |
Correct |
51 ms |
71260 KB |
Output is correct |
90 |
Correct |
51 ms |
71180 KB |
Output is correct |
91 |
Correct |
53 ms |
71248 KB |
Output is correct |
92 |
Correct |
53 ms |
70748 KB |
Output is correct |
93 |
Correct |
120 ms |
91332 KB |
Output is correct |
94 |
Correct |
112 ms |
90724 KB |
Output is correct |
95 |
Correct |
107 ms |
91420 KB |
Output is correct |
96 |
Correct |
124 ms |
91296 KB |
Output is correct |
97 |
Correct |
133 ms |
91492 KB |
Output is correct |
98 |
Correct |
142 ms |
91336 KB |
Output is correct |
99 |
Correct |
55 ms |
88508 KB |
Output is correct |
100 |
Correct |
155 ms |
93380 KB |
Output is correct |
101 |
Correct |
162 ms |
93256 KB |
Output is correct |
102 |
Correct |
156 ms |
93384 KB |
Output is correct |
103 |
Correct |
177 ms |
92992 KB |
Output is correct |
104 |
Correct |
150 ms |
92772 KB |
Output is correct |
105 |
Correct |
154 ms |
92992 KB |
Output is correct |
106 |
Correct |
119 ms |
92616 KB |
Output is correct |
107 |
Correct |
135 ms |
92620 KB |
Output is correct |
108 |
Correct |
136 ms |
93432 KB |
Output is correct |
109 |
Correct |
128 ms |
93380 KB |
Output is correct |
110 |
Correct |
138 ms |
93128 KB |
Output is correct |
111 |
Correct |
149 ms |
93384 KB |
Output is correct |