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>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const int N=200'010;
const int LG=20;
struct event {
int s, e, idx;
};
event es[N];
struct Binary_Lifting {
int up[N][LG];
void add(int node, int par) {
up[node][0]=par;
for(int i = 1;i<LG;i++) {
up[node][i]=up[up[node][i-1]][i-1];
}
}
int get(int start, int num) {
if(es[start].e>=num)return 1;
int res=1;
for(int i = LG-1;i>=0;i--) {
if(es[up[start][i]].e<num) {
start=up[start][i];
res+=(1<<i);
}
}
if(es[up[start][0]].e>=num)return res+1;
return -1;
}
};
bool operator<(const event &a, const event &b) {
return a.e>b.e;
}
Binary_Lifting bl;
vector<int> child[N];
int out[N];
void dfs(int at) {
for(int to:child[at]){
bl.add(to,at);
dfs(to);
}
}
pair<int,int> T[4*N];
void update(int at, pair<int,int> val, int tl=0, int tr=N-1, int v=1) {
if(tl==tr) {
T[v]=max(T[v], val);
return;
}
int tm=(tl+tr)/2;
if(at<=tm) {
update(at, val, tl,tm,2*v);
}
else update(at, val, tm+1,tr,2*v+1);
T[v]=max(T[2*v],T[2*v+1]);
}
pair<int,int> query(int l, int r, int tl=0, int tr=N-1, int v=1) {
if(tr<l or r<tl)return {-1,-1};
if(l<=tl and tr<=r)return T[v];
int tm=(tl+tr)/2;
return max(query(l,r,tl,tm,2*v), query(l,r,tm+1,tr,2*v+1));
}
int main () {
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
map<int,int> cc;
for(int i = 0;i<n;i++) {
cin >> es[i].e >> es[i].s;
es[i].s*=-1;
es[i].e*=-1;
cc[es[i].s]=1;
cc[es[i].e]=1;
es[i].idx=i;
}
vector<event> on[2*n], off[2*n];
{
int cnt=0;
for(auto &i:cc) {
i.second=cnt++;
}
for(int i = 0;i<n;i++) {
es[i].s=cc[es[i].s];
es[i].e=cc[es[i].e];
}
}
for(int i = 0;i<n;i++) {
update(es[i].s, {es[i].e, i});
}
for(int i = 0;i<n;i++) {
pair<int,int> c=query(es[i].s, es[i].e);
if(c.first>es[i].e){
child[c.second].push_back(i);
out[i]++;
}
}
for(int i = 0;i<n;i++) {
if(out[i]==0){
bl.add(i,i);
dfs(i);
}
}
while(q--) {
int s, e;
cin >> s >> e;
swap(s,e);
if(es[s-1].s>es[e-1].s){
cout << "impossible" << "\n";
continue;
}
if(s==e) {
cout << 0 << "\n";
continue;
}
int res=bl.get(s-1,es[e-1].s);
if(res>=0)cout << res << "\n";
else cout << "impossible\n";
}
}
# | 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... |