답안 #604066

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
604066 2022-07-24T16:29:47 Z Evirir Event Hopping (BOI22_events) C++17
20 / 100
198 ms 23572 KB
#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) 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(e[v]<e[u])
		{
			cout<<"impossible\n";
			continue;
		}
		if(s[v]<=e[u] && e[u]<=e[v])
		{
			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(e[nxt[u][j]]<s[v])
			{
				u=nxt[u][j];
				ans+=(1<<j);
				// cout<<"ans="<<ans<<'\n';
			}
		}
		u=nxt[u][1];
		
		if(e[u]>=s[v]) cout<<ans<<'\n';
		else cout<<"impossible\n";
	}
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 17876 KB Output is correct
2 Correct 139 ms 20516 KB Output is correct
3 Correct 152 ms 20492 KB Output is correct
4 Correct 166 ms 20552 KB Output is correct
5 Correct 165 ms 20600 KB Output is correct
6 Correct 154 ms 20680 KB Output is correct
7 Correct 160 ms 20644 KB Output is correct
8 Incorrect 133 ms 20980 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 39 ms 17876 KB Output is correct
5 Correct 38 ms 17912 KB Output is correct
6 Incorrect 46 ms 17912 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 39 ms 17876 KB Output is correct
5 Correct 38 ms 17912 KB Output is correct
6 Incorrect 46 ms 17912 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 39 ms 17876 KB Output is correct
5 Correct 38 ms 17912 KB Output is correct
6 Incorrect 46 ms 17912 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 137 ms 20484 KB Output is correct
2 Correct 174 ms 20584 KB Output is correct
3 Correct 171 ms 20556 KB Output is correct
4 Correct 136 ms 21112 KB Output is correct
5 Correct 151 ms 20980 KB Output is correct
6 Correct 161 ms 21188 KB Output is correct
7 Correct 159 ms 23344 KB Output is correct
8 Correct 158 ms 23572 KB Output is correct
9 Correct 131 ms 21416 KB Output is correct
10 Correct 171 ms 23044 KB Output is correct
11 Correct 198 ms 22768 KB Output is correct
12 Correct 158 ms 22924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 17876 KB Output is correct
2 Correct 139 ms 20516 KB Output is correct
3 Correct 152 ms 20492 KB Output is correct
4 Correct 166 ms 20552 KB Output is correct
5 Correct 165 ms 20600 KB Output is correct
6 Correct 154 ms 20680 KB Output is correct
7 Correct 160 ms 20644 KB Output is correct
8 Incorrect 133 ms 20980 KB Output isn't correct
9 Halted 0 ms 0 KB -