Submission #531433

# Submission time Handle Problem Language Result Execution time Memory
531433 2022-02-28T16:56:46 Z Alex_tz307 Werewolf (IOI18_werewolf) C++17
100 / 100
954 ms 172588 KB
#include <bits/stdc++.h>
#include "werewolf.h"

using namespace std;
using vi = vector<int>;

const int kN = 2e5;
int n, m, q, stamp;
vi g[kN];
vector<pair<int, int>> t;

struct DSU {
  vi p;

  void init() {
    p.clear();
    p.resize(n);
    iota(p.begin(), p.end(), 0);
  }

  int root(int x) {
    if (x == p[x]) {
      return x;
    }
    return p[x] = root(p[x]);
  }

  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return;
    }
    p[x] = p.size();
    p[y] = p[x];
    p.emplace_back(p[x]);
    t.emplace_back(x, y);
  }
};

pair<int, int> dfs(int v) {
  if (v < n) {
    t[v].first = t[v].second = stamp;
    stamp += 1;
    return {stamp - 1, stamp - 1};
  }
  pair<int, int> st = dfs(t[v].first);
  pair<int, int> dr = dfs(t[v].second);
  t[v].first = min(st.first, dr.first);
  t[v].second = max(st.second, dr.second);
  return t[v];
}

struct node {
  int sum;
  node* l;
  node* r;

  node() : sum(0), l(nullptr), r(nullptr) {}
};

void build(node* x, int lx, int rx) {
  if (lx == rx) {
    return;
  }
  int mid = (lx + rx) / 2;
  x->l = new node();
  build(x->l, lx, mid);
  x->r = new node();
  build(x->r, mid + 1, rx);
}

void update(node* prev, node* curr, int lx, int rx, int pos) {
  if (lx == rx) {
    curr->sum += 1;
    return;
  }
  int mid = (lx + rx) / 2;
  if (pos <= mid) {
    curr->r = prev->r;
    curr->l = new node();
    update(prev->l, curr->l, lx, mid, pos);
  } else {
    curr->l = prev->l;
    curr->r = new node();
    update(prev->r, curr->r, mid + 1, rx, pos);
  }
  curr->sum = 0;
  if (curr->l) {
    curr->sum += curr->l->sum;
  }
  if (curr->r) {
    curr->sum += curr->r->sum;
  }
}

int query(node* x, int lx, int rx, int st, int dr) {
  if (st <= lx && rx <= dr) {
    return x->sum;
  }
  int mid = (lx + rx) / 2, ans = 0;
  if (st <= mid) {
    ans += query(x->l, lx, mid, st, dr);
  }
  if (mid < dr) {
    ans += query(x->r, mid + 1, rx, st, dr);
  }
  return ans;
}

node* roots[1 + kN];

struct query_t {
  int x1, x2;
  int y1, y2;
  int index;

  bool operator < (const query_t &rhs) const {
    return x2 < rhs.x2;
  }
};

vi check_validity(int N, vi X, vi Y, vi S, vi E, vi L, vi R) {
  n = N;
  m = X.size();
  q = S.size();
  for (int i = 0; i < m; ++i) {
    int u = X[i], v = Y[i];
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  S.emplace_back(0);
  E.emplace_back(0);
  L.emplace_back(0);
  R.emplace_back(n - 1);
  q += 1;
  vi idx(q);
  iota(idx.begin(), idx.end(), 0);
  sort(idx.begin(), idx.end(), [&](const int &i, const int &j) -> bool {
    return L[i] > L[j];
  });
  DSU dsu;
  dsu.init();
  vi rep(q);
  t.resize(n);
  int u = n - 1;
  for (int qq = 0; qq < q; ++qq) {
    int i = idx[qq];
    while (u >= L[i]) {
      for (int v : g[u]) {
        if (v >= L[i]) {
          dsu.unite(u, v);
        }
      }
      u -= 1;
    }
    rep[i] = dsu.root(S[i]);
  }
  stamp = 0;
  dfs(t.size() - 1);
  vector<query_t> queries(q);
  vector<pair<int, int>> points(n);
  for (int i = 0; i < q; ++i) {
    queries[i].x1 = t[rep[i]].first;
    queries[i].x2 = t[rep[i]].second;
    queries[i].index = i;
  }
  for (int i = 0; i < n; ++i) {
    points[i].first = t[i].first;
  }
  dsu.init();
  t.clear();
  t = vector<pair<int, int>>(n);
  sort(idx.begin(), idx.end(), [&](const int &i, const int &j) -> bool {
    return R[i] < R[j];
  });
  u = 0;
  for (int qq = 0; qq < q; ++qq) {
    int i = idx[qq];
    while (u <= R[i]) {
      for (int v : g[u]) {
        if (v <= R[i]) {
          dsu.unite(u, v);
        }
      }
      u += 1;
    }
    rep[i] = dsu.root(E[i]);
  }
  stamp = 0;
  dfs(t.size() - 1);
  for (int i = 0; i < q; ++i) {
    queries[i].y1 = t[rep[i]].first;
    queries[i].y2 = t[rep[i]].second;
  }
  for (int i = 0; i < n; ++i) {
    points[i].second = t[i].first;
  }
  sort(queries.begin(), queries.end());
  sort(points.begin(), points.end());
  vector<int> sol(q - 1);
  roots[0] = new node();
  build(roots[0], 0, n - 1);
  int ptr = 0;
  for (auto it : queries) {
    if (it.index == q - 1) {
      continue;
    }
    while (ptr < n && points[ptr].first <= it.x2) {
      roots[ptr + 1] = new node();
      update(roots[ptr], roots[ptr + 1], 0, n - 1, points[ptr].second);
      ptr += 1;
    }
    int l = 0, r = n - 1, pos = 0;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (points[mid].first >= it.x1) {
        pos = mid;
        r = mid - 1;
      } else {
        l = mid + 1;
      }
    }
    if (query(roots[ptr], 0, n - 1, it.y1, it.y2) - query(roots[pos], 0, n - 1, it.y1, it.y2)) {
      sol[it.index] = 1;
    }
  }
  return sol;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4996 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 3 ms 4996 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 3 ms 5000 KB Output is correct
6 Correct 3 ms 4996 KB Output is correct
7 Correct 4 ms 5004 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4996 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 3 ms 4996 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 3 ms 5000 KB Output is correct
6 Correct 3 ms 4996 KB Output is correct
7 Correct 4 ms 5004 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 10 ms 6988 KB Output is correct
11 Correct 9 ms 6932 KB Output is correct
12 Correct 9 ms 6860 KB Output is correct
13 Correct 11 ms 6988 KB Output is correct
14 Correct 10 ms 6960 KB Output is correct
15 Correct 11 ms 6988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 724 ms 166080 KB Output is correct
2 Correct 853 ms 168884 KB Output is correct
3 Correct 790 ms 167084 KB Output is correct
4 Correct 758 ms 166188 KB Output is correct
5 Correct 733 ms 166116 KB Output is correct
6 Correct 698 ms 166144 KB Output is correct
7 Correct 571 ms 166004 KB Output is correct
8 Correct 858 ms 168820 KB Output is correct
9 Correct 780 ms 167052 KB Output is correct
10 Correct 595 ms 165924 KB Output is correct
11 Correct 618 ms 163752 KB Output is correct
12 Correct 635 ms 166040 KB Output is correct
13 Correct 729 ms 168900 KB Output is correct
14 Correct 779 ms 168892 KB Output is correct
15 Correct 737 ms 168884 KB Output is correct
16 Correct 733 ms 168884 KB Output is correct
17 Correct 613 ms 165984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4996 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 3 ms 4996 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 3 ms 5000 KB Output is correct
6 Correct 3 ms 4996 KB Output is correct
7 Correct 4 ms 5004 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 10 ms 6988 KB Output is correct
11 Correct 9 ms 6932 KB Output is correct
12 Correct 9 ms 6860 KB Output is correct
13 Correct 11 ms 6988 KB Output is correct
14 Correct 10 ms 6960 KB Output is correct
15 Correct 11 ms 6988 KB Output is correct
16 Correct 724 ms 166080 KB Output is correct
17 Correct 853 ms 168884 KB Output is correct
18 Correct 790 ms 167084 KB Output is correct
19 Correct 758 ms 166188 KB Output is correct
20 Correct 733 ms 166116 KB Output is correct
21 Correct 698 ms 166144 KB Output is correct
22 Correct 571 ms 166004 KB Output is correct
23 Correct 858 ms 168820 KB Output is correct
24 Correct 780 ms 167052 KB Output is correct
25 Correct 595 ms 165924 KB Output is correct
26 Correct 618 ms 163752 KB Output is correct
27 Correct 635 ms 166040 KB Output is correct
28 Correct 729 ms 168900 KB Output is correct
29 Correct 779 ms 168892 KB Output is correct
30 Correct 737 ms 168884 KB Output is correct
31 Correct 733 ms 168884 KB Output is correct
32 Correct 613 ms 165984 KB Output is correct
33 Correct 754 ms 166912 KB Output is correct
34 Correct 309 ms 32064 KB Output is correct
35 Correct 860 ms 169088 KB Output is correct
36 Correct 739 ms 166436 KB Output is correct
37 Correct 830 ms 168540 KB Output is correct
38 Correct 807 ms 166880 KB Output is correct
39 Correct 741 ms 172476 KB Output is correct
40 Correct 782 ms 171952 KB Output is correct
41 Correct 719 ms 168232 KB Output is correct
42 Correct 582 ms 166416 KB Output is correct
43 Correct 954 ms 171076 KB Output is correct
44 Correct 833 ms 168532 KB Output is correct
45 Correct 746 ms 172588 KB Output is correct
46 Correct 774 ms 172400 KB Output is correct
47 Correct 718 ms 168968 KB Output is correct
48 Correct 709 ms 169004 KB Output is correct
49 Correct 715 ms 168888 KB Output is correct
50 Correct 718 ms 168844 KB Output is correct
51 Correct 739 ms 172116 KB Output is correct
52 Correct 741 ms 172088 KB Output is correct