Submission #319402

# Submission time Handle Problem Language Result Execution time Memory
319402 2020-11-05T08:19:03 Z arnold518 Long Mansion (JOI17_long_mansion) C++14
100 / 100
1560 ms 58868 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 5e5;
const int INF = 987654321;

int N, QQ;
int C[MAXN+10];
vector<int> A[MAXN+10];

int L[MAXN+10], R[MAXN+10];
int P[MAXN+10], Q[MAXN+10];

struct SEG1
{
	int tree[MAXN*4+10];
	void update(int node, int tl, int tr, int p, int v)
	{
		if(tl==tr)
		{
			tree[node]=v;
			return;
		}
		int mid=tl+tr>>1;
		if(p<=mid) update(node*2, tl, mid, p, v);
		else update(node*2+1, mid+1, tr, p, v);
		tree[node]=max(tree[node*2], tree[node*2+1]);
	}
	int query1(int node, int tl, int tr, int l, int r)
	{
		int mid=tl+tr>>1;
		if(l<=tl && tr<=r) return tree[node];
		if(r<tl || tr<l) return -INF;
		return max(query1(node*2, tl, mid, l, r), query1(node*2+1, mid+1, tr, l, r));
	}
	int query2(int node, int tl, int tr, int l, int r, int p)
	{
		if(r<tl || tr<l) return -1;
		if(tree[node]<=p) return -1;
		if(tl==tr) return tl;
		int mid=tl+tr>>1;
		int t=query2(node*2, tl, mid, l, r, p);
		if(t!=-1) return t;
		t=query2(node*2+1, mid+1, tr, l, r, p);
		if(t!=-1) return t;
		return -1;
	}
}seg1, seg4;

struct SEG2
{
	int tree[MAXN*4+10];
	void update(int node, int tl, int tr, int p, int v)
	{
		if(tl==tr)
		{
			tree[node]=v;
			return;
		}
		int mid=tl+tr>>1;
		if(p<=mid) update(node*2, tl, mid, p, v);
		else update(node*2+1, mid+1, tr, p, v);
		tree[node]=min(tree[node*2], tree[node*2+1]);
	}
	int query1(int node, int tl, int tr, int l, int r)
	{
		int mid=tl+tr>>1;
		if(l<=tl && tr<=r) return tree[node];
		if(r<tl || tr<l) return INF;
		return min(query1(node*2, tl, mid, l, r), query1(node*2+1, mid+1, tr, l, r));
	}
	int query2(int node, int tl, int tr, int l, int r, int p)
	{
		if(r<tl || tr<l) return -1;
		if(tree[node]>=p) return -1;
		if(tl==tr) return tl;
		int mid=tl+tr>>1;
		int t=query2(node*2+1, mid+1, tr, l, r, p);
		if(t!=-1) return t;
		t=query2(node*2, tl, mid, l, r, p);
		if(t!=-1) return t;
		return -1;
	}
}seg2, seg3;

int main()
{
	scanf("%d", &N);
	for(int i=1; i<N; i++) scanf("%d", &C[i]);
	for(int i=1; i<=N; i++) A[i].push_back(0);
	for(int i=1; i<=N; i++)
	{
		int t;
		scanf("%d", &t);
		while(t--)
		{
			int p;
			scanf("%d", &p);
			A[p].push_back(i);
		}
	}
	for(int i=1; i<=N; i++) A[i].push_back(N+1);

	for(int i=1; i<N; i++)
	{
		int col=C[i];
		L[i]=*(--upper_bound(A[col].begin(), A[col].end(), i));
		R[i]=*lower_bound(A[col].begin(), A[col].end(), i+1);
	}

	L[0]=0; R[0]=N+1;
	L[N]=0; R[N]=N+1;

	for(int i=1; i<=N; i++) seg1.update(1, 1, N, i, R[i-1]);

	for(int i=1; i<N; i++)
	{
		if(seg1.query1(1, 1, N, L[i]+1, i)<=i) P[i]=N+1;
		else P[i]=seg1.query2(1, 1, N, L[i]+1, i, i);
		seg3.update(1, 1, N, i, P[i]);
	}

	for(int i=1; i<N; i++) seg2.update(1, 1, N, i, L[i]);

	for(int i=2; i<=N; i++)
	{
		if(seg2.query1(1, 1, N, i, R[i-1]-1)>=i) Q[i]=0;
		else Q[i]=seg2.query2(1, 1, N, i, R[i-1]-1, i);
		seg4.update(1, 1, N, i-1, Q[i]);
	}

	scanf("%d", &QQ);
	while(QQ--)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		if(x<y)
		{
			if(seg3.query1(1, 1, N, x, y-1)<=x) printf("NO\n");
			else printf("YES\n");
		}
		else
		{
			if(seg4.query1(1, 1, N, y, x-1)>=x) printf("NO\n");
			else printf("YES\n");
		}
	}
}

Compilation message

long_mansion.cpp: In member function 'void SEG1::update(int, int, int, int, int)':
long_mansion.cpp:28:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   28 |   int mid=tl+tr>>1;
      |           ~~^~~
long_mansion.cpp: In member function 'int SEG1::query1(int, int, int, int, int)':
long_mansion.cpp:35:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   35 |   int mid=tl+tr>>1;
      |           ~~^~~
long_mansion.cpp: In member function 'int SEG1::query2(int, int, int, int, int, int)':
long_mansion.cpp:45:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   45 |   int mid=tl+tr>>1;
      |           ~~^~~
long_mansion.cpp: In member function 'void SEG2::update(int, int, int, int, int)':
long_mansion.cpp:64:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   64 |   int mid=tl+tr>>1;
      |           ~~^~~
long_mansion.cpp: In member function 'int SEG2::query1(int, int, int, int, int)':
long_mansion.cpp:71:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   71 |   int mid=tl+tr>>1;
      |           ~~^~~
long_mansion.cpp: In member function 'int SEG2::query2(int, int, int, int, int, int)':
long_mansion.cpp:81:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   81 |   int mid=tl+tr>>1;
      |           ~~^~~
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:92:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   92 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
long_mansion.cpp:93:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   93 |  for(int i=1; i<N; i++) scanf("%d", &C[i]);
      |                         ~~~~~^~~~~~~~~~~~~
long_mansion.cpp:98:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   98 |   scanf("%d", &t);
      |   ~~~~~^~~~~~~~~~
long_mansion.cpp:102:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  102 |    scanf("%d", &p);
      |    ~~~~~^~~~~~~~~~
long_mansion.cpp:136:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  136 |  scanf("%d", &QQ);
      |  ~~~~~^~~~~~~~~~~
long_mansion.cpp:140:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  140 |   scanf("%d%d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12396 KB Output is correct
2 Correct 14 ms 12524 KB Output is correct
3 Correct 17 ms 12652 KB Output is correct
4 Correct 13 ms 12396 KB Output is correct
5 Correct 13 ms 12396 KB Output is correct
6 Correct 13 ms 12396 KB Output is correct
7 Correct 13 ms 12268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12396 KB Output is correct
2 Correct 14 ms 12524 KB Output is correct
3 Correct 17 ms 12652 KB Output is correct
4 Correct 13 ms 12396 KB Output is correct
5 Correct 13 ms 12396 KB Output is correct
6 Correct 13 ms 12396 KB Output is correct
7 Correct 13 ms 12268 KB Output is correct
8 Correct 269 ms 13956 KB Output is correct
9 Correct 262 ms 13796 KB Output is correct
10 Correct 280 ms 14216 KB Output is correct
11 Correct 295 ms 14480 KB Output is correct
12 Correct 249 ms 14184 KB Output is correct
13 Correct 228 ms 14052 KB Output is correct
14 Correct 239 ms 14268 KB Output is correct
15 Correct 230 ms 14180 KB Output is correct
16 Correct 214 ms 14436 KB Output is correct
17 Correct 229 ms 14052 KB Output is correct
18 Correct 236 ms 14052 KB Output is correct
19 Correct 241 ms 14052 KB Output is correct
20 Correct 225 ms 14308 KB Output is correct
21 Correct 214 ms 14308 KB Output is correct
22 Correct 241 ms 14056 KB Output is correct
23 Correct 212 ms 13924 KB Output is correct
24 Correct 219 ms 13924 KB Output is correct
25 Correct 212 ms 13924 KB Output is correct
26 Correct 215 ms 13924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 598 ms 25444 KB Output is correct
2 Correct 589 ms 24932 KB Output is correct
3 Correct 598 ms 24996 KB Output is correct
4 Correct 591 ms 25196 KB Output is correct
5 Correct 605 ms 25444 KB Output is correct
6 Correct 452 ms 24420 KB Output is correct
7 Correct 452 ms 24548 KB Output is correct
8 Correct 458 ms 24632 KB Output is correct
9 Correct 450 ms 24548 KB Output is correct
10 Correct 449 ms 24544 KB Output is correct
11 Correct 451 ms 24676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12396 KB Output is correct
2 Correct 14 ms 12524 KB Output is correct
3 Correct 17 ms 12652 KB Output is correct
4 Correct 13 ms 12396 KB Output is correct
5 Correct 13 ms 12396 KB Output is correct
6 Correct 13 ms 12396 KB Output is correct
7 Correct 13 ms 12268 KB Output is correct
8 Correct 269 ms 13956 KB Output is correct
9 Correct 262 ms 13796 KB Output is correct
10 Correct 280 ms 14216 KB Output is correct
11 Correct 295 ms 14480 KB Output is correct
12 Correct 249 ms 14184 KB Output is correct
13 Correct 228 ms 14052 KB Output is correct
14 Correct 239 ms 14268 KB Output is correct
15 Correct 230 ms 14180 KB Output is correct
16 Correct 214 ms 14436 KB Output is correct
17 Correct 229 ms 14052 KB Output is correct
18 Correct 236 ms 14052 KB Output is correct
19 Correct 241 ms 14052 KB Output is correct
20 Correct 225 ms 14308 KB Output is correct
21 Correct 214 ms 14308 KB Output is correct
22 Correct 241 ms 14056 KB Output is correct
23 Correct 212 ms 13924 KB Output is correct
24 Correct 219 ms 13924 KB Output is correct
25 Correct 212 ms 13924 KB Output is correct
26 Correct 215 ms 13924 KB Output is correct
27 Correct 598 ms 25444 KB Output is correct
28 Correct 589 ms 24932 KB Output is correct
29 Correct 598 ms 24996 KB Output is correct
30 Correct 591 ms 25196 KB Output is correct
31 Correct 605 ms 25444 KB Output is correct
32 Correct 452 ms 24420 KB Output is correct
33 Correct 452 ms 24548 KB Output is correct
34 Correct 458 ms 24632 KB Output is correct
35 Correct 450 ms 24548 KB Output is correct
36 Correct 449 ms 24544 KB Output is correct
37 Correct 451 ms 24676 KB Output is correct
38 Correct 1481 ms 53508 KB Output is correct
39 Correct 1560 ms 58868 KB Output is correct
40 Correct 1123 ms 48868 KB Output is correct
41 Correct 1319 ms 58020 KB Output is correct
42 Correct 468 ms 24420 KB Output is correct
43 Correct 485 ms 24420 KB Output is correct
44 Correct 714 ms 34776 KB Output is correct
45 Correct 716 ms 34660 KB Output is correct
46 Correct 729 ms 34660 KB Output is correct
47 Correct 502 ms 24420 KB Output is correct
48 Correct 480 ms 24548 KB Output is correct
49 Correct 706 ms 34788 KB Output is correct
50 Correct 727 ms 34660 KB Output is correct
51 Correct 779 ms 34808 KB Output is correct
52 Correct 686 ms 32740 KB Output is correct
53 Correct 908 ms 46432 KB Output is correct
54 Correct 1148 ms 50960 KB Output is correct
55 Correct 894 ms 46124 KB Output is correct