Submission #402157

# Submission time Handle Problem Language Result Execution time Memory
402157 2021-05-11T11:36:31 Z AriaH Long Mansion (JOI17_long_mansion) C++11
100 / 100
1999 ms 89208 KB
/** I can do this all day **/

#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;

typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define SZ(x)			    		(int)x.size()
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);

const int N = 5e5 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 1e9;
const int LOG = 22;

ll pw(ll a , ll b, ll M)  { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }

int Ans[N];

int n, q, C[N], Z[N], X[N], Y[N], same[N << 2], seg[N << 2], lz[N << 2], last[N], nxt[N], cur[N];

vector < int > keys[N], Q[N];

void build(int v = 1, int tl = 1, int tr = n - 1)
{
	lz[v] = 0;
	same[v] = 0;
	if(tl == tr)
	{
		/*if(last[tl] == -inf)
		{
			seg[v] = -1;
			return;
		}*/
		seg[v] = -last[tl];
		return;
	}
	int mid = (tl + tr) >> 1;
	build(v << 1, tl, mid);
	build(v << 1 | 1, mid + 1, tr);
	seg[v] = max(seg[v << 1], seg[v << 1 | 1]);
}

void S(int v, int tl, int tr)
{
	if(tl == tr || !lz[v]) return;
	if(same[v << 1] >= 0)
	{
	    same[v << 1] += lz[v];
	}
	if(same[v << 1 | 1] >= 0) 
	{
	    same[v << 1 | 1] += lz[v];
	}
	seg[v << 1] += lz[v];
	seg[v << 1 | 1] += lz[v];
	lz[v << 1] += lz[v];
	lz[v << 1 | 1] += lz[v];
	lz[v] = 0;
}

void Set(int l, int r, int x, int v = 1, int tl = 1, int tr = n - 1)
{
	S(v, tl, tr);
	if(l > tr || r < tl || l > r) return;
	if(l <= tl && tr <= r && same[v] >= 0)
	{
		lz[v] += x - same[v];
		seg[v] += x - same[v];
		same[v] = x;
		return;
	}
	int mid = (tl + tr) >> 1;
	Set(l, r, x, v << 1, tl, mid);
	Set(l, r, x, v << 1 | 1, mid + 1, tr);
	seg[v] = max(seg[v << 1], seg[v << 1 | 1]);
	same[v] = (same[v << 1] == same[v << 1 | 1]? same[v << 1] : -1);
}

int get(int l, int r, int v = 1, int tl = 1, int tr = n - 1)
{
	S(v, tl, tr);
	if(l > tr || r < tl || l > r) return -inf;
	if(l <= tl && tr <= r) return seg[v];
	int mid = (tl + tr) >> 1;
	return max(get(l, r, v << 1, tl, mid), get(l, r, v << 1 | 1, mid + 1, tr));
}

void solve()
{
	fill(cur, cur + N, -inf);
	for(int i = 1; i <= n; i ++) Q[i].clear();
	for(int i = 1; i <= n; i ++)
	{
		for(auto x : keys[i])
		{
			cur[x] = i;
		}
		/*printf("here i = %d cur : \n", i);
		for(int j = 1; j <= n; j ++)
		{
			printf("%d ", cur[j]);
		}
		printf("\n");*/
		if(i < n) last[i] = cur[C[i]];
	}
	fill(cur, cur + N, inf);
	for(int i = n; i >= 1; i --)
	{
		for(auto x : keys[i]) cur[x] = i;
		if(i > 1) nxt[i - 1] = cur[C[i - 1]];
	}
	/*for(int i = 1; i < n; i ++)
	{
		printf("i = %d last = %d nxt = %d\n", i, last[i], nxt[i]);
	}*/
	build();
	for(int i = 1; i <= q; i ++)
	{
		if(X[i] < Y[i])
		{
			Q[X[i]].push_back(i);
		}
		if(X[i] == Y[i]) Ans[i] = 1;
	}
	/*printf("checking segment i = 0\n");
	for(int i = 1; i < n; i ++) printf("%d ", get(i, i));
	printf("\n");
	*/
	for(int i = 1; i < n; i ++)
	{
		for(auto now : Q[i])
		{
			if(get(X[now], Y[now] - 1) >= 0) Ans[now] = 0;
			else Ans[now] = 1;
		}
		Set(i + 1, min(n - 1, nxt[i] - 1), i);
		/*printf("i = %d\n", i);
		for(int j = 1; j < n; j ++)
		{
			printf("%d ", get(j, j));
		}
		printf("\n");
		*/
	}
}

int main()
{
	scanf("%d", &n);
	for(int i = 1; i < n; i ++)
	{
		scanf("%d", &C[i]);
	}
	for(int i = 1; i <= n; i ++)
	{
		scanf("%d", &Z[i]);
		for(int j = 1; j <= Z[i]; j ++)
		{
			int x;
			scanf("%d", &x);
			keys[i].push_back(x);
		}
	}
	scanf("%d", &q);
	for(int i = 1; i <= q; i ++)
	{
		scanf("%d%d", &X[i], &Y[i]);
	}
	solve();
	reverse(C + 1, C + n);
	reverse(keys + 1, keys + n + 1);
	reverse(Z + 1, Z + n + 1);
	for(int i = 1; i <= q; i ++)
	{
		X[i] = n - X[i] + 1;
		Y[i] = n - Y[i] + 1;
	}
	solve();
	for(int i = 1; i <= q; i ++)
	{
		if(Ans[i])
		{
			printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}
	}
	return 0;
}

/** test corner cases(n = 1?) watch for overflow or minus indices **/

Compilation message

long_mansion.cpp: In function 'int main()':
long_mansion.cpp:159:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
long_mansion.cpp:162:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |   scanf("%d", &C[i]);
      |   ~~~~~^~~~~~~~~~~~~
long_mansion.cpp:166:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  166 |   scanf("%d", &Z[i]);
      |   ~~~~~^~~~~~~~~~~~~
long_mansion.cpp:170:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  170 |    scanf("%d", &x);
      |    ~~~~~^~~~~~~~~~
long_mansion.cpp:174:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  174 |  scanf("%d", &q);
      |  ~~~~~^~~~~~~~~~
long_mansion.cpp:177:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  177 |   scanf("%d%d", &X[i], &Y[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 23 ms 26060 KB Output is correct
2 Correct 23 ms 26268 KB Output is correct
3 Correct 26 ms 26456 KB Output is correct
4 Correct 21 ms 26156 KB Output is correct
5 Correct 21 ms 26068 KB Output is correct
6 Correct 21 ms 26060 KB Output is correct
7 Correct 22 ms 26104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 26060 KB Output is correct
2 Correct 23 ms 26268 KB Output is correct
3 Correct 26 ms 26456 KB Output is correct
4 Correct 21 ms 26156 KB Output is correct
5 Correct 21 ms 26068 KB Output is correct
6 Correct 21 ms 26060 KB Output is correct
7 Correct 22 ms 26104 KB Output is correct
8 Correct 336 ms 39404 KB Output is correct
9 Correct 336 ms 39340 KB Output is correct
10 Correct 425 ms 40020 KB Output is correct
11 Correct 378 ms 40356 KB Output is correct
12 Correct 314 ms 38876 KB Output is correct
13 Correct 263 ms 39876 KB Output is correct
14 Correct 273 ms 39880 KB Output is correct
15 Correct 265 ms 39956 KB Output is correct
16 Correct 304 ms 39604 KB Output is correct
17 Correct 266 ms 39876 KB Output is correct
18 Correct 308 ms 39852 KB Output is correct
19 Correct 282 ms 39876 KB Output is correct
20 Correct 258 ms 39612 KB Output is correct
21 Correct 261 ms 39576 KB Output is correct
22 Correct 281 ms 39836 KB Output is correct
23 Correct 264 ms 39504 KB Output is correct
24 Correct 266 ms 39584 KB Output is correct
25 Correct 266 ms 39496 KB Output is correct
26 Correct 260 ms 39532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 697 ms 48296 KB Output is correct
2 Correct 693 ms 52348 KB Output is correct
3 Correct 686 ms 52128 KB Output is correct
4 Correct 728 ms 52504 KB Output is correct
5 Correct 708 ms 52468 KB Output is correct
6 Correct 510 ms 51316 KB Output is correct
7 Correct 477 ms 51104 KB Output is correct
8 Correct 444 ms 51072 KB Output is correct
9 Correct 436 ms 51028 KB Output is correct
10 Correct 438 ms 51072 KB Output is correct
11 Correct 431 ms 51060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 26060 KB Output is correct
2 Correct 23 ms 26268 KB Output is correct
3 Correct 26 ms 26456 KB Output is correct
4 Correct 21 ms 26156 KB Output is correct
5 Correct 21 ms 26068 KB Output is correct
6 Correct 21 ms 26060 KB Output is correct
7 Correct 22 ms 26104 KB Output is correct
8 Correct 336 ms 39404 KB Output is correct
9 Correct 336 ms 39340 KB Output is correct
10 Correct 425 ms 40020 KB Output is correct
11 Correct 378 ms 40356 KB Output is correct
12 Correct 314 ms 38876 KB Output is correct
13 Correct 263 ms 39876 KB Output is correct
14 Correct 273 ms 39880 KB Output is correct
15 Correct 265 ms 39956 KB Output is correct
16 Correct 304 ms 39604 KB Output is correct
17 Correct 266 ms 39876 KB Output is correct
18 Correct 308 ms 39852 KB Output is correct
19 Correct 282 ms 39876 KB Output is correct
20 Correct 258 ms 39612 KB Output is correct
21 Correct 261 ms 39576 KB Output is correct
22 Correct 281 ms 39836 KB Output is correct
23 Correct 264 ms 39504 KB Output is correct
24 Correct 266 ms 39584 KB Output is correct
25 Correct 266 ms 39496 KB Output is correct
26 Correct 260 ms 39532 KB Output is correct
27 Correct 697 ms 48296 KB Output is correct
28 Correct 693 ms 52348 KB Output is correct
29 Correct 686 ms 52128 KB Output is correct
30 Correct 728 ms 52504 KB Output is correct
31 Correct 708 ms 52468 KB Output is correct
32 Correct 510 ms 51316 KB Output is correct
33 Correct 477 ms 51104 KB Output is correct
34 Correct 444 ms 51072 KB Output is correct
35 Correct 436 ms 51028 KB Output is correct
36 Correct 438 ms 51072 KB Output is correct
37 Correct 431 ms 51060 KB Output is correct
38 Correct 1975 ms 83896 KB Output is correct
39 Correct 1999 ms 89208 KB Output is correct
40 Correct 1516 ms 76784 KB Output is correct
41 Correct 1216 ms 87680 KB Output is correct
42 Correct 516 ms 52024 KB Output is correct
43 Correct 465 ms 52076 KB Output is correct
44 Correct 581 ms 63632 KB Output is correct
45 Correct 592 ms 63780 KB Output is correct
46 Correct 600 ms 63820 KB Output is correct
47 Correct 484 ms 52164 KB Output is correct
48 Correct 460 ms 51996 KB Output is correct
49 Correct 669 ms 63676 KB Output is correct
50 Correct 660 ms 63824 KB Output is correct
51 Correct 657 ms 64208 KB Output is correct
52 Correct 748 ms 63732 KB Output is correct
53 Correct 979 ms 78196 KB Output is correct
54 Correct 1239 ms 85668 KB Output is correct
55 Correct 1027 ms 78160 KB Output is correct