/*#pragma GCC optimize("Ofast,unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")*/
/*#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")*/
#include <bits/stdc++.h>
using namespace std;
using ll=long long int;
using ld=long double;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
using point=complex<double>;
#define F first
#define S second
//#define X real()
//#define Y imag()
#define pb push_back
#define mp make_pair
#define lc v<<1
#define rc v<<1|1
#define fast_io ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
#define kill(x) cout << x << '\n';exit(0)
#define killshayan kill("done!")
#define killmashtali kill("Hello, World!")
const int N=2e5+10,LN=20,M=1e6+10,SQ=700,BS=737,inf=1.05e9,NSQ=N/SQ+3;
const ll INF=1e18;
const double pi=acos(-1);
const ld ep=1e-7;
const int MH=1000696969,MD=998244353,MOD=1000000007;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pii, null_type,greater<pii>, rb_tree_tag,tree_order_statistics_node_update>
ll pow(ll x, ll y, ll mod){
ll ans=1;
while (y != 0) {
if (y & 1) ans = ans * x % mod;
y >>= 1;
x = x * x % mod;
}
return ans;
}
int n,q,dp[LN][N],L[N],R[N],k;
pii f[N*4];
vector<int> cp;
void build(int v, int l, int r){
f[v]={inf,inf};
if(r-l>1){
int m=(l+r)>>1;
build(lc,l,m);
build(rc,m,r);
}
}
pii get(int v, int l, int r, int tl, int tr){
if(l>=tr || tl>=r) return {inf,inf};
if(l>=tl && r<=tr) return f[v];
int m=(l+r)>>1;
return min(get(lc,l,m,tl,tr),get(rc,m,r,tl,tr));
}
void upd(int v, int l, int r, int p, pii x){
if(r-l==1){
f[v]=min(f[v],x);
return;
}
int m=(l+r)>>1;
if(p<m) upd(lc,l,m,p,x);
else upd(rc,m,r,p,x);
f[v]=min(f[lc],f[rc]);
}
int main(){
fast_io;
cin >> n >> q;
for(int i=1; i<=n; i++){
cin >> L[i] >> R[i];
cp.pb(L[i]);
cp.pb(R[i]);
}
sort(cp.begin(),cp.end());
cp.resize(unique(cp.begin(),cp.end())-cp.begin());
k=cp.size();
build(1,1,k+1);
for(int i=1; i<=n; i++){
L[i]=lower_bound(cp.begin(),cp.end(),L[i])-cp.begin()+1;
R[i]=lower_bound(cp.begin(),cp.end(),R[i])-cp.begin()+1;
upd(1,1,k+1,R[i],{L[i],i});
}
for(int i=1; i<=n; i++){
dp[0][i]=get(1,1,k+1,L[i],R[i]+1).S;
}
for(int i=1; i<LN; i++){
for(int j=1; j<=n; j++){
dp[i][j]=dp[i-1][dp[i-1][j]];
}
}
for(int i=1; i<=q; i++){
int s,e,ans=0;
cin >> s >> e;
if(s==e){
cout << 0 << '\n';
continue;
}
if(R[s]>=L[e] && R[s]<=R[e]){
cout << 1 << '\n';
continue;
}
if(R[s]>R[e]){
cout << "impossible\n";
continue;
}
for(int j=LN; j--; ){
if(L[dp[j][e]]>R[s]){
ans+=(1<<j);
e=dp[j][e];
}
}
if(ans>n) cout << "impossible\n";
else cout << ans+2 << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
125 ms |
12744 KB |
Output is correct |
3 |
Correct |
130 ms |
13200 KB |
Output is correct |
4 |
Correct |
174 ms |
13108 KB |
Output is correct |
5 |
Correct |
142 ms |
13088 KB |
Output is correct |
6 |
Correct |
137 ms |
13224 KB |
Output is correct |
7 |
Correct |
146 ms |
13104 KB |
Output is correct |
8 |
Correct |
155 ms |
15776 KB |
Output is correct |
9 |
Correct |
150 ms |
15964 KB |
Output is correct |
10 |
Correct |
168 ms |
13636 KB |
Output is correct |
11 |
Correct |
153 ms |
15712 KB |
Output is correct |
12 |
Correct |
77 ms |
13376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
416 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
31 ms |
1808 KB |
Output is correct |
20 |
Correct |
41 ms |
1624 KB |
Output is correct |
21 |
Correct |
31 ms |
1816 KB |
Output is correct |
22 |
Correct |
29 ms |
2088 KB |
Output is correct |
23 |
Correct |
25 ms |
1976 KB |
Output is correct |
24 |
Correct |
26 ms |
2056 KB |
Output is correct |
25 |
Correct |
30 ms |
1532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
496 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
100 ms |
13476 KB |
Output is correct |
20 |
Correct |
92 ms |
12228 KB |
Output is correct |
21 |
Correct |
107 ms |
12284 KB |
Output is correct |
22 |
Correct |
99 ms |
14300 KB |
Output is correct |
23 |
Correct |
132 ms |
14544 KB |
Output is correct |
24 |
Correct |
120 ms |
14928 KB |
Output is correct |
25 |
Correct |
27 ms |
10044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
12896 KB |
Output is correct |
2 |
Correct |
134 ms |
12724 KB |
Output is correct |
3 |
Correct |
171 ms |
12944 KB |
Output is correct |
4 |
Correct |
153 ms |
15268 KB |
Output is correct |
5 |
Correct |
146 ms |
13140 KB |
Output is correct |
6 |
Correct |
154 ms |
14988 KB |
Output is correct |
7 |
Correct |
158 ms |
14964 KB |
Output is correct |
8 |
Correct |
166 ms |
15204 KB |
Output is correct |
9 |
Correct |
117 ms |
14320 KB |
Output is correct |
10 |
Correct |
152 ms |
14992 KB |
Output is correct |
11 |
Correct |
159 ms |
14836 KB |
Output is correct |
12 |
Correct |
149 ms |
14608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
125 ms |
12744 KB |
Output is correct |
3 |
Correct |
130 ms |
13200 KB |
Output is correct |
4 |
Correct |
174 ms |
13108 KB |
Output is correct |
5 |
Correct |
142 ms |
13088 KB |
Output is correct |
6 |
Correct |
137 ms |
13224 KB |
Output is correct |
7 |
Correct |
146 ms |
13104 KB |
Output is correct |
8 |
Correct |
155 ms |
15776 KB |
Output is correct |
9 |
Correct |
150 ms |
15964 KB |
Output is correct |
10 |
Correct |
168 ms |
13636 KB |
Output is correct |
11 |
Correct |
153 ms |
15712 KB |
Output is correct |
12 |
Correct |
77 ms |
13376 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
416 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
31 ms |
1808 KB |
Output is correct |
32 |
Correct |
41 ms |
1624 KB |
Output is correct |
33 |
Correct |
31 ms |
1816 KB |
Output is correct |
34 |
Correct |
29 ms |
2088 KB |
Output is correct |
35 |
Correct |
25 ms |
1976 KB |
Output is correct |
36 |
Correct |
26 ms |
2056 KB |
Output is correct |
37 |
Correct |
30 ms |
1532 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
512 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
496 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
100 ms |
13476 KB |
Output is correct |
48 |
Correct |
92 ms |
12228 KB |
Output is correct |
49 |
Correct |
107 ms |
12284 KB |
Output is correct |
50 |
Correct |
99 ms |
14300 KB |
Output is correct |
51 |
Correct |
132 ms |
14544 KB |
Output is correct |
52 |
Correct |
120 ms |
14928 KB |
Output is correct |
53 |
Correct |
27 ms |
10044 KB |
Output is correct |
54 |
Correct |
119 ms |
12896 KB |
Output is correct |
55 |
Correct |
134 ms |
12724 KB |
Output is correct |
56 |
Correct |
171 ms |
12944 KB |
Output is correct |
57 |
Correct |
153 ms |
15268 KB |
Output is correct |
58 |
Correct |
146 ms |
13140 KB |
Output is correct |
59 |
Correct |
154 ms |
14988 KB |
Output is correct |
60 |
Correct |
158 ms |
14964 KB |
Output is correct |
61 |
Correct |
166 ms |
15204 KB |
Output is correct |
62 |
Correct |
117 ms |
14320 KB |
Output is correct |
63 |
Correct |
152 ms |
14992 KB |
Output is correct |
64 |
Correct |
159 ms |
14836 KB |
Output is correct |
65 |
Correct |
149 ms |
14608 KB |
Output is correct |
66 |
Correct |
0 ms |
340 KB |
Output is correct |
67 |
Correct |
118 ms |
12900 KB |
Output is correct |
68 |
Correct |
129 ms |
13292 KB |
Output is correct |
69 |
Correct |
164 ms |
12796 KB |
Output is correct |
70 |
Correct |
168 ms |
13220 KB |
Output is correct |
71 |
Correct |
139 ms |
13000 KB |
Output is correct |
72 |
Correct |
143 ms |
13504 KB |
Output is correct |
73 |
Correct |
155 ms |
15356 KB |
Output is correct |
74 |
Correct |
160 ms |
15644 KB |
Output is correct |
75 |
Correct |
160 ms |
13448 KB |
Output is correct |
76 |
Correct |
142 ms |
15268 KB |
Output is correct |
77 |
Correct |
81 ms |
13188 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
2 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
596 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
32 ms |
1612 KB |
Output is correct |
87 |
Correct |
35 ms |
1580 KB |
Output is correct |
88 |
Correct |
31 ms |
1836 KB |
Output is correct |
89 |
Correct |
30 ms |
2088 KB |
Output is correct |
90 |
Correct |
26 ms |
1868 KB |
Output is correct |
91 |
Correct |
24 ms |
1888 KB |
Output is correct |
92 |
Correct |
41 ms |
1480 KB |
Output is correct |
93 |
Correct |
90 ms |
12100 KB |
Output is correct |
94 |
Correct |
92 ms |
12008 KB |
Output is correct |
95 |
Correct |
110 ms |
12036 KB |
Output is correct |
96 |
Correct |
95 ms |
14148 KB |
Output is correct |
97 |
Correct |
118 ms |
14484 KB |
Output is correct |
98 |
Correct |
118 ms |
14188 KB |
Output is correct |
99 |
Correct |
28 ms |
10216 KB |
Output is correct |
100 |
Correct |
156 ms |
15060 KB |
Output is correct |
101 |
Correct |
165 ms |
15464 KB |
Output is correct |
102 |
Correct |
143 ms |
16480 KB |
Output is correct |
103 |
Correct |
148 ms |
16388 KB |
Output is correct |
104 |
Correct |
154 ms |
15548 KB |
Output is correct |
105 |
Correct |
146 ms |
16516 KB |
Output is correct |
106 |
Correct |
130 ms |
14708 KB |
Output is correct |
107 |
Correct |
144 ms |
15056 KB |
Output is correct |
108 |
Correct |
153 ms |
14664 KB |
Output is correct |
109 |
Correct |
158 ms |
14160 KB |
Output is correct |
110 |
Correct |
153 ms |
15420 KB |
Output is correct |
111 |
Correct |
157 ms |
15136 KB |
Output is correct |