Submission #866612

#TimeUsernameProblemLanguageResultExecution timeMemory
866612fanwenAlternating Heights (CCO22_day1problem1)C++17
25 / 25
196 ms13940 KiB
/** * author : pham van sam * created : 26.10.2023 20:29:38 **/ #include <bits/stdc++.h> using namespace std; #define MASK(x) (1LL << (x)) #define BIT(x, i) (((x) >> (i)) & 1) #define ALL(x) (x).begin(), (x).end() #define REP(i, n) for (int i = 0, _n = n; i < _n; ++i) #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i) #define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; } template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; } const int MAXN = 3e3 + 5; int N, K, Q, A[MAXN], res[MAXN], deg_in[MAXN], vis[MAXN], exist[MAXN]; vector <int> adj[MAXN]; void dfs(int u, int &ok) { vis[u] = 1; for (auto v : adj[u]) if(vis[v] < 2) { if(vis[v] == 1) { ok = 0; return; } else { dfs(v, ok); } } vis[u] = 2; } bool not_cycles(int l, int r) { fill(adj + 1, adj + K + 1, vector <int>()); fill(deg_in + 1, deg_in + K + 1, 0); fill(vis + 1, vis + K + 1, 0); fill(exist + 1, exist + K + 1, 0); FOR(i, l, r) { exist[A[i]] = 1; if(i & 1) { if(i + 1 <= r) { adj[A[i]].push_back(A[i + 1]); deg_in[A[i + 1]]++; } if(i - 1 >= l) { adj[A[i]].push_back(A[i - 1]); deg_in[A[i - 1]]++; } } // if(l == 2 && r == 4) { // if(i % 2 == 1 && i + 1 <= r) { // cout << A[i] << " " << A[i + 1] << '\n'; // } // if(i % 2 == 0 && i - 1 >= l) { // cout << A[i] << " " << A[i - 1] << '\n'; // } // } } FOR(i, 1, K) if(exist[i] && !deg_in[i]) { int oke = true; dfs(i, oke); if(!oke) return false; } FOR(i, 1, K) if(exist[i] && !vis[i]) return false; return true; } void you_make_it(void) { cin >> N >> K >> Q; FOR(i, 1, N) cin >> A[i]; int j = 1; FOR(i, 1, N) { while(j <= N && not_cycles(i, j)) ++j; res[i] = j; // cout << res[i] << " \n"[i == N]; } while(Q--) { int l, r; cin >> l >> r; cout << (res[l] <= r ? "NO\n" : "YES\n"); } } signed main() { #ifdef LOCAL freopen("TASK.inp", "r", stdin); freopen("TASK.out", "w", stdout); #endif auto start_time = chrono::steady_clock::now(); cin.tie(0), cout.tie(0) -> sync_with_stdio(0); you_make_it(); auto end_time = chrono::steady_clock::now(); cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl; return (0 ^ 0); } // Dream it. Wish it. Do it.
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...