답안 #988893

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
988893 2024-05-26T15:18:03 Z Essa2006 늑대인간 (IOI18_werewolf) C++14
100 / 100
1173 ms 223404 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)

const int N = 1 << 22, lg = 20, inf = 1e9;
int n, cur;
vector<int> SS;
vector<vector<int>> A;
vector<array<int, 2>> P, Anc, L, R, Dsc, Fin;
vector<vector<array<int, 2>>> Up;

int get(int id, int u, int v, int l, int r) {
    if (l > v || r < u) {
        return 0;
    }

    if (l >= u && r <= v) {
        return SS[id];
    }

    int md = (l + r) / 2;
    return get(id * 2, u, v, l, md) + get(id * 2 + 1, u, v, md + 1, r);
}

void update(int id, int c) {
    id += N;
    SS[id] += c;
    while (id /= 2) {
        SS[id] += c;
    }
}

int find(int ind, int id) {
    if (P[ind][id] == ind) {
        return ind;
    }

    return P[ind][id] = find(P[ind][id], id);
}

array<int, 2> cnt;
void merge(int a, int b, int id) {
    int ga = find(a, id), gb = find(b, id);
    if (ga == gb) {
        return ;
    }

    Anc[cnt[id]][id] = cur;
    L[cnt[id]][id] = ga, R[cnt[id]][id] = gb;
    Up[ga][0][id] = Up[gb][0][id] = P[ga][id] = P[gb][id] = cnt[id]++;

    return;
}

array<int, 2> time_;
void build(int u, int id) {
    Dsc[u][id] = time_[id]++;

    if (Up[u][0][id] == -1) {
        Up[u][0][id] = u;
    }
    for (int j = 1; j <= lg; j++) {
        Up[u][j][id] = Up[Up[u][j - 1][id]][j - 1][id];
    }

    if (u >= n) {
        build(L[u][id], id);
        build(R[u][id], id);
    }

    Fin[u][id] = time_[id]++;
}

int lca(int u, int c, int id) {
    for (int j = lg; j >= 0; j--) {
        if (Anc[Up[u][j][id]][id] <= c) {
            u = Up[u][j][id];
        }
    }
    return u;
}

vector<int> check_validity(int nn, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> LL, vector<int> RR) {
    n = nn;
    cnt[0] = cnt[1] = n;
    SS.resize(2 * N), A.resize(n), P.resize(2 * n), Anc.resize(2 * n, {inf, inf}), L.resize(2 * n, {-1, -1}), R.resize(2 * n, {-1, -1}), Dsc.resize(2 * n), Fin.resize(2 * n);
    Up.resize(2 * n, vector<array<int, 2>>(lg + 1, {-1, -1}));
    for (int i = 0; i < 2 * n; i++) {
        P[i][0] = P[i][1] = i;
    }


    for (int i = 0; i < X.size(); i++) {
        A[X[i]].push_back(Y[i]);
        A[Y[i]].push_back(X[i]);
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < A[i].size(); j++) {
            if (A[i][j] < i) {
                cur = i;
                merge(i, A[i][j], 0);
            }
        }
    }
    for (int i = n - 1; i >= 0; i--) {
        for (int j = 0; j < A[i].size(); j++) {
            if (A[i][j] > i) {
                cur =  n - 1 - i;
                merge(i, A[i][j], 1);
            }
        }
    }


    build(cnt[0] - 1, 0);
    build(cnt[1] - 1, 1);


    vector<int> Ans(LL.size());
    vector<vector<array<int, 3>>> All(4 * n);
    vector<vector<int>> All2(4 * n);
    for (int i = 0; i < S.size(); i++) {
        int u = lca(E[i], RR[i], 0), v = lca(S[i], (n - 1) - LL[i], 1);

        All[Dsc[u][0]].push_back({i, Dsc[v][1], Fin[v][1]});
        All[Fin[u][0] + 1].push_back({-(i + 1), Dsc[v][1], Fin[v][1]});
    }
    for (int i = 0; i < n; i++) {
        All2[Dsc[i][0] + 1].push_back(Dsc[i][1]);
    }

    vector<int> Prf(LL.size());
    for (int i = 0; i < 4 * n; i++) {
        for (int j = 0; j < All[i].size(); j++) {
            if (All[i][j][0] < 0) {
                All[i][j][0] *= -1;
                All[i][j][0]--;
                int res = get(1, All[i][j][1], All[i][j][2], 0, N - 1);
                res -= Prf[All[i][j][0]];
                assert(res >= 0);
                if (res) {
                    Ans[All[i][j][0]] =1;
                }
            }
            else {
                Prf[All[i][j][0]] = get(1, All[i][j][1], All[i][j][2], 0, N - 1);
            }
        }
        for (int j = 0; j < All2[i].size(); j++) {
            update(All2[i][j], 1);
        }
    }

    return Ans;
}

Compilation message

werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:98:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |     for (int i = 0; i < X.size(); i++) {
      |                     ~~^~~~~~~~~~
werewolf.cpp:104:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |         for (int j = 0; j < A[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~
werewolf.cpp:112:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |         for (int j = 0; j < A[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~
werewolf.cpp:128:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  128 |     for (int i = 0; i < S.size(); i++) {
      |                     ~~^~~~~~~~~~
werewolf.cpp:140:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  140 |         for (int j = 0; j < All[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~
werewolf.cpp:155:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  155 |         for (int j = 0; j < All2[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 33164 KB Output is correct
2 Correct 16 ms 33200 KB Output is correct
3 Correct 13 ms 33116 KB Output is correct
4 Correct 13 ms 33260 KB Output is correct
5 Correct 13 ms 33112 KB Output is correct
6 Correct 13 ms 33368 KB Output is correct
7 Correct 13 ms 33128 KB Output is correct
8 Correct 12 ms 33116 KB Output is correct
9 Correct 12 ms 33288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 33164 KB Output is correct
2 Correct 16 ms 33200 KB Output is correct
3 Correct 13 ms 33116 KB Output is correct
4 Correct 13 ms 33260 KB Output is correct
5 Correct 13 ms 33112 KB Output is correct
6 Correct 13 ms 33368 KB Output is correct
7 Correct 13 ms 33128 KB Output is correct
8 Correct 12 ms 33116 KB Output is correct
9 Correct 12 ms 33288 KB Output is correct
10 Correct 19 ms 35932 KB Output is correct
11 Correct 18 ms 35932 KB Output is correct
12 Correct 20 ms 35936 KB Output is correct
13 Correct 20 ms 35928 KB Output is correct
14 Correct 19 ms 35932 KB Output is correct
15 Correct 19 ms 35976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 890 ms 204412 KB Output is correct
2 Correct 971 ms 216848 KB Output is correct
3 Correct 787 ms 213276 KB Output is correct
4 Correct 720 ms 211792 KB Output is correct
5 Correct 724 ms 211976 KB Output is correct
6 Correct 822 ms 212604 KB Output is correct
7 Correct 820 ms 210752 KB Output is correct
8 Correct 791 ms 216696 KB Output is correct
9 Correct 602 ms 213008 KB Output is correct
10 Correct 556 ms 210576 KB Output is correct
11 Correct 618 ms 211284 KB Output is correct
12 Correct 620 ms 210880 KB Output is correct
13 Correct 1059 ms 217428 KB Output is correct
14 Correct 1040 ms 217324 KB Output is correct
15 Correct 1059 ms 217428 KB Output is correct
16 Correct 1146 ms 217428 KB Output is correct
17 Correct 771 ms 210472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 33164 KB Output is correct
2 Correct 16 ms 33200 KB Output is correct
3 Correct 13 ms 33116 KB Output is correct
4 Correct 13 ms 33260 KB Output is correct
5 Correct 13 ms 33112 KB Output is correct
6 Correct 13 ms 33368 KB Output is correct
7 Correct 13 ms 33128 KB Output is correct
8 Correct 12 ms 33116 KB Output is correct
9 Correct 12 ms 33288 KB Output is correct
10 Correct 19 ms 35932 KB Output is correct
11 Correct 18 ms 35932 KB Output is correct
12 Correct 20 ms 35936 KB Output is correct
13 Correct 20 ms 35928 KB Output is correct
14 Correct 19 ms 35932 KB Output is correct
15 Correct 19 ms 35976 KB Output is correct
16 Correct 890 ms 204412 KB Output is correct
17 Correct 971 ms 216848 KB Output is correct
18 Correct 787 ms 213276 KB Output is correct
19 Correct 720 ms 211792 KB Output is correct
20 Correct 724 ms 211976 KB Output is correct
21 Correct 822 ms 212604 KB Output is correct
22 Correct 820 ms 210752 KB Output is correct
23 Correct 791 ms 216696 KB Output is correct
24 Correct 602 ms 213008 KB Output is correct
25 Correct 556 ms 210576 KB Output is correct
26 Correct 618 ms 211284 KB Output is correct
27 Correct 620 ms 210880 KB Output is correct
28 Correct 1059 ms 217428 KB Output is correct
29 Correct 1040 ms 217324 KB Output is correct
30 Correct 1059 ms 217428 KB Output is correct
31 Correct 1146 ms 217428 KB Output is correct
32 Correct 771 ms 210472 KB Output is correct
33 Correct 1171 ms 214104 KB Output is correct
34 Correct 294 ms 72788 KB Output is correct
35 Correct 1085 ms 217424 KB Output is correct
36 Correct 960 ms 213620 KB Output is correct
37 Correct 1117 ms 216656 KB Output is correct
38 Correct 1173 ms 214160 KB Output is correct
39 Correct 1074 ms 222424 KB Output is correct
40 Correct 983 ms 220284 KB Output is correct
41 Correct 781 ms 214184 KB Output is correct
42 Correct 666 ms 211784 KB Output is correct
43 Correct 934 ms 220688 KB Output is correct
44 Correct 875 ms 215664 KB Output is correct
45 Correct 846 ms 223404 KB Output is correct
46 Correct 858 ms 223172 KB Output is correct
47 Correct 1045 ms 217680 KB Output is correct
48 Correct 1010 ms 217428 KB Output is correct
49 Correct 1012 ms 217684 KB Output is correct
50 Correct 1047 ms 217340 KB Output is correct
51 Correct 910 ms 221080 KB Output is correct
52 Correct 859 ms 221048 KB Output is correct