답안 #383153

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
383153 2021-03-28T22:50:00 Z mohamedsobhi777 늑대인간 (IOI18_werewolf) C++14
100 / 100
1855 ms 207648 KB
#include <bits/stdc++.h>
#include "werewolf.h"
using namespace std;
const int N = 8e5 + 7;

int n, m, col;
vector<int> le, ri;
int act[N];
vector<int> adj[N];
vector<int> QL[N], QR[N];
int ST[N], EN[N];
int ro1[N], ro2[N];

struct tree
{
       vector<int> adj[N];
       int st[N], en[N], vec[N], t;
       tree() {}
       void dfs(int x, int p)
       {
              st[x] = ++t;
              if (x < n)
                     vec[t] = x;
              else
                     vec[t] = -1;
              for (auto u : adj[x])
              {
                     if (u == p)
                            continue;

                     dfs(u, x);
              }
              en[x] = ++t;
              vec[t] = -1;
       }

       vector<int> inter(int x)
       {
              vector<int> ret;
              for (int i = st[x]; i <= en[x]; ++i)
              {
                     if (~vec[i])
                     {
                            ret.push_back(vec[i]);
                     }
              }
              return ret;
       }
} t1, t2;

struct dsu
{
       int fat[N];
       dsu() { iota(fat, fat + N, 0); }
       int find(int x) { return fat[x] = x == fat[x] ? x : find(fat[x]); }
       void link(int u, int v)
       {
              u = find(u), v = find(v);
              if (u > v)
                     swap(u, v);
              fat[u] = v;
       }
       inline bool same(int u, int v) { return find(u) == find(v); }
} d;

void build(tree &t, int dir)
{
       dsu d;
       int lst = n;
       ++col;
       for (int i = (dir == 1 ? 0 : n - 1); i >= 0 && i < n; i += dir)
       {
              for (auto u : adj[i])
              {
                     if (act[u] == col)
                     {
                            int x = d.find(u);
                            if (!d.same(lst, x))
                            {
                                   t.adj[lst].push_back(x);
                                   d.link(x, lst);
                            }
                     }
              }
              t.adj[lst].push_back(i);
              d.link(i, lst);
              ++lst;
              if (dir == -1)
              {
                     for (auto u : QL[i])
                     {
                            ro1[u] = d.find(ST[u]);
                     }
              }
              else
              {
                     for (auto u : QR[i])
                     {
                            ro2[u] = d.find(EN[u]);
                     }
              }
              act[i] = col;
       }
}

vector<int> bit[N];

void add(int x, int v)
{
       x ++ ; 
       for (; x < N; x += x & -x)
              bit[x].push_back(v);
}

int get(int x, int y)
{
       int ret = 0 ;
       ++ x; 
       for(;x;x-=x&-x){
              ret += upper_bound(bit[x].begin() , bit[x].end() , y) - bit[x].begin() ;
       }
       return ret; 
}

int query(int l, int r, int x, int y)
{
       return get(r, y) - get(r, x - 1) - get(l - 1, y) + get(l - 1, 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)
{
       n = _N, m = X.size();
       le = X, ri = Y;
       int Q = S.size();
       std::vector<int> A(Q);
       for (int i = 0; i < m; ++i)
       {
              adj[X[i]].push_back(Y[i]);
              adj[Y[i]].push_back(X[i]);
       }
       for (int i = 0; i < Q; ++i)
       {
              QL[L[i]].push_back(i);
              QR[R[i]].push_back(i);
              ST[i] = S[i];
              EN[i] = E[i];
       }
       build(t1, -1);
       build(t2, 1);
       t1.dfs(2 * n - 1, 2 * n - 1), t2.dfs(2 * n - 1, 2 * n - 1);
       for (int i = 0; i < n; ++i)
       {
              t2.vec[t2.st[i]] = t1.st[i];
       }
       for(int i = 0 ;i < n ; ++ i){
              add(t2.st[i] , t2.vec[ t2.st[i] ] ) ; 
       }
       for(int i = 0 ;i < N ;++ i){
              sort(bit[i].begin() , bit[i].end()) ;
       }
       for (int i = 0; i < Q; ++i)
       {
              int L = t1.st[ro1[i]], R = t1.en[ro1[i]];
              if( query( t2.st[ ro2[i] ] , t2.en[ ro2[i] ] , L , R) ){
                     A[i] = 1; 
              }
       }

       return A;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 119404 KB Output is correct
2 Correct 78 ms 119532 KB Output is correct
3 Correct 89 ms 119404 KB Output is correct
4 Correct 85 ms 119404 KB Output is correct
5 Correct 82 ms 119532 KB Output is correct
6 Correct 78 ms 119532 KB Output is correct
7 Correct 77 ms 119404 KB Output is correct
8 Correct 79 ms 119404 KB Output is correct
9 Correct 83 ms 119404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 119404 KB Output is correct
2 Correct 78 ms 119532 KB Output is correct
3 Correct 89 ms 119404 KB Output is correct
4 Correct 85 ms 119404 KB Output is correct
5 Correct 82 ms 119532 KB Output is correct
6 Correct 78 ms 119532 KB Output is correct
7 Correct 77 ms 119404 KB Output is correct
8 Correct 79 ms 119404 KB Output is correct
9 Correct 83 ms 119404 KB Output is correct
10 Correct 91 ms 120556 KB Output is correct
11 Correct 90 ms 120556 KB Output is correct
12 Correct 91 ms 120636 KB Output is correct
13 Correct 86 ms 120556 KB Output is correct
14 Correct 90 ms 120556 KB Output is correct
15 Correct 89 ms 120556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1549 ms 192032 KB Output is correct
2 Correct 1364 ms 193892 KB Output is correct
3 Correct 1400 ms 198620 KB Output is correct
4 Correct 1338 ms 198376 KB Output is correct
5 Correct 1305 ms 198612 KB Output is correct
6 Correct 1555 ms 198500 KB Output is correct
7 Correct 1219 ms 196180 KB Output is correct
8 Correct 1215 ms 200504 KB Output is correct
9 Correct 1176 ms 197536 KB Output is correct
10 Correct 886 ms 197092 KB Output is correct
11 Correct 1127 ms 197300 KB Output is correct
12 Correct 1349 ms 197932 KB Output is correct
13 Correct 1180 ms 201080 KB Output is correct
14 Correct 1094 ms 201184 KB Output is correct
15 Correct 1098 ms 201040 KB Output is correct
16 Correct 1079 ms 201228 KB Output is correct
17 Correct 1261 ms 196432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 119404 KB Output is correct
2 Correct 78 ms 119532 KB Output is correct
3 Correct 89 ms 119404 KB Output is correct
4 Correct 85 ms 119404 KB Output is correct
5 Correct 82 ms 119532 KB Output is correct
6 Correct 78 ms 119532 KB Output is correct
7 Correct 77 ms 119404 KB Output is correct
8 Correct 79 ms 119404 KB Output is correct
9 Correct 83 ms 119404 KB Output is correct
10 Correct 91 ms 120556 KB Output is correct
11 Correct 90 ms 120556 KB Output is correct
12 Correct 91 ms 120636 KB Output is correct
13 Correct 86 ms 120556 KB Output is correct
14 Correct 90 ms 120556 KB Output is correct
15 Correct 89 ms 120556 KB Output is correct
16 Correct 1549 ms 192032 KB Output is correct
17 Correct 1364 ms 193892 KB Output is correct
18 Correct 1400 ms 198620 KB Output is correct
19 Correct 1338 ms 198376 KB Output is correct
20 Correct 1305 ms 198612 KB Output is correct
21 Correct 1555 ms 198500 KB Output is correct
22 Correct 1219 ms 196180 KB Output is correct
23 Correct 1215 ms 200504 KB Output is correct
24 Correct 1176 ms 197536 KB Output is correct
25 Correct 886 ms 197092 KB Output is correct
26 Correct 1127 ms 197300 KB Output is correct
27 Correct 1349 ms 197932 KB Output is correct
28 Correct 1180 ms 201080 KB Output is correct
29 Correct 1094 ms 201184 KB Output is correct
30 Correct 1098 ms 201040 KB Output is correct
31 Correct 1079 ms 201228 KB Output is correct
32 Correct 1261 ms 196432 KB Output is correct
33 Correct 1788 ms 199224 KB Output is correct
34 Correct 474 ms 153656 KB Output is correct
35 Correct 1813 ms 200408 KB Output is correct
36 Correct 1777 ms 198680 KB Output is correct
37 Correct 1855 ms 199696 KB Output is correct
38 Correct 1775 ms 198912 KB Output is correct
39 Correct 1348 ms 207648 KB Output is correct
40 Correct 1318 ms 202100 KB Output is correct
41 Correct 1643 ms 199476 KB Output is correct
42 Correct 1275 ms 197412 KB Output is correct
43 Correct 1824 ms 203808 KB Output is correct
44 Correct 1821 ms 199744 KB Output is correct
45 Correct 1164 ms 206804 KB Output is correct
46 Correct 1249 ms 206532 KB Output is correct
47 Correct 1115 ms 201352 KB Output is correct
48 Correct 1055 ms 201008 KB Output is correct
49 Correct 1097 ms 201216 KB Output is correct
50 Correct 1122 ms 201136 KB Output is correct
51 Correct 1196 ms 200800 KB Output is correct
52 Correct 1162 ms 200984 KB Output is correct