답안 #815427

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
815427 2023-08-08T15:01:18 Z erray 늑대인간 (IOI18_werewolf) C++17
100 / 100
448 ms 79488 KB
#include "werewolf.h"
//author: erray
#include <bits/stdc++.h>

using namespace std;
 
#ifdef DEBUG
  #include "/home/ioi/codes/debug.h"
#else
  #define debug(...) (void) 37
#endif

struct DSU {
  vector<int> link;
  DSU(int n) {
    link.resize(n);
    iota(link.begin(), link.end(), 0);
  }
  int get(int v) {
    return link[v] = (v == link[v] ? v : get(link[v]));
  }
  bool unite(int x, int y) {
    x = get(x), y = get(y);
    if (x == y) {
      return false;
    }
    link[y] = x;
    return true;
  }
};

struct SegTree {
  vector<int> mx;
  int n;
  SegTree(int _n) : n(_n) {
    mx.resize(2 * n + 1, -1);
  }
  int get(int l, int r) {
    int res = -1;
    for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
      debug(l, r, mx[l], mx[r - 1]);
      if (l & 1) {
        res = max(res, mx[l++]);
      }
      if (r & 1) {
        res = max(res, mx[--r]);
      }
    }
    return res;
  }
  void modify(int x, int v) {
    for (mx[x += n] = v; x > 1; x >>= 1) {
      mx[x >> 1] = max(mx[x], mx[x ^ 1]);
    }
  }
};


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) {
  int M = int(X.size());
  int Q = int(S.size());
  vector<vector<int>> g(N);
  for (int i = 0; i < M; ++i) {
    g[X[i]].push_back(Y[i]);
    g[Y[i]].push_back(X[i]);
  }
  debug(N, g);
  auto Convert_to_array = [&](bool rev, vector<int> end, vector<int> ver) {
    vector<int> ord(N);
    iota(ord.begin(), ord.end(), 0);
    if (rev) {
      reverse(ord.begin(), ord.end());
    }
    debug(ord);
    DSU d(N);
    const int LG = __lg(N) + 1;    
    vector<vector<int>> lift(LG, vector<int>(N, -1));    
    vector<vector<int>> ch(N);
    for (auto v : ord) {
      for (auto u : g[v]) {
        if ((u < v) ^ rev && d.get(v) != d.get(u)) {
          ch[v].push_back(d.get(u));
          lift[0][d.get(u)] = v;
          d.unite(v, u);
        }
      }
    }
    debug(ch, lift);
    for (int l = 1; l < LG; ++l) {
      for (int i = 0; i < N; ++i) {
        int go = lift[l - 1][i];
        lift[l][i] = (go == -1 ? -1 : lift[l - 1][go]);
      }
    }
    vector<int> tin(N), tout(N), tour;
    function<void(int)> Dfs = [&](int v) {
      tin[v] = int(tour.size());
      tour.push_back(v);
      for (auto u : ch[v]) {
        Dfs(u);
      }
      tout[v] = int(tour.size()) - 1;
    };
    Dfs(ord.back());
    debug(tin, tout, lift);
    vector<int> tour_l(Q), tour_r(Q);
    for (int i = 0; i < Q; ++i) {
      int v = ver[i];
      for (int l = LG - 1; l >= 0; --l) {
        if (lift[l][v] != -1 && (!rev? lift[l][v] <= end[i] : lift[l][v] >= end[i])) {
          v = lift[l][v];
        }
      }
      tour_l[i] = tin[v];
      tour_r[i] = tout[v];
    }
    return make_tuple(tour, tour_l, tour_r);
  };
  auto[end_tour, end_l, end_r] = Convert_to_array(false, R, E);
  auto[start_tour, start_l, start_r] = Convert_to_array(true, L, S);
  debug(end_tour, end_l, end_r);
  debug(start_tour, start_l, start_r);
  vector<vector<int>> qs(N);
  for (int i = 0; i < Q; ++i) {
    qs[end_r[i]].push_back(i);
  }
  vector<int> ind(N);
  for (int i = 0; i < N; ++i) {
    ind[start_tour[i]] = i;
  }
  debug(qs, ind);
  SegTree st(N);
  vector<int> ans(Q);
  for (int i = 0; i < N; ++i) {
    st.modify(ind[end_tour[i]], i);
    debug(ind[end_tour[i]], i);
    for (auto q : qs[i]) {
      int mx = st.get(start_l[q], start_r[q]);
      debug(mx, start_l[q], start_r[q]);
      ans[q] = (mx >= end_l[q]);
    }
  }
  return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 4 ms 1108 KB Output is correct
11 Correct 4 ms 1108 KB Output is correct
12 Correct 3 ms 980 KB Output is correct
13 Correct 3 ms 1236 KB Output is correct
14 Correct 3 ms 1236 KB Output is correct
15 Correct 4 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 348 ms 55568 KB Output is correct
2 Correct 366 ms 68656 KB Output is correct
3 Correct 368 ms 65528 KB Output is correct
4 Correct 337 ms 64336 KB Output is correct
5 Correct 344 ms 64208 KB Output is correct
6 Correct 337 ms 64092 KB Output is correct
7 Correct 330 ms 64032 KB Output is correct
8 Correct 358 ms 68564 KB Output is correct
9 Correct 289 ms 65548 KB Output is correct
10 Correct 282 ms 64268 KB Output is correct
11 Correct 275 ms 64140 KB Output is correct
12 Correct 281 ms 64008 KB Output is correct
13 Correct 383 ms 72404 KB Output is correct
14 Correct 351 ms 72400 KB Output is correct
15 Correct 356 ms 72404 KB Output is correct
16 Correct 362 ms 72332 KB Output is correct
17 Correct 360 ms 64008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 4 ms 1108 KB Output is correct
11 Correct 4 ms 1108 KB Output is correct
12 Correct 3 ms 980 KB Output is correct
13 Correct 3 ms 1236 KB Output is correct
14 Correct 3 ms 1236 KB Output is correct
15 Correct 4 ms 1108 KB Output is correct
16 Correct 348 ms 55568 KB Output is correct
17 Correct 366 ms 68656 KB Output is correct
18 Correct 368 ms 65528 KB Output is correct
19 Correct 337 ms 64336 KB Output is correct
20 Correct 344 ms 64208 KB Output is correct
21 Correct 337 ms 64092 KB Output is correct
22 Correct 330 ms 64032 KB Output is correct
23 Correct 358 ms 68564 KB Output is correct
24 Correct 289 ms 65548 KB Output is correct
25 Correct 282 ms 64268 KB Output is correct
26 Correct 275 ms 64140 KB Output is correct
27 Correct 281 ms 64008 KB Output is correct
28 Correct 383 ms 72404 KB Output is correct
29 Correct 351 ms 72400 KB Output is correct
30 Correct 356 ms 72404 KB Output is correct
31 Correct 362 ms 72332 KB Output is correct
32 Correct 360 ms 64008 KB Output is correct
33 Correct 425 ms 65068 KB Output is correct
34 Correct 192 ms 37352 KB Output is correct
35 Correct 385 ms 68332 KB Output is correct
36 Correct 354 ms 64948 KB Output is correct
37 Correct 386 ms 67392 KB Output is correct
38 Correct 394 ms 65604 KB Output is correct
39 Correct 333 ms 76288 KB Output is correct
40 Correct 448 ms 79488 KB Output is correct
41 Correct 318 ms 66700 KB Output is correct
42 Correct 275 ms 64808 KB Output is correct
43 Correct 413 ms 73420 KB Output is correct
44 Correct 346 ms 67296 KB Output is correct
45 Correct 324 ms 76616 KB Output is correct
46 Correct 310 ms 76360 KB Output is correct
47 Correct 357 ms 72444 KB Output is correct
48 Correct 351 ms 72332 KB Output is correct
49 Correct 368 ms 72492 KB Output is correct
50 Correct 382 ms 72328 KB Output is correct
51 Correct 399 ms 75084 KB Output is correct
52 Correct 408 ms 74960 KB Output is correct