Submission #126237

# Submission time Handle Problem Language Result Execution time Memory
126237 2019-07-07T09:49:25 Z briansu Long Mansion (JOI17_long_mansion) C++14
100 / 100
1110 ms 136236 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
#define REP(i, n) for(int i = 0;i < n;i ++)
#define REP1(i, n) for(int i = 1;i <= n;i ++)
#define FILL(i, n) memset(i, n, sizeof(i))
#define X first
#define Y second
#define pb push_back
#define SZ(_a) ((int)(_a).size())
#define ALL(_a) (_a).begin(), (_a).end()
#ifdef brian
template<typename T>void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...t>void _do(T &&x, t &&...X){cerr<<x<<", ";_do(X...);}
#define debug(...) {\
	fprintf(stderr, "%s - %d (%s) = ", __PRETTY_FUNCTION__, __LINE__, #__VA_ARGS__);\
	_do(__VA_ARGS__);\
}
#define IOS()
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...)
#define endl '\n'
#endif

const ll MAXn = 5e5 + 5;
const ll INF = ll(1e17);
const ll MOD = 1000000007;

struct node{
	ll l, r;
	node *lc, *rc;
	ll mn;
	node(ll li,ll ri,node *lci = 0, node *rci = 0):l(li), r(ri), lc(lci), rc(rci), mn(INF){}
	void pull(){
		if(l != r-1)mn = min(lc->mn, rc->mn);
	}
	void ins(ll x,ll k)
	{
		if(l == r-1)mn = k;
		else if(x < (l + r) / 2)lc->ins(x, k), pull();
		else rc->ins(x, k), pull();
	}
	ll qr(ll li,ll ri)
	{
		if(li >= r || ri <= l)return INF;
		else if(li <= l && ri >= r)return mn;
		else return min(lc->qr(li, ri), rc->qr(li, ri));
	}
	ll fd(ll x)
	{
		if(l == r-1)return l;
		else if(lc->mn <= x)return lc->fd(x);
		else return rc->fd(x);
	}
};

node *build(ll l,ll r)
{
	if(l == r-1)return new node(l, r);
	else return new node(l, r, build(l, (l + r) / 2), build((l + r) / 2, r));
}

vector<ll> keys[MAXn];
vector<ii> qr[MAXn];
ll lk[MAXn], rk[MAXn], l[MAXn], r[MAXn], lst[MAXn], ans[MAXn];

int main(){
	IOS();
	ll n;
	cin>>n;
	REP1(i, n - 1)cin>>rk[i], lk[i + 1] = rk[i];
	REP1(i, n)
	{
		ll t;
		cin>>t;
		while(t--)
		{
			ll x;
			cin>>x;
			keys[i].pb(x);
		}
	}
	lk[1] = rk[n] = 1;
	REP1(i, n)lst[i] = 0;
	REP1(i, n)
	{
		l[i] = lst[lk[i]];
		for(ll t:keys[i])lst[t] = i;
	}
	REP1(i, n)lst[i] = n + 1;
	for(int i = n;i > 0;i --)
	{
		r[i] = lst[rk[i]];
		for(ll t:keys[i])lst[t] = i;
	}
	ll q;
	cin>>q;
	REP(i, q)
	{
		ll a, b;
		cin>>a>>b;
		qr[a].pb(ii(b, i));
	}
	node *rt = build(0, n + 3);
	REP1(i, n)rt->ins(i, l[i]);
	rt->ins(n + 1, 0);
	set<ll> st;
	st.insert(0);
	r[0] = n + 1;
	for(int i = 0;i <= n + 1;i ++)debug(i, l[i], r[i]);
	REP1(i, n)
	{
		rt->ins(i, INF);
		while(1)
		{
			ll t = *st.rbegin();
			debug(t, r[t], rt->qr(t + 1, r[t] + 1));
			if(rt->qr(t + 1, r[t] + 1) > t)st.erase(t);
			else break;
		}
		ll a = *st.rbegin();
		ll b = rt->fd(a);
		for(auto &p:qr[i])ans[p.Y] = (p.X > a && p.X < b);
		while(SZ(st) && r[*st.rbegin()] <= r[i])
		{
			debug(*st.rbegin());
			st.erase(*st.rbegin());
		}
		st.insert(i);
	}
	REP(i, q)cout<<(ans[i] ? "YES" : "NO")<<endl;

}
# Verdict Execution time Memory Grader output
1 Correct 28 ms 24440 KB Output is correct
2 Correct 29 ms 24568 KB Output is correct
3 Correct 31 ms 24952 KB Output is correct
4 Correct 27 ms 24440 KB Output is correct
5 Correct 28 ms 24568 KB Output is correct
6 Correct 28 ms 24440 KB Output is correct
7 Correct 28 ms 24440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 24440 KB Output is correct
2 Correct 29 ms 24568 KB Output is correct
3 Correct 31 ms 24952 KB Output is correct
4 Correct 27 ms 24440 KB Output is correct
5 Correct 28 ms 24568 KB Output is correct
6 Correct 28 ms 24440 KB Output is correct
7 Correct 28 ms 24440 KB Output is correct
8 Correct 209 ms 46312 KB Output is correct
9 Correct 193 ms 46312 KB Output is correct
10 Correct 203 ms 47424 KB Output is correct
11 Correct 210 ms 45692 KB Output is correct
12 Correct 185 ms 44448 KB Output is correct
13 Correct 187 ms 47428 KB Output is correct
14 Correct 193 ms 47260 KB Output is correct
15 Correct 200 ms 46968 KB Output is correct
16 Correct 202 ms 46832 KB Output is correct
17 Correct 195 ms 47600 KB Output is correct
18 Correct 185 ms 47236 KB Output is correct
19 Correct 194 ms 47180 KB Output is correct
20 Correct 189 ms 44836 KB Output is correct
21 Correct 187 ms 46584 KB Output is correct
22 Correct 218 ms 47352 KB Output is correct
23 Correct 210 ms 46328 KB Output is correct
24 Correct 207 ms 46584 KB Output is correct
25 Correct 194 ms 46584 KB Output is correct
26 Correct 193 ms 46172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 488 ms 68136 KB Output is correct
2 Correct 497 ms 67836 KB Output is correct
3 Correct 491 ms 67448 KB Output is correct
4 Correct 494 ms 68088 KB Output is correct
5 Correct 539 ms 68028 KB Output is correct
6 Correct 394 ms 63612 KB Output is correct
7 Correct 397 ms 63300 KB Output is correct
8 Correct 397 ms 63104 KB Output is correct
9 Correct 422 ms 63292 KB Output is correct
10 Correct 403 ms 63176 KB Output is correct
11 Correct 407 ms 63176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 24440 KB Output is correct
2 Correct 29 ms 24568 KB Output is correct
3 Correct 31 ms 24952 KB Output is correct
4 Correct 27 ms 24440 KB Output is correct
5 Correct 28 ms 24568 KB Output is correct
6 Correct 28 ms 24440 KB Output is correct
7 Correct 28 ms 24440 KB Output is correct
8 Correct 209 ms 46312 KB Output is correct
9 Correct 193 ms 46312 KB Output is correct
10 Correct 203 ms 47424 KB Output is correct
11 Correct 210 ms 45692 KB Output is correct
12 Correct 185 ms 44448 KB Output is correct
13 Correct 187 ms 47428 KB Output is correct
14 Correct 193 ms 47260 KB Output is correct
15 Correct 200 ms 46968 KB Output is correct
16 Correct 202 ms 46832 KB Output is correct
17 Correct 195 ms 47600 KB Output is correct
18 Correct 185 ms 47236 KB Output is correct
19 Correct 194 ms 47180 KB Output is correct
20 Correct 189 ms 44836 KB Output is correct
21 Correct 187 ms 46584 KB Output is correct
22 Correct 218 ms 47352 KB Output is correct
23 Correct 210 ms 46328 KB Output is correct
24 Correct 207 ms 46584 KB Output is correct
25 Correct 194 ms 46584 KB Output is correct
26 Correct 193 ms 46172 KB Output is correct
27 Correct 488 ms 68136 KB Output is correct
28 Correct 497 ms 67836 KB Output is correct
29 Correct 491 ms 67448 KB Output is correct
30 Correct 494 ms 68088 KB Output is correct
31 Correct 539 ms 68028 KB Output is correct
32 Correct 394 ms 63612 KB Output is correct
33 Correct 397 ms 63300 KB Output is correct
34 Correct 397 ms 63104 KB Output is correct
35 Correct 422 ms 63292 KB Output is correct
36 Correct 403 ms 63176 KB Output is correct
37 Correct 407 ms 63176 KB Output is correct
38 Correct 1010 ms 120012 KB Output is correct
39 Correct 1069 ms 136236 KB Output is correct
40 Correct 828 ms 103100 KB Output is correct
41 Correct 1110 ms 134716 KB Output is correct
42 Correct 401 ms 64248 KB Output is correct
43 Correct 409 ms 63536 KB Output is correct
44 Correct 567 ms 82296 KB Output is correct
45 Correct 561 ms 82356 KB Output is correct
46 Correct 571 ms 82432 KB Output is correct
47 Correct 396 ms 65144 KB Output is correct
48 Correct 390 ms 63400 KB Output is correct
49 Correct 574 ms 82392 KB Output is correct
50 Correct 570 ms 82704 KB Output is correct
51 Correct 588 ms 82768 KB Output is correct
52 Correct 654 ms 87304 KB Output is correct
53 Correct 827 ms 108072 KB Output is correct
54 Correct 1048 ms 128612 KB Output is correct
55 Correct 839 ms 107892 KB Output is correct