Submission #781076

#TimeUsernameProblemLanguageResultExecution timeMemory
781076arnold518Alternating Heights (CCO22_day1problem1)C++17
25 / 25
464 ms13780 KiB
#include <bits/stdc++.h>
using namespace std;

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

const int MAXN = 3000;

int N, K, Q;
int A[MAXN+10], ans[MAXN+10];

int vis[MAXN+10];
vector<int> adj[MAXN+10];
bool dfs(int now)
{
	vis[now]=1;
	bool ret=false;
	for(auto nxt : adj[now])
	{
		if(vis[nxt]==1) return true;
		if(vis[nxt]==0) ret|=dfs(nxt);
	}
	vis[now]=2;
	return ret;
}

bool f(int l, int r)
{
	for(int i=l; i<r; i++)
	{
		if(i%2==l%2) adj[A[i+1]].push_back(A[i]);
		else adj[A[i]].push_back(A[i+1]);
	}

	bool ret=false;
	for(int i=1; i<=K; i++) if(!vis[i]) ret|=dfs(i);

	for(int i=1; i<=K; i++)
	{
		adj[i].clear(), adj[i].shrink_to_fit();
		vis[i]=0;
	}
	return !ret;
}

int main()
{
	scanf("%d%d%d", &N, &K, &Q);
	for(int i=1; i<=N; i++) scanf("%d", &A[i]);

	for(int i=N; i>=1; i--)
	{
		int pos=N;
		if(i+2<=N) pos=ans[i+2];
		for(; pos>=i && !f(i, pos); pos--) ;
		ans[i]=pos;
	}

	while(Q--)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		if(ans[x]>=y) printf("YES\n");
		else printf("NO\n");
	}
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:49:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |  scanf("%d%d%d", &N, &K, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:50:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |  for(int i=1; i<=N; i++) scanf("%d", &A[i]);
      |                          ~~~~~^~~~~~~~~~~~~
Main.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |   scanf("%d%d", &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...