This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#pragma GCC target("sse,sse2,avx2")
#pragma GCC optimize("unroll-loops,O3")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 1e5+20,mod = 1e9+7,inf = 1e9+10,sq = 500;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k >>= 1;
}
return z;
}
int s[N],e[N],sp[N][20],seg[N*4];
vector<pll> ve;
bool cmp(pll i,pll j){
if (e[i.Y] == e[j.Y]) return (s[i.Y] < s[j.Y]);
return (e[i.Y] < e[j.Y]);
}
void build(int l,int r,int v = 1){
if (r-l == 1){
seg[v] = ve[l].Y;
return;
}
int u = (v << 1),m = (l+r) >> 1;
build(l,m,u);
build(m,r,u|1);
if (s[seg[u]] < s[seg[u|1]]) seg[v] = seg[u];
else seg[v] = seg[u|1];
}
int que(int l,int r,int S,int E,int v = 1){
if (S == l && r == E) return seg[v];
int m = (l+r) >> 1,u = (v << 1);
if (E <= m) return que(l,m,S,E,u);
if (S >= m) return que(m,r,S,E,u|1);
int x = que(l,m,S,m,u),y = que(m,r,m,E,u|1);
if (s[x] < s[y]) return x;
return y;
}
int main(){
ios :: sync_with_stdio(0); cin.tie(0);
int n,q;
cin >> n >> q;
rep(i,1,n+1){
cin >> s[i] >> e[i];
ve.pb({e[i],i});
}
sort(all(ve),cmp);
build(0,n,1);
rep(i,0,n){
int j = ve[i].Y;
int ind = lower_bound(all(ve),make_pair(s[j],0))-ve.begin();
sp[j][0] = que(0,n,ind,i+1,1);
}
rep(j,1,20){
rep(i,1,n+1){
sp[i][j] = sp[sp[i][j-1]][j-1];
}
}
while (q--){
int a,b;
cin >> a >> b;
if (a == b){
cout << 0 << endl;
continue;
}
if (e[a] > e[b] || s[sp[b][19]] > s[a]){
cout << "impossible" << endl;
continue;
}
if (e[a] == e[b]){
cout << 1 << endl;
continue;
}
int x = 0;
repr(i,19,0){
if (s[sp[b][i]] > e[a]){
x += (1 << i);
b = sp[b][i];
}
}
x++;
if (e[a] < s[b]) x++;
cout << x << endl;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |