Submission #208845

# Submission time Handle Problem Language Result Execution time Memory
208845 2020-03-12T09:48:34 Z teomrn Werewolf (IOI18_werewolf) C++14
100 / 100
1355 ms 122664 KB
#include <bits/stdc++.h>
using namespace std;

struct Aib {
    vector <int> aib;
    function <int(int)> lsb = [](int x) { return x & (-x); };
    function <int(int)> qry = [&](int p) {
        int ans = 0;
        while (p)
            ans += aib[p], p -= lsb(p);
        return ans;
    };

    Aib(int N) : aib(N + 2, 0) { }

    void Update(int poz, int val)
    {
        poz++;
        while (poz < (int)aib.size())
            aib[poz] += val, poz += lsb(poz);
    }

    int Query(int st, int dr)
    {
        st++, dr++;
        return qry(dr) - (st > 0 ? qry(st - 1) : 0);
    }
};

struct Tree {
    vector <int> stramos, liniarizare;
    vector <pair <int, int>> poz_in_liniarizare;
    vector <vector <int>> adia;
    vector <vector <int>> up;
    int root;

    Tree(int N) : stramos(N), poz_in_liniarizare(N), adia(N), up(N, vector <int> (20, -1)) {
        iota(stramos.begin(), stramos.end(), 0);
    }

    int FindStramos(int nod) {
        if (stramos[nod] != nod)
            return stramos[nod] = FindStramos(stramos[nod]);
        return stramos[nod];
    }

    void Dfs(int nod) {
        
        // cerr << "Node " << nod << " has sons ";
        // for (auto i : adia[nod])
        //     cerr << i << ' ';
        // cerr << '\n';

        for (int i = 1; i < 20; i++)
            if (up[nod][i - 1] != -1)
                up[nod][i] = up[up[nod][i - 1]][i - 1];

        poz_in_liniarizare[nod].first = liniarizare.size();
        liniarizare.push_back(nod);
        for (auto i : adia[nod])
            Dfs(i);

        poz_in_liniarizare[nod].second = liniarizare.size() - 1;
    }

    pair <int, int> Span(int nod, function <bool(int)> ok) {
        assert(ok(nod));
        for (int i = 19; i >= 0; i--)
            if (up[nod][i] != -1 && ok(up[nod][i]))
                nod = up[nod][i];

        return poz_in_liniarizare[nod];
    }
};

vector <int> check_validity(int N, vector <int> X, vector <int> Y, vector <int> S, vector <int> E, vector <int> L, vector <int> R)
{
    vector <vector <int>> graph(N);
    for (int i = 0; i < (int)X.size(); i++) {
        graph[X[i]].push_back(Y[i]);
        graph[Y[i]].push_back(X[i]);
    }

    Tree small_up(N), big_up(N);

    for (int i = 0; i < N; i++) {
        for (auto vec : graph[i]) {
            int x = big_up.FindStramos(vec);
            if (vec < i && x != i) {
                big_up.stramos[x] = i;
                big_up.up[x][0] = i;
                big_up.adia[i].push_back(x);
            }
        }
    }
    for (int i = N - 1; i >= 0; i--) {
        for (auto vec : graph[i]) {
            int x = small_up.FindStramos(vec);
            if (vec > i && x != i) {
                small_up.stramos[x] = i;
                small_up.up[x][0] = i;
                small_up.adia[i].push_back(x);
            }
        }
    }

    // cerr << "For small_up:\n";
    small_up.Dfs(0);
    // cerr << "\nFor bi_up:\n";
    big_up.Dfs(N - 1);

    // cerr << "Finished trees\n";

    /// I have built the trees, now I have to check for each node the interval it can reach

    int Q = S.size();
    vector <int> intersection(Q, 0);
    vector <pair <int, int>> to_verify(Q);
    vector <vector <pair <int, int>>> events(N + 1); // pentru fiecare pozitie daca trebuie sa scad / sa adaug raspunsul pt un query
    Aib aib(N);

    for (int i = 0; i < Q; i++) {
        auto intervS = small_up.Span(S[i], [&](int nod) -> bool { return nod >= L[i]; });
        auto intervE = big_up.Span(E[i], [&](int nod) -> bool { return nod <= R[i]; });

        // cerr << "Query i can get from S=" << S[i] << " to ";
        // for (int j = intervS.first; j <= intervS.second; j++)
        //     cerr << small_up.liniarizare[j] << ' ';
            
        // cerr << "\nQuery i can get from E=" << E[i] << " to ";
        // for (int j = intervE.first; j <= intervE.second; j++)
        //     cerr << big_up.liniarizare[j] << ' ';
        // cerr << "\n\n";
        
        to_verify[i] = intervE;
        events[intervS.first].push_back({ i, -1 });
        events[intervS.second + 1].push_back({ i, 1 });
    }

    // cerr << "Finished adding segments\n";

    for (int i = 0; i <= N; i++) {
        /// mai intai verific de e de procesat
        for (auto op : events[i]) {
            int val = aib.Query(to_verify[op.first].first, to_verify[op.first].second);
            intersection[op.first] += val * op.second;
        }

        if (i < N) {
            int elem = small_up.liniarizare[i];
            int poz_in_big = big_up.poz_in_liniarizare[elem].first;
            aib.Update(poz_in_big, 1);   
        }
    }

    for (auto & it : intersection) {
        assert(it >= 0);
        it = min(it, 1);
    }

    return intersection;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 380 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 380 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 380 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 380 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 380 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 380 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 14 ms 2040 KB Output is correct
11 Correct 12 ms 2040 KB Output is correct
12 Correct 12 ms 2040 KB Output is correct
13 Correct 13 ms 2168 KB Output is correct
14 Correct 13 ms 2064 KB Output is correct
15 Correct 14 ms 2168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1033 ms 107120 KB Output is correct
2 Correct 1185 ms 115000 KB Output is correct
3 Correct 1004 ms 113236 KB Output is correct
4 Correct 864 ms 112496 KB Output is correct
5 Correct 876 ms 112672 KB Output is correct
6 Correct 958 ms 113644 KB Output is correct
7 Correct 904 ms 112728 KB Output is correct
8 Correct 1062 ms 114884 KB Output is correct
9 Correct 759 ms 112580 KB Output is correct
10 Correct 675 ms 112240 KB Output is correct
11 Correct 681 ms 112324 KB Output is correct
12 Correct 760 ms 112112 KB Output is correct
13 Correct 1266 ms 120036 KB Output is correct
14 Correct 1274 ms 120044 KB Output is correct
15 Correct 1275 ms 120032 KB Output is correct
16 Correct 1316 ms 120164 KB Output is correct
17 Correct 921 ms 112776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 380 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 380 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 380 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 14 ms 2040 KB Output is correct
11 Correct 12 ms 2040 KB Output is correct
12 Correct 12 ms 2040 KB Output is correct
13 Correct 13 ms 2168 KB Output is correct
14 Correct 13 ms 2064 KB Output is correct
15 Correct 14 ms 2168 KB Output is correct
16 Correct 1033 ms 107120 KB Output is correct
17 Correct 1185 ms 115000 KB Output is correct
18 Correct 1004 ms 113236 KB Output is correct
19 Correct 864 ms 112496 KB Output is correct
20 Correct 876 ms 112672 KB Output is correct
21 Correct 958 ms 113644 KB Output is correct
22 Correct 904 ms 112728 KB Output is correct
23 Correct 1062 ms 114884 KB Output is correct
24 Correct 759 ms 112580 KB Output is correct
25 Correct 675 ms 112240 KB Output is correct
26 Correct 681 ms 112324 KB Output is correct
27 Correct 760 ms 112112 KB Output is correct
28 Correct 1266 ms 120036 KB Output is correct
29 Correct 1274 ms 120044 KB Output is correct
30 Correct 1275 ms 120032 KB Output is correct
31 Correct 1316 ms 120164 KB Output is correct
32 Correct 921 ms 112776 KB Output is correct
33 Correct 1173 ms 114240 KB Output is correct
34 Correct 438 ms 37008 KB Output is correct
35 Correct 1309 ms 116204 KB Output is correct
36 Correct 1087 ms 114540 KB Output is correct
37 Correct 1293 ms 115564 KB Output is correct
38 Correct 1134 ms 115052 KB Output is correct
39 Correct 1155 ms 121072 KB Output is correct
40 Correct 1318 ms 122664 KB Output is correct
41 Correct 979 ms 113900 KB Output is correct
42 Correct 809 ms 112388 KB Output is correct
43 Correct 1355 ms 120520 KB Output is correct
44 Correct 1107 ms 114792 KB Output is correct
45 Correct 914 ms 119664 KB Output is correct
46 Correct 936 ms 119276 KB Output is correct
47 Correct 1288 ms 120292 KB Output is correct
48 Correct 1265 ms 120032 KB Output is correct
49 Correct 1283 ms 120300 KB Output is correct
50 Correct 1278 ms 120032 KB Output is correct
51 Correct 1226 ms 122092 KB Output is correct
52 Correct 1208 ms 121844 KB Output is correct