Submission #652547

# Submission time Handle Problem Language Result Execution time Memory
652547 2022-10-23T03:37:08 Z kym Long Mansion (JOI17_long_mansion) C++14
100 / 100
2407 ms 143100 KB
#include <bits/stdc++.h>
using namespace std;
#define int ll 
#define FOR(i,s,e) for(ll i = s; i <= (ll)e; ++i)
#define DEC(i,s,e) for(ll i = s; i >= (ll)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{"  << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#else
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long 
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first 	
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
#define reach cerr << "LINE: " << __LINE__ << "\n";
typedef pair <ll, ll> pi;
typedef tuple<ll,ll,ll> ti3;
typedef tuple<ll,ll,ll,ll> ti4;
ll rand(ll a, ll b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (ll)1e18 + 500;
template <typename T> bool chmax(T& a, const T b) { return a<b ? a = b, 1 : 0; }
template <typename T> bool chmin(T& a, const T b) { return a>b ? a = b, 1 : 0; }
const int MAXN = 500005;
int n;
int C[MAXN];
vector<int> V[MAXN];

struct SparseTable {
    vector<vector<int> > ST;
    int N, K;
    SparseTable(int _N, vector<int> a): N(_N) {
        K = MSB(N);
        ST.resize(K);
        ST[0] = a;
        for (int k = 1; k < K; ++k) 
            for (int i = 0; i+(1<<k) <= N; ++i) 
                ST[k].push_back( min(ST[k-1][i], ST[k-1][i+(1<<(k-1))]) ); //min
    }
    
    /* returns most significant bit of an integer */
    inline int MSB(unsigned int x) { return 32-__builtin_clz(x); }
    
    /* Min of range (x, x + 2^k-1) and (y-2^k+1, y) */
    int query(int x, int y) {
    	--x;--y;
        int k = MSB(y-x+1) - 1;
        return min(ST[k][x], ST[k][y-(1<<k)+1]);
    }
    
};

map<int,int> pre;
int goright[MAXN];
int goleft[MAXN];
int L[MAXN];
int R[MAXN];

int32_t main() 
{
	IAMSPEED
	cin >> n;
	FOR(i,1,n-1) cin >> C[i];
	FOR(i,1,n) {
		int b; cin >> b;
		while(b--) {
			int x; cin >> x;
			V[i].pb(x);
		}
	}
	//lfseg=new node(1,n);
	//rgseg=new node(1,n);
	FOR(i,1,n-1) {
		for(auto key:V[i]){
			pre[key]=i;
		}
		goright[i+1]=pre[C[i]];
	}
	pre.clear();
	DEC(i,n,1) {
		for(auto key:V[i]) {
			pre[key]=i;
		}
		if(pre.find(C[i-1]) != pre.end())goleft[i-1]=pre[C[i-1]];
		else goleft[i-1]=n+1;
	}
	dba(goright,1,n);
	dba(goleft,1,n);
	/*
	FOR(i,1,n) {
		rgset->update(i,-goright[i]);
	}
	*/
	vector<int> v;
	FOR(i,1,n) {
		v.pb(goright[i]);
	}
	SparseTable ST=SparseTable(n,v);
	FOR(i,1,n) {
		L[i]=R[i]=i;
		int newl=L[i],newr=R[i];
		reach
		db2(L[i],R[i]);
		do {
			
			L[i]=newl;R[i]=newr;
			db2(L[i],R[i]);
			int lo=R[i],hi=n+1;
			while(lo<hi-1) {
				int mid=(lo+hi)/2;
				if(ST.query(R[i]+1,mid) >= L[i])lo=mid;
				else hi=mid;
			}
			newr=lo;
			newl=L[i];
			db(L[i]-1);
			if(L[i]!=1 && goleft[L[i]-1] <= R[i])newl=L[L[i]-1];
		} while(newl!=L[i] || newr!=R[i]);
	}
	FOR(i,1,n) db2(L[i], R[i]);
	int q; cin >> q;
	while(q--) {
		int x,y; cin >> x >> y;
		if(L[x] <= y && y <= R[x])cout << "YES\n";
		else cout << "NO\n";
	}
	
}


# Verdict Execution time Memory Grader output
1 Correct 18 ms 12500 KB Output is correct
2 Correct 23 ms 12780 KB Output is correct
3 Correct 28 ms 13248 KB Output is correct
4 Correct 20 ms 12616 KB Output is correct
5 Correct 16 ms 12560 KB Output is correct
6 Correct 16 ms 12628 KB Output is correct
7 Correct 16 ms 12616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 12500 KB Output is correct
2 Correct 23 ms 12780 KB Output is correct
3 Correct 28 ms 13248 KB Output is correct
4 Correct 20 ms 12616 KB Output is correct
5 Correct 16 ms 12560 KB Output is correct
6 Correct 16 ms 12628 KB Output is correct
7 Correct 16 ms 12616 KB Output is correct
8 Correct 101 ms 18368 KB Output is correct
9 Correct 114 ms 18428 KB Output is correct
10 Correct 108 ms 18864 KB Output is correct
11 Correct 111 ms 19532 KB Output is correct
12 Correct 107 ms 17784 KB Output is correct
13 Correct 100 ms 18560 KB Output is correct
14 Correct 119 ms 18664 KB Output is correct
15 Correct 99 ms 18516 KB Output is correct
16 Correct 97 ms 18304 KB Output is correct
17 Correct 104 ms 18492 KB Output is correct
18 Correct 96 ms 18668 KB Output is correct
19 Correct 98 ms 18628 KB Output is correct
20 Correct 101 ms 18588 KB Output is correct
21 Correct 89 ms 18296 KB Output is correct
22 Correct 103 ms 18508 KB Output is correct
23 Correct 100 ms 18400 KB Output is correct
24 Correct 98 ms 18368 KB Output is correct
25 Correct 131 ms 18456 KB Output is correct
26 Correct 120 ms 18480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 663 ms 45540 KB Output is correct
2 Correct 611 ms 45288 KB Output is correct
3 Correct 625 ms 44856 KB Output is correct
4 Correct 611 ms 45552 KB Output is correct
5 Correct 594 ms 45424 KB Output is correct
6 Correct 553 ms 42260 KB Output is correct
7 Correct 556 ms 41844 KB Output is correct
8 Correct 553 ms 41840 KB Output is correct
9 Correct 554 ms 41796 KB Output is correct
10 Correct 627 ms 41812 KB Output is correct
11 Correct 521 ms 41944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 12500 KB Output is correct
2 Correct 23 ms 12780 KB Output is correct
3 Correct 28 ms 13248 KB Output is correct
4 Correct 20 ms 12616 KB Output is correct
5 Correct 16 ms 12560 KB Output is correct
6 Correct 16 ms 12628 KB Output is correct
7 Correct 16 ms 12616 KB Output is correct
8 Correct 101 ms 18368 KB Output is correct
9 Correct 114 ms 18428 KB Output is correct
10 Correct 108 ms 18864 KB Output is correct
11 Correct 111 ms 19532 KB Output is correct
12 Correct 107 ms 17784 KB Output is correct
13 Correct 100 ms 18560 KB Output is correct
14 Correct 119 ms 18664 KB Output is correct
15 Correct 99 ms 18516 KB Output is correct
16 Correct 97 ms 18304 KB Output is correct
17 Correct 104 ms 18492 KB Output is correct
18 Correct 96 ms 18668 KB Output is correct
19 Correct 98 ms 18628 KB Output is correct
20 Correct 101 ms 18588 KB Output is correct
21 Correct 89 ms 18296 KB Output is correct
22 Correct 103 ms 18508 KB Output is correct
23 Correct 100 ms 18400 KB Output is correct
24 Correct 98 ms 18368 KB Output is correct
25 Correct 131 ms 18456 KB Output is correct
26 Correct 120 ms 18480 KB Output is correct
27 Correct 663 ms 45540 KB Output is correct
28 Correct 611 ms 45288 KB Output is correct
29 Correct 625 ms 44856 KB Output is correct
30 Correct 611 ms 45552 KB Output is correct
31 Correct 594 ms 45424 KB Output is correct
32 Correct 553 ms 42260 KB Output is correct
33 Correct 556 ms 41844 KB Output is correct
34 Correct 553 ms 41840 KB Output is correct
35 Correct 554 ms 41796 KB Output is correct
36 Correct 627 ms 41812 KB Output is correct
37 Correct 521 ms 41944 KB Output is correct
38 Correct 2114 ms 118908 KB Output is correct
39 Correct 2407 ms 141608 KB Output is correct
40 Correct 1612 ms 93892 KB Output is correct
41 Correct 2144 ms 140092 KB Output is correct
42 Correct 624 ms 45784 KB Output is correct
43 Correct 613 ms 44244 KB Output is correct
44 Correct 1199 ms 74212 KB Output is correct
45 Correct 1201 ms 74940 KB Output is correct
46 Correct 1233 ms 76132 KB Output is correct
47 Correct 614 ms 45772 KB Output is correct
48 Correct 613 ms 43384 KB Output is correct
49 Correct 1213 ms 71488 KB Output is correct
50 Correct 1192 ms 73600 KB Output is correct
51 Correct 1241 ms 77124 KB Output is correct
52 Correct 1191 ms 79432 KB Output is correct
53 Correct 1811 ms 110296 KB Output is correct
54 Correct 2035 ms 143100 KB Output is correct
55 Correct 1556 ms 111400 KB Output is correct