답안 #629922

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
629922 2022-08-15T11:02:01 Z stevancv 늑대인간 (IOI18_werewolf) C++14
100 / 100
696 ms 74788 KB
#include <bits/stdc++.h>
#include "werewolf.h"
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int mod = 1e9 + 7;
vector<int> g[2][N];
int in[2][N], out[2][N], tsz;
void Dfs(int s, int o) {
    in[o][s] = ++tsz;
    for (int u : g[o][s]) Dfs(u, o);
    out[o][s] = tsz;
}
int st[4 * N];
void Add(int node, int l, int r, int p) {
    st[node]++;
    if (l == r) return;
    int mid = l + r >> 1;
    if (p <= mid) Add(2 * node, l, mid, p);
    else Add(2 * node + 1, mid + 1, r, p);
}
int Get(int node, int l, int r, int ql, int qr) {
    if (r < ql || qr < l) return 0;
    if (ql <= l && r <= qr) return st[node];
    int mid = l + r >> 1;
    return Get(2 * node, l, mid, ql, qr) + Get(2 * node + 1, mid + 1, r, ql, qr);
}
vector<int> check_validity(int n, vector<int> x, vector<int> y, vector<int> s, vector<int> e, vector<int> l, vector<int> r) {
    int m = x.size(); int q = s.size();
    vector<int> link(n);
    iota(link.begin(), link.end(), 0);
    function<int(int)> Find = [&] (int x) {
        if (x == link[x]) return x;
        return link[x] = Find(link[x]);
    };
    function<void(int, int, int)> Unite = [&] (int x, int y, int o) {
        x = Find(x);
        y = Find(y);
        if (x == y) return;
        if (o == 0) {
            link[max(x, y)] = min(x, y);
            g[o][min(x, y)].push_back(max(x, y));
        }
        else {
            link[min(x, y)] = max(x, y);
            g[o][max(x, y)].push_back(min(x, y));
        }
    };
    vector<array<int, 3>> all;
    for (int i = 0; i < m; i++) all.push_back({min(x[i], y[i]), 0, i});
    for (int i = 0; i < q; i++) all.push_back({l[i], 1, i});
    sort(all.begin(), all.end(), [&] (array<int, 3> i, array<int, 3> j) {
        if (i[0] != j[0]) return i[0] > j[0];
        return i[1] < j[1];
    });
    vector<int> who1(q);
    for (auto i : all) {
        if (i[1] == 0) Unite(x[i[2]], y[i[2]], 0);
        else who1[i[2]] = Find(s[i[2]]);
    }
    tsz = -1;
    Dfs(0, 0);
    iota(link.begin(), link.end(), 0);
    all.clear();
    for (int i = 0; i < m; i++) all.push_back({max(x[i], y[i]), 0, i});
    for (int i = 0; i < q; i++) all.push_back({r[i], 1, i});
    sort(all.begin(), all.end());
    vector<int> who2(q);
    for (auto i : all) {
        if (i[1] == 0) Unite(x[i[2]], y[i[2]], 1);
        else who2[i[2]] = Find(e[i[2]]);
    }
    tsz = -1;
    Dfs(n - 1, 1);
    vector<int> nz(n);
    for (int i = 0; i < n; i++) nz[in[0][i]] = i;
    vector<int> ans(q);
    vector<vector<array<int, 4>>> qq(n);
    for (int i = 0; i < q; i++) {
        if (in[0][who1[i]] > 0) qq[in[0][who1[i]] - 1].push_back({i, -1, in[1][who2[i]], out[1][who2[i]]});
        qq[out[0][who1[i]]].push_back({i, 1, in[1][who2[i]], out[1][who2[i]]});
    }
    for (int i = 0; i < n; i++) {
        Add(1, 0, n - 1, in[1][nz[i]]);
        for (auto j : qq[i]) {
            ans[j[0]] += j[1] * Get(1, 0, n - 1, j[2], j[3]);
        }
    }
    for (int i = 0; i < q; i++) smin(ans[i], 1);
    return ans;
}

Compilation message

werewolf.cpp: In function 'void Add(int, int, int, int)':
werewolf.cpp:23:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   23 |     int mid = l + r >> 1;
      |               ~~^~~
werewolf.cpp: In function 'int Get(int, int, int, int, int)':
werewolf.cpp:30:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   30 |     int mid = l + r >> 1;
      |               ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 7 ms 9704 KB Output is correct
3 Correct 6 ms 9692 KB Output is correct
4 Correct 8 ms 9684 KB Output is correct
5 Correct 7 ms 9692 KB Output is correct
6 Correct 7 ms 9684 KB Output is correct
7 Correct 6 ms 9700 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 7 ms 9704 KB Output is correct
3 Correct 6 ms 9692 KB Output is correct
4 Correct 8 ms 9684 KB Output is correct
5 Correct 7 ms 9692 KB Output is correct
6 Correct 7 ms 9684 KB Output is correct
7 Correct 6 ms 9700 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 15 ms 10584 KB Output is correct
11 Correct 11 ms 10516 KB Output is correct
12 Correct 11 ms 10452 KB Output is correct
13 Correct 11 ms 10620 KB Output is correct
14 Correct 14 ms 10608 KB Output is correct
15 Correct 13 ms 10644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 620 ms 63988 KB Output is correct
2 Correct 523 ms 67108 KB Output is correct
3 Correct 519 ms 64288 KB Output is correct
4 Correct 526 ms 62744 KB Output is correct
5 Correct 536 ms 62872 KB Output is correct
6 Correct 530 ms 63912 KB Output is correct
7 Correct 477 ms 62728 KB Output is correct
8 Correct 512 ms 67184 KB Output is correct
9 Correct 426 ms 62488 KB Output is correct
10 Correct 449 ms 62760 KB Output is correct
11 Correct 467 ms 62756 KB Output is correct
12 Correct 439 ms 62308 KB Output is correct
13 Correct 461 ms 71832 KB Output is correct
14 Correct 485 ms 71808 KB Output is correct
15 Correct 496 ms 71820 KB Output is correct
16 Correct 460 ms 71848 KB Output is correct
17 Correct 471 ms 62688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 7 ms 9704 KB Output is correct
3 Correct 6 ms 9692 KB Output is correct
4 Correct 8 ms 9684 KB Output is correct
5 Correct 7 ms 9692 KB Output is correct
6 Correct 7 ms 9684 KB Output is correct
7 Correct 6 ms 9700 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 15 ms 10584 KB Output is correct
11 Correct 11 ms 10516 KB Output is correct
12 Correct 11 ms 10452 KB Output is correct
13 Correct 11 ms 10620 KB Output is correct
14 Correct 14 ms 10608 KB Output is correct
15 Correct 13 ms 10644 KB Output is correct
16 Correct 620 ms 63988 KB Output is correct
17 Correct 523 ms 67108 KB Output is correct
18 Correct 519 ms 64288 KB Output is correct
19 Correct 526 ms 62744 KB Output is correct
20 Correct 536 ms 62872 KB Output is correct
21 Correct 530 ms 63912 KB Output is correct
22 Correct 477 ms 62728 KB Output is correct
23 Correct 512 ms 67184 KB Output is correct
24 Correct 426 ms 62488 KB Output is correct
25 Correct 449 ms 62760 KB Output is correct
26 Correct 467 ms 62756 KB Output is correct
27 Correct 439 ms 62308 KB Output is correct
28 Correct 461 ms 71832 KB Output is correct
29 Correct 485 ms 71808 KB Output is correct
30 Correct 496 ms 71820 KB Output is correct
31 Correct 460 ms 71848 KB Output is correct
32 Correct 471 ms 62688 KB Output is correct
33 Correct 689 ms 64328 KB Output is correct
34 Correct 490 ms 51912 KB Output is correct
35 Correct 605 ms 67360 KB Output is correct
36 Correct 562 ms 64696 KB Output is correct
37 Correct 671 ms 66408 KB Output is correct
38 Correct 576 ms 65664 KB Output is correct
39 Correct 519 ms 74668 KB Output is correct
40 Correct 689 ms 74788 KB Output is correct
41 Correct 509 ms 64540 KB Output is correct
42 Correct 414 ms 63364 KB Output is correct
43 Correct 696 ms 73160 KB Output is correct
44 Correct 535 ms 66024 KB Output is correct
45 Correct 437 ms 74472 KB Output is correct
46 Correct 431 ms 73784 KB Output is correct
47 Correct 471 ms 72136 KB Output is correct
48 Correct 481 ms 71832 KB Output is correct
49 Correct 559 ms 72156 KB Output is correct
50 Correct 512 ms 71820 KB Output is correct
51 Correct 630 ms 74276 KB Output is correct
52 Correct 653 ms 73916 KB Output is correct