Submission #809050

#TimeUsernameProblemLanguageResultExecution timeMemory
809050math_rabbit_1028Werewolf (IOI18_werewolf)C++14
100 / 100
1428 ms257564 KiB
#include "werewolf.h" #include <bits/stdc++.h> using namespace std; int n, m, q; vector<int> adj[202020]; struct disjoint { int root[202020], num[202020]; int r, mn[404040], mx[404040], par[404040]; vector<int> child[404040]; int _find(int v) { if (v == root[v]) return v; else return root[v] = _find(root[v]); } void _union(int v, int u) { v = _find(v); u = _find(u); if (v == u) return; if (v > u) swap(u, v); root[u] = v; child[r].push_back(num[v]); child[r].push_back(num[u]); par[num[v]] = par[num[u]] = r; mn[r] = min(mn[num[v]], mn[num[u]]); mx[r] = max(mx[num[v]], mx[num[u]]); num[v] = r; r++; } int ord[808080], lt[404040], rt[404040], s, inv[202020]; void ett() { s = 0; DFS(r - 1); for (int i = 1; i <= s; i++) { if (ord[i] < n) inv[ord[i]] = i; } } void DFS(int v) { ord[++s] = v; lt[v] = s; for (int i = 0; i < child[v].size(); i++) { int next = child[v][i]; DFS(next); ord[++s] = v; } rt[v] = s; } int table[404040][20]; void sparse() { for (int i = 0; i < r; i++) table[i][0] = par[i]; for (int k = 1; k < 20; k++) { for (int i = 0; i < r; i++) { if (table[i][k - 1] < 0) table[i][k] = -1; else table[i][k] = table[table[i][k - 1]][k - 1]; } } } pair<int, int> get_segment(int st, int limit, int f) { if (f) { int now = st; for (int k = 19; k >= 0; k--) { if (table[now][k] != -1 && mn[table[now][k]] >= limit) now = table[now][k]; } return {lt[now], rt[now]}; } else { int now = st; for (int k = 19; k >= 0; k--) { if (table[now][k] != -1 && mx[table[now][k]] <= limit) now = table[now][k]; } return {lt[now], rt[now]}; } } } left_tree, right_tree; void make_left_tree() { for (int i = 0; i < n; i++) left_tree.root[i] = i; for (int i = 0; i < n; i++) left_tree.num[i] = i; for (int i = 0; i < n; i++) left_tree.mn[i] = left_tree.mx[i] = i; left_tree.r = n; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < adj[i].size(); j++) { int v = adj[i][j]; if (v >= i) left_tree._union(i, v); } } left_tree.par[left_tree.r - 1] = -1; } void make_right_tree() { for (int i = 0; i < n; i++) right_tree.root[i] = i; for (int i = 0; i < n; i++) right_tree.num[i] = i; for (int i = 0; i < n; i++) right_tree.mn[i] = right_tree.mx[i] = i; right_tree.r = n; for (int i = 0; i <= n - 1; i++) { for (int j = 0; j < adj[i].size(); j++) { int v = adj[i][j]; if (v <= i) right_tree._union(i, v); } } right_tree.par[right_tree.r - 1] = -1; } vector< pair<int, int> > p; struct node { int sum; int lt, rt; }; vector<node> tree; vector< pair<int, int> > pst_root; void init(int v, int st, int ed) { tree.push_back({0, 2 * v, 2 * v + 1}); if (st == ed) return; int mid = (st + ed) / 2; init(2 * v, st, mid); init(2 * v + 1, mid + 1, ed); tree[v].sum = tree[tree[v].lt].sum + tree[tree[v].rt].sum; } void update(int v, int st, int ed, int idx, int val) { if (st > idx || ed < idx) return; if (st == ed) { tree[v].sum += 1; return; } int mid = (st + ed) / 2; if (idx <= mid) { tree.push_back(tree[tree[v].lt]); tree[v].lt = tree.size() - 1; update(tree[v].lt, st, mid, idx, val); } else { tree.push_back(tree[tree[v].rt]); tree[v].rt = tree.size() - 1; update(tree[v].rt, mid + 1, ed, idx, val); } tree[v].sum = tree[tree[v].lt].sum + tree[tree[v].rt].sum; } int get(int v, int st, int ed, int lt, int rt) { if (st > rt || ed < lt) return 0; if (lt <= st && ed <= rt) return tree[v].sum; int mid = (st + ed) / 2; return get(tree[v].lt, st, mid, lt, rt) + get(tree[v].rt, mid + 1, ed, lt, rt); } vector<int> ans; 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) { n = N; m = X.size(); for (int i = 0; i < m; i++) { adj[X[i]].push_back(Y[i]); adj[Y[i]].push_back(X[i]); } make_left_tree(); left_tree.ett(); left_tree.sparse(); make_right_tree(); right_tree.ett(); right_tree.sparse(); q = S.size(); for (int i = 0; i < q; i++) ans.push_back(0); pst_root.push_back({0, 0}); tree.push_back({0, 0, 0}); init(pst_root.back().second, 1, 4 * n); for (int i = 0; i < n; i++) p.push_back({left_tree.inv[i], right_tree.inv[i]}); sort(p.begin(), p.end()); for (int i = 0; i < n; i++) { tree.push_back(tree[pst_root.back().second]); pst_root.push_back({p[i].first, tree.size() - 1}); update(pst_root.back().second, 1, 4 * n, p[i].second, 1); } //cout << get(pst_root.back().second, 1, 4 * n, 1, 4 * n) << "\n"; for (int i = 0; i < q; i++) { int s = S[i], e = E[i], l = L[i], r = R[i]; pair<int, int> left_segment = left_tree.get_segment(s, l, 1); pair<int, int> right_segment = right_tree.get_segment(e, r, 0); /* int ch[3030]; for (int j = 0; j < n; j++) ch[j] = 0; for (int j = left_segment.first; j <= left_segment.second; j++) if (left_tree.ord[j] < n) ch[left_tree.ord[j]] = 1; for (int j = right_segment.first; j <= right_segment.second; j++) if (right_tree.ord[j] < n && ch[right_tree.ord[j]] == 1) ans[i] = 1; */ int cnt = 0; int ed = lower_bound(pst_root.begin(), pst_root.end(), make_pair(left_segment.second + 1, 0)) - pst_root.begin(); if (ed > 0) cnt += get(pst_root[ed - 1].second, 1, 4 * n, right_segment.first, right_segment.second); int st = lower_bound(pst_root.begin(), pst_root.end(), make_pair(left_segment.first, 0)) - pst_root.begin(); if (st > 0) cnt -= get(pst_root[st - 1].second, 1, 4 * n, right_segment.first, right_segment.second); if (cnt > 0) ans[i] = 1; } return ans; }

Compilation message (stderr)

werewolf.cpp: In member function 'void disjoint::DFS(int)':
werewolf.cpp:41:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |         for (int i = 0; i < child[v].size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~
werewolf.cpp: In function 'void make_left_tree()':
werewolf.cpp:85:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         for (int j = 0; j < adj[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~
werewolf.cpp: In function 'void make_right_tree()':
werewolf.cpp:99:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |         for (int j = 0; j < adj[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...