#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<int> v;
void update(int p, int 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]);
}
int query(int s, int e, int k, int l, int r)
{
if(e < l || r < s) return 0; //dummy value
if(s <= l && r <= e) return v[k];
int mid = (l+r)>>1;
int lc = query(s, e, k*2, l, mid);
int rc = query(s, e, k*2+1, mid+1, r);
return merge(lc, rc);
}
public:
PointSegmentTree(): v(vector<int>()) {}
PointSegmentTree(int n){
for(size_=1;size_<n;) size_<<=1;
v.resize(size_*4);
}
//void reset(){}
inline int merge(int x, int y){
return max(x, y);
}
inline void update(int p, int val){
update(p, val, 1, 0, size_-1);
}
inline int 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
vii a,q,q0;
int nxt[MAXA][LG];
PointSegmentTree st[LG];
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]);
a.pb({s[i], e[i]});
}
forn(z,0,Q)
{
int u,v; cin>>u>>v; u--; v--;
q0.pb({v, u});
q.pb({s[v], s[u]});
}
// compress everything in reverse
sort(all(comp));
forn(i,0,n)
{
s[i]=getid(s[i]);
e[i]=getid(e[i]);
a[i]={getid(a[i].F), getid(a[i].S)};
}
forn(i,0,Q) q[i]={getid(q[i].F), getid(q[i].S)};
forn(i,0,MAXA) forn(j,0,LG) nxt[i][j]=i;
forn(i,0,n) amax(nxt[s[i]][0], e[i]);
forn(i,0,LG) st[i]=PointSegmentTree(MAXA);
forn(i,0,MAXA) st[0].update(i, nxt[i][0]);
for(int i=MAXA-1;i>=0;i--)
{
forn(j,1,LG)
{
nxt[i][j] = st[j-1].query(i, nxt[i][j-1]);
st[j].update(i, nxt[i][j]);
}
}
forn(z,0,Q)
{
if(q0[z].F==q0[z].S)
{
cout<<0<<'\n';
continue;
}
if(e[q0[z].F]>e[q0[z].S])
{
cout<<"impossible\n";
continue;
}
int u=q[z].F, v=q[z].S;
int ans=0;
for(int j=LG-1;j>=0;j--)
{
if(nxt[u][j]<v)
{
u=nxt[u][j];
ans+=(1<<j);
}
}
u=nxt[u][0];
ans++;
if(u>=v) cout<<ans<<'\n';
else cout<<"impossible\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
637 ms |
98144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
725 ms |
98244 KB |
Output is correct |
2 |
Incorrect |
667 ms |
98132 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
725 ms |
98244 KB |
Output is correct |
2 |
Incorrect |
667 ms |
98132 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
725 ms |
98244 KB |
Output is correct |
2 |
Incorrect |
667 ms |
98132 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
918 ms |
102704 KB |
Output is correct |
2 |
Correct |
948 ms |
102700 KB |
Output is correct |
3 |
Correct |
942 ms |
102700 KB |
Output is correct |
4 |
Correct |
748 ms |
103212 KB |
Output is correct |
5 |
Correct |
966 ms |
103104 KB |
Output is correct |
6 |
Incorrect |
930 ms |
104232 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
637 ms |
98144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |