답안 #531237

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
531237 2022-02-28T07:21:54 Z cadmiumsky 늑대인간 (IOI18_werewolf) C++14
100 / 100
2121 ms 321048 KB
#include <bits/stdc++.h>
#include "werewolf.h"
using namespace std;
const int nmax = 2e5 + 5;
#define tree asdhgasdgjahsd
struct DSU {
  vector<int> dsu;
  vector<vector<int> > tree;
  vector< vector<int> > p;
  vector<int> pin, pout;
  int inp = 0;
  vector<int> preord;
  bool cond;
  void init(int n, bool c) {
    dsu.resize(n);
    pin.resize(n);
    pout.resize(n);
    tree.resize(n, vector<int>());
    p.resize(n, vector<int>(18,0));
    cond = c;
    for(int i = 0; i < n; i++)
      dsu[i] = i;
  }
  int father(int x) {
    return (dsu[x] == x? x : father(dsu[x] = father(dsu[dsu[x]]))); 
  }
  void push(int u, int v) { 
    v = father(v);
    if(v == u)
      return;
    p[v][0] = u;
    tree[u].push_back(v);
    dsu[v] = u;
  }
  void dfs(int node) {
    preord.push_back(node);
    pin[node] = inp++;
    for(int i = 1; i < 18; ++i)
      p[node][i] = p[p[node][i - 1]][i - 1];
    for(auto x : tree[node])
      dfs(x);
    pout[node] = inp - 1;
  }
  int r;
  vector<int> init(int root) {
    p[root][0] = root;
    dfs(root);
    r = root;
    return preord;
  }
  bool isanc(int f, int node) {
    return pin[f] <= pin[node] && pout[node] <= pout[f];
  }
  pair<int,int> findbest(int start, int limit) {
    for(int i = 17; i >= 0; i--) {
      if(((p[start][i] < limit) ^ cond) || p[start][i] == limit)
        start = p[start][i];
    }
    return {pin[start], pout[start]};
  }
};
int n, m, q;

vector<int> g[nmax];
vector<int> v;
namespace AINT {
  set<int> aint[nmax * 4];
  void construct(int node = 1, int cl = 0, int cr = n - 1) {
    if(cl == cr) {
      aint[node].insert(v[cl]);
      return;
    }
    int mid = cl + cr >> 1;
    construct(2 * node, cl, mid);
    construct(2 * node + 1, mid + 1, cr);
    for(auto x : aint[2 * node])
      aint[node].insert(x);
    for(auto x : aint[2 * node + 1])
      aint[node].insert(x);
    return;
  }
  bool query(int l, int r, int liml, int limr, int node = 1, int cl = 0, int cr = n - 1) {
    if(cr < l || r < cl)
      return 0;
    if(l <= cl && cr <= r) {
      //cout <<"! " <<l <<' '<< cl << ' ' << cr << ' '<< r << '\t' << liml << ' ' << *aint[node].lower_bound(liml) <<' ' << limr << '\n';
      auto it = aint[node].lower_bound(liml);
      return (it != aint[node].end() &&(*it) <= limr);
    }
    int mid = cl + cr >> 1;
    return query(l, r, liml, limr, 2 * node, cl, mid) ||
           query(l, r, liml, limr, 2 * node + 1, mid + 1, cr);
  }
};

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();
  q = l.size();
  DSU lower;
  DSU upper;
  lower.init(n ,0);
  upper.init(n ,1);
  for(int i = 0; i < m; i++) {
    g[X[i]].push_back(Y[i]);
    g[Y[i]].push_back(X[i]);
  }
  for(int i = 0; i < n; i++) {
    for(auto x : g[i])
      if(x <= i)
        lower.push(i, x);
  }
  for(int i = n - 1; i >= 0; i--) {
    for(auto x : g[i])
      if(x >= i)
        upper.push(i, x);
  }
  vector<int> down = lower.init(n - 1), up = upper.init(0), inv(n);

  v.resize(n);
  for(int i = 0; i < n; i++) {
    inv[up[i]] = i;
  }
  for(int i = 0; i < n; i++)
    v[i] = inv[down[i]];
  AINT::construct();
  vector<int> sol(q, 0);
  for(int i = 0, cl, cr, liml, limr; i < q; i++) {
    swap(s[i], e[i]);
    tie(cl, cr) = lower.findbest(s[i], r[i]);
    tie(liml, limr) = upper.findbest(e[i], l[i]);
    sol[i] = AINT::query(cl, cr, liml, limr);
  }
  return sol;
}

Compilation message

werewolf.cpp: In function 'void AINT::construct(int, int, int)':
werewolf.cpp:73:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   73 |     int mid = cl + cr >> 1;
      |               ~~~^~~~
werewolf.cpp: In function 'bool AINT::query(int, int, int, int, int, int, int)':
werewolf.cpp:90:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   90 |     int mid = cl + cr >> 1;
      |               ~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 42548 KB Output is correct
2 Correct 24 ms 42560 KB Output is correct
3 Correct 20 ms 42472 KB Output is correct
4 Correct 21 ms 42652 KB Output is correct
5 Correct 19 ms 42520 KB Output is correct
6 Correct 21 ms 42524 KB Output is correct
7 Correct 20 ms 42540 KB Output is correct
8 Correct 19 ms 42620 KB Output is correct
9 Correct 19 ms 42572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 42548 KB Output is correct
2 Correct 24 ms 42560 KB Output is correct
3 Correct 20 ms 42472 KB Output is correct
4 Correct 21 ms 42652 KB Output is correct
5 Correct 19 ms 42520 KB Output is correct
6 Correct 21 ms 42524 KB Output is correct
7 Correct 20 ms 42540 KB Output is correct
8 Correct 19 ms 42620 KB Output is correct
9 Correct 19 ms 42572 KB Output is correct
10 Correct 39 ms 45624 KB Output is correct
11 Correct 39 ms 45644 KB Output is correct
12 Correct 31 ms 45644 KB Output is correct
13 Correct 31 ms 45804 KB Output is correct
14 Correct 31 ms 45780 KB Output is correct
15 Correct 31 ms 45772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1484 ms 310772 KB Output is correct
2 Correct 1454 ms 313044 KB Output is correct
3 Correct 1323 ms 311592 KB Output is correct
4 Correct 1436 ms 311004 KB Output is correct
5 Correct 1508 ms 310972 KB Output is correct
6 Correct 1598 ms 310788 KB Output is correct
7 Correct 1361 ms 310852 KB Output is correct
8 Correct 1403 ms 313176 KB Output is correct
9 Correct 1260 ms 311660 KB Output is correct
10 Correct 1051 ms 311004 KB Output is correct
11 Correct 1075 ms 310868 KB Output is correct
12 Correct 1409 ms 310996 KB Output is correct
13 Correct 1688 ms 317948 KB Output is correct
14 Correct 1723 ms 318012 KB Output is correct
15 Correct 1678 ms 317976 KB Output is correct
16 Correct 1662 ms 317988 KB Output is correct
17 Correct 1404 ms 310908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 42548 KB Output is correct
2 Correct 24 ms 42560 KB Output is correct
3 Correct 20 ms 42472 KB Output is correct
4 Correct 21 ms 42652 KB Output is correct
5 Correct 19 ms 42520 KB Output is correct
6 Correct 21 ms 42524 KB Output is correct
7 Correct 20 ms 42540 KB Output is correct
8 Correct 19 ms 42620 KB Output is correct
9 Correct 19 ms 42572 KB Output is correct
10 Correct 39 ms 45624 KB Output is correct
11 Correct 39 ms 45644 KB Output is correct
12 Correct 31 ms 45644 KB Output is correct
13 Correct 31 ms 45804 KB Output is correct
14 Correct 31 ms 45780 KB Output is correct
15 Correct 31 ms 45772 KB Output is correct
16 Correct 1484 ms 310772 KB Output is correct
17 Correct 1454 ms 313044 KB Output is correct
18 Correct 1323 ms 311592 KB Output is correct
19 Correct 1436 ms 311004 KB Output is correct
20 Correct 1508 ms 310972 KB Output is correct
21 Correct 1598 ms 310788 KB Output is correct
22 Correct 1361 ms 310852 KB Output is correct
23 Correct 1403 ms 313176 KB Output is correct
24 Correct 1260 ms 311660 KB Output is correct
25 Correct 1051 ms 311004 KB Output is correct
26 Correct 1075 ms 310868 KB Output is correct
27 Correct 1409 ms 310996 KB Output is correct
28 Correct 1688 ms 317948 KB Output is correct
29 Correct 1723 ms 318012 KB Output is correct
30 Correct 1678 ms 317976 KB Output is correct
31 Correct 1662 ms 317988 KB Output is correct
32 Correct 1404 ms 310908 KB Output is correct
33 Correct 1775 ms 311068 KB Output is correct
34 Correct 314 ms 74412 KB Output is correct
35 Correct 1819 ms 313108 KB Output is correct
36 Correct 1601 ms 311348 KB Output is correct
37 Correct 1774 ms 312404 KB Output is correct
38 Correct 1690 ms 311956 KB Output is correct
39 Correct 2121 ms 318140 KB Output is correct
40 Correct 1745 ms 321048 KB Output is correct
41 Correct 1402 ms 311928 KB Output is correct
42 Correct 1135 ms 311472 KB Output is correct
43 Correct 1581 ms 317828 KB Output is correct
44 Correct 1768 ms 312376 KB Output is correct
45 Correct 1602 ms 318520 KB Output is correct
46 Correct 1352 ms 318196 KB Output is correct
47 Correct 1744 ms 318208 KB Output is correct
48 Correct 1735 ms 317980 KB Output is correct
49 Correct 1709 ms 318300 KB Output is correct
50 Correct 1697 ms 317884 KB Output is correct
51 Correct 1539 ms 320796 KB Output is correct
52 Correct 1591 ms 320784 KB Output is correct