Submission #411355

#TimeUsernameProblemLanguageResultExecution timeMemory
411355usachevd0Werewolf (IOI18_werewolf)C++14
100 / 100
1425 ms173164 KiB
#include <bits/stdc++.h> #ifndef LOCAL #include "werewolf.h" #endif using namespace std; #define fi first #define se second #define mp make_pair #define pb push_back #define all(a) (a).begin(), (a).end() #define Time (clock() * 1.0 / CLOCKS_PER_SEC) using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; using ld = long double; template<typename T1, typename T2> bool chkmin(T1& x, T2 y) { return y < x ? (x = y, true) : false; } template<typename T1, typename T2> bool chkmax(T1& x, T2 y) { return y > x ? (x = y, true) : false; } void debug_out() { cerr << endl; } template<typename T1, typename... T2> void debug_out(T1 A, T2... B) { cerr << ' ' << A; debug_out(B...); } template<typename T> void mdebug_out(T* a, int n) { for (int i = 0; i < n; ++i) cerr << a[i] << ' '; cerr << endl; } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n) #else #define debug(...) 1337 #define mdebug(a, n) 1337 #endif template<typename T> ostream& operator << (ostream& stream, const vector<T>& v) { for (auto& x : v) stream << x << ' '; return stream; } template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) { return stream << p.first << ' ' << p.second; } const int INF32 = 1e9; const int maxN = 200005; int n, m, Q; vector<int> G[maxN]; int timer; int where[maxN]; vector<pii> hist[maxN]; vector<int> comp[maxN]; void joinR(int x, int y) { x = where[x]; y = where[y]; if (x == y) return; if (comp[x].size() < comp[y].size()) swap(x, y); for (int v : comp[y]) { where[v] = x; while (!hist[v].empty() && hist[v].back().fi == timer) hist[v].pop_back(); hist[v].emplace_back(timer, x); comp[x].push_back(v); } comp[y].clear(); } map<int, int> kek[maxN]; void joinL(int x, int y) { x = where[x]; y = where[y]; if (x == y) return; if (comp[x].size() < comp[y].size()) swap(x, y); for (int v : comp[y]) { where[v] = x; comp[x].push_back(v); } for (auto el : kek[y]) { int tau = el.se; int c = el.fi; if (!kek[x].count(c) || kek[x][c] > tau) { kek[x][c] = tau; } } comp[y].clear(); } int getCompR(int v, int tau) { int i = upper_bound(all(hist[v]), mp(tau, INF32)) - hist[v].begin() - 1; return hist[v][i].se; } vector<int> queL[maxN]; vector<int> check_validity(int _n, vector<int> eu, vector<int> ev, vector<int> qs, vector<int> qt, vector<int> ql, vector<int> qr) { n = _n; m = eu.size(); Q = qs.size(); for (int j = 0; j < m; ++j) { int u = eu[j], v = ev[j]; G[u].push_back(v); G[v].push_back(u); } for (int r = 0; r < n; ++r) { timer = r; where[r] = r; comp[r] = {r}; hist[r].emplace_back(timer, r); for (int y : G[r]) if (y <= r) { joinR(y, r); } } // for (int v = 0; v < n; ++v) // debug(v, hist[v]); for (int i = 0; i < Q; ++i) { if (qs[i] >= ql[i] && qt[i] <= qr[i]) queL[ql[i]].push_back(i); } for (int c = 0; c < n; ++c) { comp[c].clear(); where[c] = -1; } vector<int> ans(Q, 0); for (int l = n - 1; l >= 0; --l) { where[l] = l; comp[l] = {l}; for (auto el : hist[l]) kek[l][el.se] = el.fi; for (int y : G[l]) if (y >= l) joinL(y, l); for (int i : queL[l]) { int s = qs[i]; int t = qt[i]; int r = qr[i]; int ct = getCompR(t, r); int ws = where[s]; // debug(ct); // debug(ws, comp[ws]); // cerr << "kek[ws] = "; for (auto el : kek[ws]) cerr << el << ' '; cerr << endl; if (kek[ws].count(ct) && kek[ws][ct] <= r) ans[i] = 1; /*for (int v : comp[where[s]]) if (true) { bool good = false; for (auto el : hist[v]) { if (el.se == ct && el.fi <= r) { good = true; break; } } if (good) { ans[i] = 1; break; } }*/ } } return ans; } #ifdef LOCAL int read_int() { int x; if (scanf("%d", &x) != 1) { fprintf(stderr, "Error while reading input\n"); exit(1); } return x; } int32_t main() { #ifdef LOCAL freopen("in", "r", stdin); #endif ios::sync_with_stdio(0); cin.tie(0); int N = read_int(); int M = read_int(); int Q = read_int(); std::vector<int> X(M), Y(M), S(Q), E(Q), L(Q), R(Q); for (int j = 0; j < M; ++j) { X[j] = read_int(); Y[j] = read_int(); } for (int i = 0; i < Q; ++i) { S[i] = read_int(); E[i] = read_int(); L[i] = read_int(); R[i] = read_int(); } std::vector<int> A = check_validity(N, X, Y, S, E, L, R); for (size_t i = 0; i < A.size(); ++i) { printf("%d\n", A[i]); } return 0; } #endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...