Submission #966906

#TimeUsernameProblemLanguageResultExecution timeMemory
966906Nhoksocqt1Game (APIO22_game)C++17
100 / 100
3884 ms89280 KiB
#ifndef Nhoksocqt1 #include "game.h" #endif // Nhoksocqt1 #include<bits/stdc++.h> using namespace std; #define inf 0x3f3f3f3f #define sz(x) int((x).size()) #define fi first #define se second typedef long long ll; typedef pair<int, int> ii; template<class X, class Y> inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);} template<class X, class Y> inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);} mt19937 rng(2); //#define HAHA int Random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } const int MAXN = 300005; const int MAXM = 800005; vector<int> adj[MAXN], adjr[MAXN]; int min_bucket_to[MAXN][20], max_bucket_from[MAXN][20]; int id_bucket[MAXN], min_same[MAXN], min_spe_to[MAXN], max_spe_from[MAXN]; int numBlock, maxLog, numNode, numSpecial; bool inQuMax[MAXN], inQuMin[MAXN], dx[MAXN]; void init(int _N, int _K) { numNode = _N, numSpecial = _K; maxLog = 31 - __builtin_clz(numSpecial); for (int i = 0; i < numNode; ++i) { if(i < numSpecial) { min_spe_to[i] = max_spe_from[i] = i; for (int j = 0; j <= maxLog; ++j) min_bucket_to[i][j] = max_bucket_from[i][j] = i >> j & 1; id_bucket[i] = i; min_same[i] = 0; } else { max_spe_from[i] = -1; min_same[i] = maxLog + 1; min_spe_to[i] = numSpecial; for (int j = 0; j <= maxLog; ++j) min_bucket_to[i][j] = 2, max_bucket_from[i][j] = -1; } } } bool brute(int u, int v) { queue<int> qu; if(min_spe_to[u] > min(min_spe_to[v], v)) { min_spe_to[u] = min(min_spe_to[v], v); qu.push(u); } while(sz(qu)) { int u(qu.front()); qu.pop(); if(max_spe_from[u] >= min_spe_to[u]) return true; for (int it = 0; it < sz(adjr[u]); ++it) { int v(adjr[u][it]); if(min_spe_to[v] > min_spe_to[u]) { min_spe_to[v] = min_spe_to[u]; qu.push(v); } } } int max_spe_from_now = max(max_spe_from[u], (u < numSpecial ? u : -1)); if(max_spe_from[v] < max_spe_from_now) { max_spe_from[v] = max_spe_from_now; qu.push(v); } while(sz(qu)) { int u(qu.front()); qu.pop(); if(u >= numSpecial && max_spe_from[u] >= min_spe_to[u]) return true; for (int it = 0; it < sz(adj[u]); ++it) { int v(adj[u][it]); if(max_spe_from[v] < max_spe_from[u]) { max_spe_from[v] = max_spe_from[u]; qu.push(v); } } } return false; } bool magicFunc(int u, int v) { queue<int> quMax, quMin; for (int k = maxLog; k >= 0; --k) { if(max(min_same[u], min_same[v]) <= k + 1 && (id_bucket[u] >> (k + 1)) == (id_bucket[v] >> (k + 1))) { if(min_bucket_to[u][k] > min_bucket_to[v][k]) { min_bucket_to[u][k] = min_bucket_to[v][k]; if(!inQuMin[u]) { quMin.push(u); inQuMin[u] = 1; } } if(max_bucket_from[v][k] < max_bucket_from[u][k]) { max_bucket_from[v][k] = max_bucket_from[u][k]; if(!inQuMax[v]) { quMax.push(v); inQuMax[v] = 1; } } } vector<int> nodes; while(sz(quMin)) { int u(quMin.front()); quMin.pop(); inQuMin[u] = 0; if(max_bucket_from[u][k] > min_bucket_to[u][k]) return true; if(max_bucket_from[u][k] == min_bucket_to[u][k]) { id_bucket[u] |= (max_bucket_from[u][k] << k); min_same[u] = min(min_same[u], k); if(k == 0) return true; if(!dx[u]) { nodes.push_back(u); dx[u] = 1; } } //cout << "MIN BUCKET " << k << " TO " << u << ' ' << min_same[u] << ' ' << min_bucket_to[u][k] << '\n'; for (int it = 0; it < sz(adjr[u]); ++it) { int v(adjr[u][it]); if(min_same[v] > k + 1 || (id_bucket[u] >> (k + 1)) != (id_bucket[v] >> (k + 1))) continue; if(min_bucket_to[v][k] > min_bucket_to[u][k]) { min_bucket_to[v][k] = min_bucket_to[u][k]; if(!inQuMin[v]) { quMin.push(v); inQuMin[v] = 1; } } } } while(sz(quMax)) { int u(quMax.front()); quMax.pop(); inQuMax[u] = 0; //cout << "MAX BUCKET " << k << " FROM " << u << ' ' << min_same[u] << ' ' << max_bucket_from[u][k] << '\n'; if(max_bucket_from[u][k] > min_bucket_to[u][k]) return true; if(max_bucket_from[u][k] == min_bucket_to[u][k]) { id_bucket[u] |= (max_bucket_from[u][k] << k); min_same[u] = min(min_same[u], k); if(k == 0) return true; if(!dx[u]) { nodes.push_back(u); dx[u] = 1; } } for (int it = 0; it < sz(adj[u]); ++it) { int v(adj[u][it]); if(min_same[v] > k + 1 || (id_bucket[u] >> (k + 1)) != (id_bucket[v] >> (k + 1))) continue; if(max_bucket_from[v][k] < max_bucket_from[u][k]) { max_bucket_from[v][k] = max_bucket_from[u][k]; if(!inQuMax[v]) { quMax.push(v); inQuMax[v] = 1; } } } } for (int it = 0; it < sz(nodes); ++it) { int u(nodes[it]); dx[u] = 0; //cout << u << ' ' << min_same[u] << ' ' << id_bucket[u] << ":\n"; for (int it = 0; it < sz(adj[u]); ++it) { int v(adj[u][it]); //cout << v << ' ' << min_same[v] << ' ' << id_bucket[v] << ' ' << (id_bucket[v] >> k) << ' ' << (id_bucket[u] >> k) << '\n'; if(max(min_same[u], min_same[v]) > k || (id_bucket[u] >> k) != (id_bucket[v] >> k)) continue; if(min_bucket_to[u][k - 1] > min_bucket_to[v][k - 1]) { min_bucket_to[u][k - 1] = min_bucket_to[v][k - 1]; if(!inQuMin[u]) { quMin.push(u); inQuMin[u] = 1; } } } for (int it = 0; it < sz(adjr[u]); ++it) { int v(adjr[u][it]); if(max(min_same[u], min_same[v]) > k || (id_bucket[u] >> k) != (id_bucket[v] >> k)) continue; if(max_bucket_from[u][k - 1] < max_bucket_from[v][k - 1]) { max_bucket_from[u][k - 1] = max_bucket_from[v][k - 1]; if(!inQuMax[u]) { quMax.push(u); inQuMax[u] = 1; } } } } } return false; } int add_teleporter(int u, int v) { if(max(u, v) < numSpecial) return (u >= v); if(u == v) return false; adj[u].push_back(v); adjr[v].push_back(u); //cout << brute(u, v) << ' ' << magicFunc(u, v) << '\n'; return false; /*if(numNode <= 30000 && numSpecial <= 1000) { return brute(u, v); } else*/ { return magicFunc(u, v); } return false; } #ifdef Nhoksocqt1 int main(void) { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define TASK "game" if(fopen(TASK".inp", "r")) { freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); } int n, k, m; cin >> n >> k >> m; init(n, k); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; //u = Random(0, n - 2), v = Random(max(k, u + 1), n - 1); if(Random(0, 1)) swap(u, v); int ans = add_teleporter(u, v); cout << "ANSWER FOR EDGE " << u << " TO " << v << ": " << ans << '\n'; if(ans == 1) break; } return 0; } #endif // Nhoksocqt1
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...