Submission #222771

# Submission time Handle Problem Language Result Execution time Memory
222771 2020-04-14T01:46:53 Z rama_pang Werewolf (IOI18_werewolf) C++14
100 / 100
1333 ms 127256 KB
#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;

// Disjoint Set
vector<int> disj;
int Find(int x) {
  return disj[x] == x ? x : disj[x] = Find(disj[x]);
}

class BIT {
 private:
  int sz;
  vector<int> tree;
  
  void Update_(int p, int x) {
    for (int i = p + 1; i <= sz; i += (i & -i)) {
      tree[i - 1] += x;
    }
  }

  int Query_(int p) {
    int res = 0;
    for (int i = p + 1; i > 0; i -= (i & -i)) {
      res += tree[i - 1];
    }
    return res;
  }

 public:
  BIT() {}
  BIT(int n) : sz(n) { tree.assign(sz, 0); }

  void Update(int p, int x) {
    return Update_(p, x);
  }

  int Query(int L, int R) {
    return Query_(R) - Query_(L - 1);
  }
};

// Line Sweep
struct Event {
  int type; // 0 (point), 1 (query end), -1 (query start)
  int lo, hi;
  int id;
  Event() {}
  Event(int t, int l, int h, int i) : type(t), lo(l), hi(h), id(i) {}
  bool operator < (const Event &o) const { return type < o.type; }
};

vector<vector<Event>> events;

void AddPoint(int x, int y) {
  events[x].emplace_back(0, y, y, -1);
}

void AddQuery(int x_1, int x_2, int y_1, int y_2) {
  static int id_ = 0;
  events[x_1].emplace_back(-1, y_1, y_2, id_);
  events[x_2].emplace_back(+1, y_1, y_2, id_);
  id_++;
}

void LineSweep(int n, vector<int> &InsideRectangle) {
  BIT tree(n);
  for (int x = 0; x < n; x++) {
    sort(begin(events[x]), end(events[x]));
    for (auto e : events[x]) {
      if (e.type == 0) { // Add Point
        tree.Update(e.lo, +1);
      } else { // Process Query
        InsideRectangle[e.id] += e.type * tree.Query(e.lo, e.hi);
      }
    }
  }
}

vector<int> check_validity(int N, vector<int> X, vector<int> Y, vector<int> S, 
                           vector<int> E, vector<int> L, vector<int> R) {
  
  disj.resize(N), events.resize(2 * N);
  vector<vector<int>> adj(N);
  for (int i = 0; i < (int) X.size(); i++) {
    adj[X[i]].emplace_back(Y[i]);
    adj[Y[i]].emplace_back(X[i]);
  }

  vector<int> LineL, LineR;
  vector<vector<int>> AncL(N, vector<int>(20, -1)), AncR(N, vector<int>(20, -1));

  function<void(int, vector<vector<int>>&, vector<vector<int>>&, vector<int>&)> dfs = [&](
      int n, vector<vector<int>> &adjList, vector<vector<int>> &anc, vector<int> &v) {
    v.emplace_back(n);
    for (int j = 1; j < 20; j++) {
      if (anc[n][j - 1] != -1) {
        anc[n][j] = anc[anc[n][j - 1]][j - 1];
      }
    }
    for (auto i : adjList[n]) {
      anc[i][0] = n;
      dfs(i, adjList, anc, v);
    }
    v.emplace_back(n);
  };

  { // Created rooted tree for L (low-population restricted)
    iota(begin(disj), end(disj), 0);
    vector<vector<int>> tree(N);
    for (int u = N - 1; u >= 0; u--) {
      for (auto v : adj[u]) {
        if (v < u) continue;
        if (u == Find(v)) continue;
        tree[u].emplace_back(Find(v));
        disj[Find(v)] = u;
      }
    }
    AncL[0][0] = -1;
    dfs(0, tree, AncL, LineL);
  }

  { // Created rooted tree for R (high-population restricted)
    iota(begin(disj), end(disj), 0);
    vector<vector<int>> tree(N);
    for (int u = 0; u < N; u++) {
      for (auto v : adj[u]) {
        if (v > u) continue;
        if (u == Find(v)) continue;
        tree[u].emplace_back(Find(v));
        disj[Find(v)] = u;
      }
    }
    AncR[N - 1][0] = -1;
    dfs(N - 1, tree, AncR, LineR);
  }

  vector<pair<int, int>> posL(N, {-1, -1}), posR(N, {-1, -1});
  for (int i = 0; i < 2 * N; i++) {
    if (posL[LineL[i]].first == -1) {
      posL[LineL[i]].first = i;
    } else {
      posL[LineL[i]].second = i;
    }
    if (posR[LineR[i]].first == -1) {
      posR[LineR[i]].first = i;
    } else {
      posR[LineR[i]].second = i;
    }
  }

  for (int i = 0; i < N; i++) {
    AddPoint(posL[i].first, posR[i].first);
  }
  
  int Q = L.size();
  for (int i = 0; i < Q; i++) {
    int s = S[i];
    int e = E[i];
    for (int j = 19; j >= 0; j--) {
      if (AncL[s][j] != -1 && AncL[s][j] >= L[i]) s = AncL[s][j];
      if (AncR[e][j] != -1 && AncR[e][j] <= R[i]) e = AncR[e][j];
    }
    AddQuery(posL[s].first, posL[s].second, posR[e].first, posR[e].second);
  }

  vector<int> res(Q, 0);
  LineSweep(2 * N, res);
  for (int i = 0; i < Q; i++) res[i] = res[i] > 0;
  return res;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 13 ms 1920 KB Output is correct
11 Correct 16 ms 1920 KB Output is correct
12 Correct 12 ms 1920 KB Output is correct
13 Correct 13 ms 2176 KB Output is correct
14 Correct 16 ms 2176 KB Output is correct
15 Correct 16 ms 2048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1124 ms 102792 KB Output is correct
2 Correct 1193 ms 116628 KB Output is correct
3 Correct 1037 ms 112248 KB Output is correct
4 Correct 873 ms 110016 KB Output is correct
5 Correct 886 ms 110224 KB Output is correct
6 Correct 988 ms 111248 KB Output is correct
7 Correct 938 ms 109296 KB Output is correct
8 Correct 1008 ms 116752 KB Output is correct
9 Correct 797 ms 110880 KB Output is correct
10 Correct 678 ms 109596 KB Output is correct
11 Correct 717 ms 109412 KB Output is correct
12 Correct 764 ms 109120 KB Output is correct
13 Correct 1197 ms 119064 KB Output is correct
14 Correct 1272 ms 119112 KB Output is correct
15 Correct 1326 ms 118936 KB Output is correct
16 Correct 1333 ms 119136 KB Output is correct
17 Correct 923 ms 109972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 13 ms 1920 KB Output is correct
11 Correct 16 ms 1920 KB Output is correct
12 Correct 12 ms 1920 KB Output is correct
13 Correct 13 ms 2176 KB Output is correct
14 Correct 16 ms 2176 KB Output is correct
15 Correct 16 ms 2048 KB Output is correct
16 Correct 1124 ms 102792 KB Output is correct
17 Correct 1193 ms 116628 KB Output is correct
18 Correct 1037 ms 112248 KB Output is correct
19 Correct 873 ms 110016 KB Output is correct
20 Correct 886 ms 110224 KB Output is correct
21 Correct 988 ms 111248 KB Output is correct
22 Correct 938 ms 109296 KB Output is correct
23 Correct 1008 ms 116752 KB Output is correct
24 Correct 797 ms 110880 KB Output is correct
25 Correct 678 ms 109596 KB Output is correct
26 Correct 717 ms 109412 KB Output is correct
27 Correct 764 ms 109120 KB Output is correct
28 Correct 1197 ms 119064 KB Output is correct
29 Correct 1272 ms 119112 KB Output is correct
30 Correct 1326 ms 118936 KB Output is correct
31 Correct 1333 ms 119136 KB Output is correct
32 Correct 923 ms 109972 KB Output is correct
33 Correct 1166 ms 112784 KB Output is correct
34 Correct 421 ms 40824 KB Output is correct
35 Correct 1288 ms 116604 KB Output is correct
36 Correct 1119 ms 112364 KB Output is correct
37 Correct 1249 ms 115724 KB Output is correct
38 Correct 1180 ms 113312 KB Output is correct
39 Correct 1237 ms 127256 KB Output is correct
40 Correct 1267 ms 121232 KB Output is correct
41 Correct 1006 ms 113652 KB Output is correct
42 Correct 819 ms 110476 KB Output is correct
43 Correct 1325 ms 121740 KB Output is correct
44 Correct 1122 ms 115468 KB Output is correct
45 Correct 984 ms 126736 KB Output is correct
46 Correct 981 ms 126068 KB Output is correct
47 Correct 1244 ms 119316 KB Output is correct
48 Correct 1295 ms 119060 KB Output is correct
49 Correct 1228 ms 119184 KB Output is correct
50 Correct 1225 ms 119188 KB Output is correct
51 Correct 1159 ms 120916 KB Output is correct
52 Correct 1148 ms 120668 KB Output is correct