Submission #1124845

#TimeUsernameProblemLanguageResultExecution timeMemory
1124845duckindogAlternating Heights (CCO22_day1problem1)C++20
25 / 25
350 ms4616 KiB
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include <bits/stdc++.h> using namespace std; const int N = 3000 + 10; int n, k, q; int a[N]; vector<int> ad[N]; int low[N], num[N], it; int scc[N], sccIt; stack<int> st; void dfs(int u, int p = -1) { low[u] = num[u] = ++it; st.push(u); for (const auto& v : ad[u]) { if (v == u) sccIt = k + 1; if (scc[v]) continue; if (num[v]) low[u] = min(low[u], num[v]); else { dfs(v, u); low[u] = min(low[u], low[v]); } } if (low[u] == num[u]) { sccIt += 1; for (;;) { int x = st.top(); st.pop(); scc[x] = sccIt; if (x == u) break; } } } int longest[N]; int32_t main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> k >> q; for (int i = 1; i <= n; ++i) cin >> a[i]; auto chk = [&](int l, int r) { for (int i = 1; i <= k; ++i) ad[i].clear(); for (int i = l + 1; i <= r; ++i) { if ((l - i) & 1) ad[a[i - 1]].push_back(a[i]); else ad[a[i]].push_back(a[i - 1]); } memset(low, 0, sizeof low); memset(num, 0, sizeof num); memset(scc, 0, sizeof scc); it = sccIt = 0; for (int i = 1; i <= k; ++i) if (!num[i]) dfs(i); return sccIt == k; }; int it = 1; for (int i = 1; i <= n; ++i) { while (it <= n && chk(i, it)) it += 1; longest[i] = it - 1; } while (q--) { int l, r; cin >> l >> r; cout << (longest[l] >= r ? "YES" : "NO") << "\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...