Submission #69422

#TimeUsernameProblemLanguageResultExecution timeMemory
69422TadijaSebezLong Mansion (JOI17_long_mansion)C++11
100 / 100
586 ms204068 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
const int N=500050;
int pos[N],lo[N],hi[N],c[N],l[N],r[N];
vector<int> a[N];
bool on(int l, int r, int x){ return l<=x && r>=x;}
bool can(int l, int r, int x){ return (lo[x] && on(l,r,lo[x])) || (hi[x] && on(l,r,hi[x]));}
int main()
{
	int n,q,i,j,x,y;
	scanf("%i",&n);
	for(i=1;i<n;i++) scanf("%i",&c[i]);
	for(i=1;i<=n;i++)
	{
		scanf("%i",&x);
		a[i].resize(x);
		for(j=0;j<x;j++) scanf("%i",&a[i][j]);
	}
	for(i=1;i<=n;i++) pos[i]=0;
	for(i=n;i>1;i--)
	{
		for(j=0;j<a[i].size();j++) pos[a[i][j]]=i;
		hi[i-1]=pos[c[i-1]];
	}
	for(i=1;i<=n;i++) pos[i]=0;
	for(i=1;i<n;i++)
	{
		for(j=0;j<a[i].size();j++) pos[a[i][j]]=i;
		lo[i]=pos[c[i]];
	}
	//for(i=1;i<n;i++) printf("->%i:%i %i\n",i,lo[i],hi[i]);
	for(i=1;i<=n;i++)
	{
		l[i]=r[i]=i;
		while(1)
		{
			if(l[i]>1 && can(l[i],r[i],l[i]-1))
			{
				//printf("%i %i\n",l[l[i]-1],r[l[i]-1]);
				r[i]=max(r[i],r[l[i]-1]);
				l[i]=l[l[i]-1];
			}
			else if(r[i]<n && can(l[i],r[i],r[i])) r[i]++;
			else break;
		}
		//printf("%i:%i %i\n",i,l[i],r[i]);
	}
	scanf("%i",&q);
	while(q--)
	{
		scanf("%i %i",&x,&y);
		if(on(l[x],r[x],y)) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

Compilation message (stderr)

long_mansion.cpp: In function 'int main()':
long_mansion.cpp:25:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=0;j<a[i].size();j++) pos[a[i][j]]=i;
           ~^~~~~~~~~~~~
long_mansion.cpp:31:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=0;j<a[i].size();j++) pos[a[i][j]]=i;
           ~^~~~~~~~~~~~
long_mansion.cpp:14:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&n);
  ~~~~~^~~~~~~~~
long_mansion.cpp:15:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1;i<n;i++) scanf("%i",&c[i]);
                   ~~~~~^~~~~~~~~~~~
long_mansion.cpp:18:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i",&x);
   ~~~~~^~~~~~~~~
long_mansion.cpp:20:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   for(j=0;j<x;j++) scanf("%i",&a[i][j]);
                    ~~~~~^~~~~~~~~~~~~~~
long_mansion.cpp:51:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&q);
  ~~~~~^~~~~~~~~
long_mansion.cpp:54:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i %i",&x,&y);
   ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...