Submission #1154943

#TimeUsernameProblemLanguageResultExecution timeMemory
1154943pemguimnThrough Another Maze Darkly (CCO21_day1problem3)C++20
Compilation error
0 ms0 KiB
#include <bits/stdc++.h> #define pii pair<int, int> using namespace std; const int N = 1.6e6 + 5; int n, q; vector<pii> adj[N]; int p[N], pt[N], eid[N], vid[N], ans[N], timedfs = 0, ee = 0, s = 0; vector<int> tid[N]; template <typename T> struct Fenwick { int n; std::vector<T> a; Fenwick(int n_ = 0) { init(n_); } inline void init(int n_) { n = n_; a.assign(n + 5, T{}); } inline void upd(int x, const T &v) { for (int i = x; i <= n; i += i & -i) { a[i] = a[i] + v; } } inline int kth(const T &k) { int x = 0; T cur{}; for (int i = 21; i >= 0; i--) { if (x + (1 << i) <= n && cur + a[x + (1 << i)] < k) { x += (1 << i); cur = cur + a[x]; } } return x + 1; } }; Fenwick<int> bit; bool vis[N]; int nxt[N], inxt = 0; int up[N]; void dfs(int i, int pre){ for(auto &[x, id] : adj[i]){ if(x == pre){ id = up[i]; continue; }; id = ++ee; p[x] = i, up[x] = ee; dfs(x, i); } } void dfs2(int i){ for(int j = (pt[i] + 1) % adj[i].size(); ; j = (j + 1) % adj[i].size()){ if(j == pt[i] && i > 1) break; int x = adj[i][j].first; vid[++timedfs] = x, eid[timedfs] = adj[i][j].second; dfs2(x); vid[++timedfs] = i, eid[timedfs] = adj[i][j].second; if(j == pt[i] && i == 1) break; } } void dfs3(int i){ while(true){ pt[i] = (pt[i] + 1) % adj[i].size(); int j = pt[i], x = adj[i][j].first, ee = adj[i][j].second; if(vis[ee]) break; if(x == p[i]){ nxt[inxt++] = i; break; } bit.upd(tid[ee][0], 1); bit.upd(tid[ee][1], 1); s += 2; dfs3(x); vis[ee] = true; } } void solve(){ cin >> n >> q; for(int i = 1; i <= n; i++){ int sz; cin >> sz; adj[i].reserve(sz); for(int j = 0; j < sz; j++){ int x; cin >> x; adj[i].push_back({x, 0}); } } dfs(1, 1); for(int i = 2; i <= n; i++){ for(int j = 0; j < (int) adj[i].size(); j++){ if(adj[i][j].first == p[i]) pt[i] = j; } } dfs2(1); bit.init(timedfs); memset(pt, 0, sizeof pt); vector<pair<long long, int>> b; for(int i = 1; i <= q; i++){ long long x; cin >> x; b.push_back({x, i}); } sort(b.begin(), b.end()); for(int i = 1; i <= timedfs; i++) tid[eid[i]].push_back(i); queue<int> qu; qu.push(1); long long total = 0; int qid = 0; while(s < timedfs){ inxt = 0; int last = total; while(!qu.empty()){ dfs3(qu.front()); qu.pop(); } for(int i = 0; i < inxt; i++){ qu.push(nxt[i]); } total += s; while(qid < q && b[qid].first <= total){ ans[b[qid++].second] = vid[bit.kth(b[qid].first - last)]; } } while(qid < q){ ans[b[qid++].second] = vid[(b[qid].first - total - 1) % timedfs + 1]; } for(int i = 1; i <= q; i++) cout << ans[i] << '\n'; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); #define task "MAZE" if(fopen(task ".inp", "r")){ freopen(task ".inp", "r", stdin); freopen(task ".out", "w", stdout); } solve();

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:146:13: error: expected '}' at end of input
  146 |     solve();
      |             ^
Main.cpp:138:14: note: to match this '{'
  138 | signed main(){
      |              ^
Main.cpp:143:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  143 |         freopen(task ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:144:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  144 |         freopen(task ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~