Submission #250535

#TimeUsernameProblemLanguageResultExecution timeMemory
250535fedoseevtimofeyWerewolf (IOI18_werewolf)C++14
15 / 100
1030 ms119456 KiB
#include <iostream> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <random> #include <iomanip> #include <functional> #include <cassert> using namespace std; typedef long long ll; #include "werewolf.h" struct Dsu { vector <int> p, cp; int n; int get(int u) { return (u == cp[u] ? u : cp[u] = get(cp[u])); } void join(int u, int v) { u = get(u); v = get(v); if (u != v) { int x = n++; p.push_back(x); cp.push_back(x); p[u] = p[v] = cp[u] = cp[v] = x; } } Dsu(int nn) { n = nn; p.resize(n), cp.resize(n); for (int i = 0; i < n; ++i) { p[i] = i; cp[i] = i; } } }; struct Fenwick { vector <int> f; int n; Fenwick(int nn) { n = nn; f.resize(n); } void modify(int i, int x) { for (; i < n; i |= i + 1) { f[i] += x; } } int get(int r) { int res = 0; for (; r >= 0; r &= r + 1, --r) { res += f[r]; } return res; } int sum(int l, int r) { return get(r) - get(l - 1); } }; struct Event { int x, l, r, tp, id; Event(int x, int l, int r, int tp, int id) : x(x), l(l), r(r), tp(tp), id(id) {} bool operator <(const Event &other) const { if (x != other.x) return x < other.x; return id < other.id; } }; std::vector<int> check_validity(int n, std::vector<int> x, std::vector<int> y, std::vector<int> s, std::vector<int> e, std::vector<int> l, std::vector<int> r) { vector <vector <int>> g(n); for (int i = 0; i < (int)x.size(); ++i) { g[x[i]].push_back(y[i]); g[y[i]].push_back(x[i]); } int q = s.size(); vector <vector <int>> who_l(n), who_r(n); for (int i = 0; i < n; ++i) { who_l[l[i]].push_back(i); who_r[r[i]].push_back(i); } vector <int> where_st(q), where_en(q); Dsu st(n), en(n); for (int i = n - 1; i >= 0; --i) { for (auto v : g[i]) { if (v >= i) { st.join(i, v); } } for (auto id : who_l[i]) { where_st[id] = st.get(s[id]); } } for (int i = 0; i < n; ++i) { for (auto v : g[i]) { if (v <= i) { en.join(i, v); } } for (auto id : who_r[i]) { where_en[id] = en.get(e[id]); } } vector <vector <int>> g_st(st.n), g_en(en.n); for (int i = 0; i < st.n; ++i) { int p = st.p[i]; if (i != p) { g_st[p].push_back(i); g_st[i].push_back(p); } } for (int i = 0; i < en.n; ++i) { int p = en.p[i]; if (i != p) { g_en[p].push_back(i); g_en[i].push_back(p); } } function <void(int, int, vector <vector <int>> &, vector <int> &, vector <int> &, vector <int>&)> dfs = [&] (int u, int p, vector <vector <int>> &g, vector <int> &l, vector <int> &r, vector <int> &e) { e.push_back(u); l[u] = (int)e.size() - 1; for (auto v : g[u]) { if (v != p) { dfs(v, u, g, l, r, e); } } r[u] = (int)e.size() - 1; }; vector <int> l_st(st.n), r_st(st.n), e_st; dfs(st.n - 1, -1, g_st, l_st, r_st, e_st); vector <int> l_en(en.n), r_en(en.n), e_en; dfs(en.n - 1, -1, g_en, l_en, r_en, e_en); vector <Event> events; for (int i = 0; i < q; ++i) { events.emplace_back(l_st[where_st[i]] - 1, l_en[where_en[i]], r_en[where_en[i]], -1, i); events.emplace_back(r_st[where_st[i]], l_en[where_en[i]], r_en[where_en[i]], 1, i); } for (int i = 0; i < n; ++i) { events.emplace_back(l_st[i], l_en[i], -1, -1, -1); } sort(events.begin(), events.end()); Fenwick F(en.n); vector <int> cnt(q); for (auto e : events) { if (e.id == -1) { F.modify(e.l, 1); } else { cnt[e.id] += e.tp * F.sum(e.l, e.r); } } vector <int> ans(q); for (int i = 0; i < q; ++i) { if (cnt[i] > 0) ans[i] = 1; else ans[i] = 0; } return ans; } #ifdef LOCAL namespace { int read_int() { int x; cin >> x; return x; } } // namespace int main() { ios_base::sync_with_stdio(false); cin.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif 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]); } } #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...