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;
#define ll long long
#define pii pair<ll, ll>
#define f first
#define s second
#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
#define FOR(i,a,b) for (int i = a; i<b; ++i)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T,typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
const int maxn = 2e5+5;
vector<pii> to[maxn]; // {lpos,id}
vector<pii> ask[maxn]; // for each seg id, the left pos and the queryid
int ans[maxn];
int fa[18][maxn];
struct seg{
int l,r,i;
bool operator < (seg o) {
return r < o.r;
}
};
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
bug(1,2);
int n,q; cin>>n>>q;
vector<pii>v(n);
vector<ll>xpos;
REP(i,n) {
cin>>v[i].f>>v[i].s;
xpos.pb(v[i].f);
xpos.pb(v[i].s);
}
sort(ALL(xpos)); xpos.resize(unique(ALL(xpos)) - xpos.begin());
auto getx = [&](ll x) {return lower_bound(ALL(xpos), x)-xpos.begin();};
REP(i,n) {
v[i].f = getx(v[i].f);
v[i].s = getx(v[i].s);
to[v[i].s].pb({v[i].f,i});
}
REP(i,q) {
int s,e; cin>>s>>e; --s; --e;
if (s == e) {
ans[i] = 0; continue;
}
ask[e].pb({v[s].s,i});
}
vector<seg> now;
REP(r, maxn) {
sort(ALL(to[r]));
for (pii T : to[r]) {
int l= T.f, i =T.s;
while (SZ(now) && l <= now[SZ(now)-1].l) now.pop_back();
if (SZ(now) && now.back().r >= l) {
fa[0][i] = lower_bound(ALL(now), seg{l,l,i})->i;
}else{
fa[0][i] = i;
}
for (int j = 1; j<18; ++j) {
fa[j][i] = fa[j-1][fa[j-1][i]];
}
now.pb({l,r,i});
for (pii qq : ask[i]) {
if (qq.f > r) {
ans[qq.s] = -1; continue;
}
int steps =0;
int at = i;
for (int j = 17; j>=0; --j) {
if (v[fa[j][at]].f > qq.f) {
steps += (1<<j);
at = fa[j][at];
}
}
if (v[at].f > qq.f) {
at = fa[0][at]; ++steps;
}
if (v[at].f > qq.f) {
ans[qq.s] = -1;
}else{
ans[qq.s] = steps+1;
}
}
}
}
REP(i,q) {
if (ans[i] == -1) cout<<"impossible"<<endl;
else cout<<ans[i]<<endl;
}
}
# | 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... |