#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#define watch(x) cout<<(#x)<<"="<<(x)<<'\n'
#define mset(d,val) memset(d,val,sizeof(d))
#define cbug if(DEBUG) cout
#define setp(x) cout<<fixed<<setprecision(x)
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
//template<typename T>
//using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
ostream& operator<<(ostream &out, ii x){ out<<"("<<x.F<<","<<x.S<<")"; return out; }
template<typename T> void amax(T &a, T b){ a=max(a,b); }
template<typename T> void amin(T &a, T b){ a=min(a,b); }
const ll INF = ll(1e18);
const int MOD = 998244353;
class PointSegmentTree{
private:
int size_;
vector<ii> v;
void update(int p, ii val, int k, int l, int r)
{
if(p < l || r < p) return;
if(l == r){
v[k]=val; //modification
return;
}
int mid = (l+r)>>1;
update(p, val, k*2, l, mid);
update(p, val, k*2+1, mid+1, r);
v[k] = merge(v[k*2], v[k*2+1]);
}
ii query(int s, int e, int k, int l, int r)
{
if(e < l || r < s) return {-1,-1}; //dummy value
if(s <= l && r <= e) return v[k];
int mid = (l+r)>>1;
ii lc = query(s, e, k*2, l, mid);
ii rc = query(s, e, k*2+1, mid+1, r);
return merge(lc, rc);
}
public:
PointSegmentTree(): v(vector<ii>()) {}
PointSegmentTree(int n){
for(size_=1;size_<n;) size_<<=1;
v.resize(size_*4,{-1,-1});
}
//void reset(){}
inline ii merge(ii x, ii y){
return max(x,y);
}
inline void update(int p, ii val){
update(p, val, 1, 0, size_-1);
}
inline ii query(int l, int r){
return query(l, r, 1, 0, size_-1);
}
};
const bool DEBUG = 0;
const int MAXN = 100005;
const int MAXA = MAXN * 2;
const int LG = 20;
int n,Q;
int s[MAXN], e[MAXN]; //reversed
ii mx[MAXA];
int nxt[MAXN][LG];
PointSegmentTree st;
vi comp;
int getid(int x){ return sz(comp) - 1 - (lower_bound(all(comp), x) - comp.begin()); }
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
mset(nxt,-1);
cin>>n>>Q;
forn(i,0,n)
{
cin>>e[i]>>s[i];
comp.pb(s[i]);
comp.pb(e[i]);
}
// compress everything in reverse
sort(all(comp));
forn(i,0,n)
{
s[i]=getid(s[i]);
e[i]=getid(e[i]);
}
// compute first next
forn(i,0,MAXA) mx[i]={-1,-1};
forn(i,0,n) amax(mx[s[i]], {e[i], i});
st=PointSegmentTree(MAXA);
forn(i,0,MAXA) st.update(i, mx[i]);
forn(i,0,n)
{
nxt[i][0] = st.query(s[i], e[i]).S;
}
// binary lifting
forn(j,1,LG) forn(i,0,n) if(nxt[i][j-1]!=-1) nxt[i][j] = nxt[nxt[i][j-1]][j-1];
forn(z,0,Q)
{
int u,v; cin>>v>>u; u--; v--;
if(u==v)
{
cout<<0<<'\n';
continue;
}
if(s[v]<s[u])
{
cout<<"impossible\n";
continue;
}
if(s[u]<=s[v] && s[v]<=e[u])
{
cout<<1<<'\n';
continue;
}
int ans=2;
for(int j=LG-1;j>=0;j--)
{
// cout<<"u="<<u<<" j="<<j<<" nxt="<<nxt[u][j]<<'\n';
if(nxt[u][j]!=-1 && e[nxt[u][j]]<s[v])
{
u=nxt[u][j];
ans+=(1<<j);
// cout<<"ans="<<ans<<'\n';
}
}
u=nxt[u][0];
assert(u!=-1);
if(e[u]>=s[v]) cout<<ans<<'\n';
else cout<<"impossible\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
17912 KB |
Output is correct |
2 |
Correct |
145 ms |
20216 KB |
Output is correct |
3 |
Correct |
188 ms |
20224 KB |
Output is correct |
4 |
Correct |
168 ms |
20084 KB |
Output is correct |
5 |
Correct |
151 ms |
20180 KB |
Output is correct |
6 |
Correct |
177 ms |
20260 KB |
Output is correct |
7 |
Correct |
163 ms |
20220 KB |
Output is correct |
8 |
Correct |
135 ms |
20604 KB |
Output is correct |
9 |
Correct |
135 ms |
21064 KB |
Output is correct |
10 |
Correct |
154 ms |
20952 KB |
Output is correct |
11 |
Correct |
154 ms |
20992 KB |
Output is correct |
12 |
Correct |
105 ms |
21128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
17876 KB |
Output is correct |
2 |
Correct |
40 ms |
17876 KB |
Output is correct |
3 |
Correct |
41 ms |
17876 KB |
Output is correct |
4 |
Correct |
38 ms |
17876 KB |
Output is correct |
5 |
Correct |
36 ms |
17876 KB |
Output is correct |
6 |
Correct |
37 ms |
17876 KB |
Output is correct |
7 |
Correct |
36 ms |
17876 KB |
Output is correct |
8 |
Correct |
37 ms |
17876 KB |
Output is correct |
9 |
Correct |
38 ms |
17876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
17876 KB |
Output is correct |
2 |
Correct |
40 ms |
17876 KB |
Output is correct |
3 |
Correct |
41 ms |
17876 KB |
Output is correct |
4 |
Correct |
38 ms |
17876 KB |
Output is correct |
5 |
Correct |
36 ms |
17876 KB |
Output is correct |
6 |
Correct |
37 ms |
17876 KB |
Output is correct |
7 |
Correct |
36 ms |
17876 KB |
Output is correct |
8 |
Correct |
37 ms |
17876 KB |
Output is correct |
9 |
Correct |
38 ms |
17876 KB |
Output is correct |
10 |
Correct |
35 ms |
17904 KB |
Output is correct |
11 |
Correct |
40 ms |
17836 KB |
Output is correct |
12 |
Correct |
39 ms |
17932 KB |
Output is correct |
13 |
Correct |
40 ms |
18004 KB |
Output is correct |
14 |
Correct |
36 ms |
17972 KB |
Output is correct |
15 |
Correct |
36 ms |
17912 KB |
Output is correct |
16 |
Correct |
36 ms |
17876 KB |
Output is correct |
17 |
Correct |
43 ms |
17876 KB |
Output is correct |
18 |
Correct |
44 ms |
18004 KB |
Output is correct |
19 |
Correct |
68 ms |
19516 KB |
Output is correct |
20 |
Correct |
66 ms |
19608 KB |
Output is correct |
21 |
Correct |
65 ms |
19780 KB |
Output is correct |
22 |
Correct |
63 ms |
19860 KB |
Output is correct |
23 |
Correct |
60 ms |
19620 KB |
Output is correct |
24 |
Correct |
60 ms |
19744 KB |
Output is correct |
25 |
Correct |
64 ms |
19352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
17876 KB |
Output is correct |
2 |
Correct |
40 ms |
17876 KB |
Output is correct |
3 |
Correct |
41 ms |
17876 KB |
Output is correct |
4 |
Correct |
38 ms |
17876 KB |
Output is correct |
5 |
Correct |
36 ms |
17876 KB |
Output is correct |
6 |
Correct |
37 ms |
17876 KB |
Output is correct |
7 |
Correct |
36 ms |
17876 KB |
Output is correct |
8 |
Correct |
37 ms |
17876 KB |
Output is correct |
9 |
Correct |
38 ms |
17876 KB |
Output is correct |
10 |
Correct |
36 ms |
17876 KB |
Output is correct |
11 |
Correct |
36 ms |
17876 KB |
Output is correct |
12 |
Correct |
37 ms |
17916 KB |
Output is correct |
13 |
Correct |
37 ms |
17980 KB |
Output is correct |
14 |
Correct |
38 ms |
17956 KB |
Output is correct |
15 |
Correct |
39 ms |
17876 KB |
Output is correct |
16 |
Correct |
37 ms |
17904 KB |
Output is correct |
17 |
Correct |
37 ms |
17924 KB |
Output is correct |
18 |
Correct |
38 ms |
17856 KB |
Output is correct |
19 |
Correct |
118 ms |
19912 KB |
Output is correct |
20 |
Correct |
122 ms |
20000 KB |
Output is correct |
21 |
Correct |
123 ms |
21272 KB |
Output is correct |
22 |
Correct |
118 ms |
21464 KB |
Output is correct |
23 |
Correct |
129 ms |
21444 KB |
Output is correct |
24 |
Correct |
124 ms |
21448 KB |
Output is correct |
25 |
Correct |
80 ms |
20832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
154 ms |
20124 KB |
Output is correct |
2 |
Correct |
145 ms |
20128 KB |
Output is correct |
3 |
Correct |
199 ms |
20132 KB |
Output is correct |
4 |
Correct |
137 ms |
20728 KB |
Output is correct |
5 |
Correct |
160 ms |
20472 KB |
Output is correct |
6 |
Correct |
172 ms |
20336 KB |
Output is correct |
7 |
Correct |
169 ms |
20320 KB |
Output is correct |
8 |
Correct |
159 ms |
20380 KB |
Output is correct |
9 |
Correct |
124 ms |
19616 KB |
Output is correct |
10 |
Correct |
152 ms |
19964 KB |
Output is correct |
11 |
Correct |
169 ms |
19748 KB |
Output is correct |
12 |
Correct |
161 ms |
19956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
17912 KB |
Output is correct |
2 |
Correct |
145 ms |
20216 KB |
Output is correct |
3 |
Correct |
188 ms |
20224 KB |
Output is correct |
4 |
Correct |
168 ms |
20084 KB |
Output is correct |
5 |
Correct |
151 ms |
20180 KB |
Output is correct |
6 |
Correct |
177 ms |
20260 KB |
Output is correct |
7 |
Correct |
163 ms |
20220 KB |
Output is correct |
8 |
Correct |
135 ms |
20604 KB |
Output is correct |
9 |
Correct |
135 ms |
21064 KB |
Output is correct |
10 |
Correct |
154 ms |
20952 KB |
Output is correct |
11 |
Correct |
154 ms |
20992 KB |
Output is correct |
12 |
Correct |
105 ms |
21128 KB |
Output is correct |
13 |
Correct |
37 ms |
17876 KB |
Output is correct |
14 |
Correct |
40 ms |
17876 KB |
Output is correct |
15 |
Correct |
41 ms |
17876 KB |
Output is correct |
16 |
Correct |
38 ms |
17876 KB |
Output is correct |
17 |
Correct |
36 ms |
17876 KB |
Output is correct |
18 |
Correct |
37 ms |
17876 KB |
Output is correct |
19 |
Correct |
36 ms |
17876 KB |
Output is correct |
20 |
Correct |
37 ms |
17876 KB |
Output is correct |
21 |
Correct |
38 ms |
17876 KB |
Output is correct |
22 |
Correct |
35 ms |
17904 KB |
Output is correct |
23 |
Correct |
40 ms |
17836 KB |
Output is correct |
24 |
Correct |
39 ms |
17932 KB |
Output is correct |
25 |
Correct |
40 ms |
18004 KB |
Output is correct |
26 |
Correct |
36 ms |
17972 KB |
Output is correct |
27 |
Correct |
36 ms |
17912 KB |
Output is correct |
28 |
Correct |
36 ms |
17876 KB |
Output is correct |
29 |
Correct |
43 ms |
17876 KB |
Output is correct |
30 |
Correct |
44 ms |
18004 KB |
Output is correct |
31 |
Correct |
68 ms |
19516 KB |
Output is correct |
32 |
Correct |
66 ms |
19608 KB |
Output is correct |
33 |
Correct |
65 ms |
19780 KB |
Output is correct |
34 |
Correct |
63 ms |
19860 KB |
Output is correct |
35 |
Correct |
60 ms |
19620 KB |
Output is correct |
36 |
Correct |
60 ms |
19744 KB |
Output is correct |
37 |
Correct |
64 ms |
19352 KB |
Output is correct |
38 |
Correct |
36 ms |
17876 KB |
Output is correct |
39 |
Correct |
36 ms |
17876 KB |
Output is correct |
40 |
Correct |
37 ms |
17916 KB |
Output is correct |
41 |
Correct |
37 ms |
17980 KB |
Output is correct |
42 |
Correct |
38 ms |
17956 KB |
Output is correct |
43 |
Correct |
39 ms |
17876 KB |
Output is correct |
44 |
Correct |
37 ms |
17904 KB |
Output is correct |
45 |
Correct |
37 ms |
17924 KB |
Output is correct |
46 |
Correct |
38 ms |
17856 KB |
Output is correct |
47 |
Correct |
118 ms |
19912 KB |
Output is correct |
48 |
Correct |
122 ms |
20000 KB |
Output is correct |
49 |
Correct |
123 ms |
21272 KB |
Output is correct |
50 |
Correct |
118 ms |
21464 KB |
Output is correct |
51 |
Correct |
129 ms |
21444 KB |
Output is correct |
52 |
Correct |
124 ms |
21448 KB |
Output is correct |
53 |
Correct |
80 ms |
20832 KB |
Output is correct |
54 |
Correct |
154 ms |
20124 KB |
Output is correct |
55 |
Correct |
145 ms |
20128 KB |
Output is correct |
56 |
Correct |
199 ms |
20132 KB |
Output is correct |
57 |
Correct |
137 ms |
20728 KB |
Output is correct |
58 |
Correct |
160 ms |
20472 KB |
Output is correct |
59 |
Correct |
172 ms |
20336 KB |
Output is correct |
60 |
Correct |
169 ms |
20320 KB |
Output is correct |
61 |
Correct |
159 ms |
20380 KB |
Output is correct |
62 |
Correct |
124 ms |
19616 KB |
Output is correct |
63 |
Correct |
152 ms |
19964 KB |
Output is correct |
64 |
Correct |
169 ms |
19748 KB |
Output is correct |
65 |
Correct |
161 ms |
19956 KB |
Output is correct |
66 |
Correct |
37 ms |
17908 KB |
Output is correct |
67 |
Correct |
145 ms |
23220 KB |
Output is correct |
68 |
Correct |
160 ms |
23252 KB |
Output is correct |
69 |
Correct |
179 ms |
23172 KB |
Output is correct |
70 |
Correct |
155 ms |
23348 KB |
Output is correct |
71 |
Correct |
168 ms |
23348 KB |
Output is correct |
72 |
Correct |
156 ms |
23396 KB |
Output is correct |
73 |
Correct |
135 ms |
23620 KB |
Output is correct |
74 |
Correct |
134 ms |
23672 KB |
Output is correct |
75 |
Correct |
179 ms |
23688 KB |
Output is correct |
76 |
Correct |
148 ms |
23620 KB |
Output is correct |
77 |
Correct |
110 ms |
22980 KB |
Output is correct |
78 |
Correct |
44 ms |
17812 KB |
Output is correct |
79 |
Correct |
38 ms |
17876 KB |
Output is correct |
80 |
Correct |
36 ms |
17876 KB |
Output is correct |
81 |
Correct |
36 ms |
17972 KB |
Output is correct |
82 |
Correct |
37 ms |
17876 KB |
Output is correct |
83 |
Correct |
38 ms |
17916 KB |
Output is correct |
84 |
Correct |
36 ms |
17916 KB |
Output is correct |
85 |
Correct |
39 ms |
17876 KB |
Output is correct |
86 |
Correct |
65 ms |
19532 KB |
Output is correct |
87 |
Correct |
80 ms |
19596 KB |
Output is correct |
88 |
Correct |
70 ms |
19788 KB |
Output is correct |
89 |
Correct |
60 ms |
19844 KB |
Output is correct |
90 |
Correct |
62 ms |
19700 KB |
Output is correct |
91 |
Correct |
72 ms |
19668 KB |
Output is correct |
92 |
Correct |
67 ms |
19312 KB |
Output is correct |
93 |
Correct |
113 ms |
21496 KB |
Output is correct |
94 |
Correct |
108 ms |
20648 KB |
Output is correct |
95 |
Correct |
130 ms |
21468 KB |
Output is correct |
96 |
Correct |
132 ms |
21516 KB |
Output is correct |
97 |
Correct |
142 ms |
21576 KB |
Output is correct |
98 |
Correct |
123 ms |
21512 KB |
Output is correct |
99 |
Correct |
81 ms |
20808 KB |
Output is correct |
100 |
Correct |
176 ms |
23368 KB |
Output is correct |
101 |
Correct |
176 ms |
23360 KB |
Output is correct |
102 |
Correct |
173 ms |
23480 KB |
Output is correct |
103 |
Correct |
153 ms |
22984 KB |
Output is correct |
104 |
Correct |
152 ms |
22604 KB |
Output is correct |
105 |
Correct |
154 ms |
23040 KB |
Output is correct |
106 |
Correct |
196 ms |
22628 KB |
Output is correct |
107 |
Correct |
163 ms |
22804 KB |
Output is correct |
108 |
Correct |
151 ms |
23316 KB |
Output is correct |
109 |
Correct |
161 ms |
23272 KB |
Output is correct |
110 |
Correct |
156 ms |
23416 KB |
Output is correct |
111 |
Correct |
185 ms |
23240 KB |
Output is correct |