Submission #699509

#TimeUsernameProblemLanguageResultExecution timeMemory
699509arcaneAlternating Heights (CCO22_day1problem1)C++17
25 / 25
424 ms9184 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define fi first #define se second #define mp make_pair #define pb emplace_back const int MAX_N = 5005; #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize ("unroll-loops") vector <int> edg[MAX_N], topo; int arr[MAX_N], ans[MAX_N], cur; bool marked[MAX_N], flag; int n, k; void dfs(int x){ marked[x] = true; for (auto edge : edg[x]){ if (!marked[edge]) dfs(edge); } topo.pb(x); } void dfs2(int x){ if (marked[x]) return; marked[x] = true; for (auto edge : edg[x]){ if (!marked[edge]) flag = true; } } bool check(int x){ //if (cur == 1) cerr << cur << ' ' << x << '\n'; for (int i = 0; i < k; i++) edg[i].clear(); topo.clear(); fill(marked, marked + k, 0); for (int i = cur; i < x; i++){ if ((i - cur) % 2) edg[arr[i]].pb(arr[i + 1]); else edg[arr[i + 1]].pb(arr[i]); if (arr[i] == arr[i + 1]) return false; /*if (cur == 1){ if ((i - cur) % 2) cerr << arr[i] << ' ' << arr[i + 1] << '\n'; else cerr << arr[i + 1] << ' ' << arr[i] << '\n'; }*/ } for (int i = 0; i < n; i++){ if (!marked[i]) dfs(i); } //reverse(topo.begin(), topo.end()); //if (cur == 1) for (auto tmp : topo) cerr << tmp << ' '; //if (cur == 1) cerr << '\n'; fill(marked, marked + k, 0); flag = false; for (auto nod : topo){ if (!marked[nod]){ dfs2(nod); /*if (cur == 1){ cerr << nod << ' ' << flag << '\n'; for (int j = 0; j < 3; j++) cerr << marked[j] << ' '; cerr << '\n'; }*/ } } //if (cur == 1) cerr << '\n'; return !flag; } void init(){ for (int i = 0; i < n; i++){ int lo = i, hi = n, mi; cur = i; if (i) ans[i] = max(i, ans[i - 1]); if (ans[i] == n - 1) continue; /*if (i == 1){ cerr << "Checking 1: \n"; check(4); cerr << "DONE\n"; }*/ while (ans[i] < n and check(ans[i])) ans[i]++; ans[i] -= 1; } } int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int q, x, y; cin >> n >> k >> q; for (int i = 0; i < n; i++) cin >> arr[i], arr[i] -= 1; init(); //for (int i = 0; i < n; i++) cerr << "Highest: " << ans[i] << '\n'; for (int i = 0; i < q; i++){ cin >> x >> y; x -= 1; y -= 1; if (y <= ans[x]) cout << "YES\n"; else cout << "NO\n"; } }

Compilation message (stderr)

Main.cpp: In function 'void init()':
Main.cpp:66:7: warning: unused variable 'lo' [-Wunused-variable]
   66 |   int lo = i, hi = n, mi; cur = i;
      |       ^~
Main.cpp:66:15: warning: unused variable 'hi' [-Wunused-variable]
   66 |   int lo = i, hi = n, mi; cur = i;
      |               ^~
Main.cpp:66:23: warning: unused variable 'mi' [-Wunused-variable]
   66 |   int lo = i, hi = n, mi; cur = i;
      |                       ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...