Submission #985960

#TimeUsernameProblemLanguageResultExecution timeMemory
985960MateiKing80Werewolf (IOI18_werewolf)C++14
100 / 100
799 ms116264 KiB
#include <bits/stdc++.h> #include "werewolf.h" using namespace std; #define all(a) (a).begin(), (a).end() const int NMAX = 2e5, MMAX = 4e5; vector<int> vec[NMAX + 5]; struct DSU { int n; vector<int> papa; DSU(int N) { papa.resize(N + 1); for(int i = 1; i <= N; i ++) papa[i] = i; n = N; } int real_papa(int nod) { if(papa[nod] == nod) return nod; return papa[nod] = real_papa(papa[nod]); } bool query(int u, int v) { return real_papa(u) == real_papa(v); } void join(int u, int v) { papa[real_papa(v)] = real_papa(u); } }; struct Arbore { vector<vector<int>> lift, vec; int n, bitMax, root; Arbore(int N, int ROOT) { n = N; vec.resize(N + 1); bitMax = 20; root = ROOT; } void add_edge(int u, int v) { vec[u].push_back(v); vec[v].push_back(u); } void dfsLift(int nod, int papa) { lift[0][nod] = papa; for(auto i : vec[nod]) if(i != papa) dfsLift(i, nod); } void makeLift() { lift.resize(bitMax, vector<int> (n + 1)); dfsLift(root, 0); for(int i = 1; i < bitMax; i ++) for(int j = 1; j <= n; j ++) lift[i][j] = lift[i - 1][lift[i - 1][j]]; } int findSmallestBigger(int u, int val) { int bit = bitMax - 1; while(bit >= 0) { if(lift[bit][u] >= val) u = lift[bit][u]; bit --; } return u; } int findBiggestSmaller(int u, int val) { int bit = bitMax - 1; while(bit >= 0) { if(lift[bit][u] <= val && lift[bit][u] != 0) u = lift[bit][u]; bit --; } return u; } void dfsOrder(int nod, int papa, vector<int> &ord, vector<pair<int, int>> &subTree) { ord.push_back(nod); subTree[nod].first = ord.size() - 1; for(auto i : vec[nod]) if(i != papa) dfsOrder(i, nod, ord, subTree); subTree[nod].second = ord.size() - 1; } vector<pair<int, int>> preOrder() { vector<int> ord(1, 0); vector<pair<int, int>> subTree(n + n); dfsOrder(root, 0, ord, subTree); return subTree; } }; struct Fenwick { vector<int> aib; int n; Fenwick(int N) { n = N; aib.resize(N + 1); } inline int lsb(int x) { return x & -x; } void update(int pos, int val) { while(pos <= n) { aib[pos] += val; pos += lsb(pos); } } int query(int pos) { int ans = 0; while(pos) { ans += aib[pos]; pos -= lsb(pos); } return ans; } }; struct ura { int pos, val, index, inm; }; bool operator < (ura a, ura b) { return a.val < b.val; } vector<int> check_validity(int n, vector<int> u, vector<int> v, vector<int> s, vector<int> f, vector<int> l, vector<int> r) { int m = u.size(), q = s.size(); vector<vector<int>> vec(n + 1); for(int i = 0; i < m; i ++) vec[u[i] + 1].push_back(v[i] + 1), vec[v[i] + 1].push_back(u[i] + 1); for(int i = 0; i < q; i ++) s[i] ++, f[i] ++, l[i] ++, r[i] ++; DSU dsuOm(n), dsuLup(n); Arbore om(n, 1), lup(n, n); for(int i = n; i; i --) { for(auto j : vec[i]) if(j >= i && !dsuOm.query(i, j)) { om.add_edge(dsuOm.real_papa(i), dsuOm.real_papa(j)); dsuOm.join(i, j); } } for(int i = 1; i <= n; i ++) { for(auto j : vec[i]) if(j <= i && !dsuLup.query(i, j)) { lup.add_edge(dsuLup.real_papa(i), dsuLup.real_papa(j)); dsuLup.join(i, j); } } om.makeLift(), lup.makeLift(); auto ordOm = om.preOrder(), ordLup = lup.preOrder(); vector<int> ans(q), posLupSchimbat(n + 1); Fenwick aib(n); for(int i = 1; i <= n; i ++) posLupSchimbat[ordOm[i].first] = ordLup[i].first; vector<ura> aux; for(int i = 0; i < q; i ++) { if(s[i] < l[i] || f[i] > r[i]) continue; l[i] = om.findSmallestBigger(s[i], l[i]); r[i] = lup.findBiggestSmaller(f[i], r[i]); aux.push_back({ordLup[r[i]].second, ordOm[l[i]].second, i, 1}); aux.push_back({ordLup[r[i]].second, ordOm[l[i]].first - 1, i, -1}); aux.push_back({ordLup[r[i]].first - 1, ordOm[l[i]].second, i, -1}); aux.push_back({ordLup[r[i]].first - 1, ordOm[l[i]].first - 1, i, 1}); } sort(all(aux)); int p = 0; for(int i = 0; i <= n; i ++) { while(p < aux.size() && aux[p].val == i) ans[aux[p].index] += aib.query(aux[p].pos) * aux[p].inm, p ++; if(i != n) aib.update(posLupSchimbat[i + 1], 1); } for(int i = 0; i < q; i ++) ans[i] = ans[i] != 0; return ans; }

Compilation message (stderr)

werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:224:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<ura>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  224 |         while(p < aux.size() && aux[p].val == i)
      |               ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...