Submission #1022679

# Submission time Handle Problem Language Result Execution time Memory
1022679 2024-07-13T22:39:55 Z efishel Werewolf (IOI18_werewolf) C++17
100 / 100
2395 ms 398328 KB
#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;
using vi = vector <int>;
using ii = pair <ll, ll>;
using vii = vector <ii>;

const ll MAXN = 2E5+16;

struct Edge {
    ll u, v, w;
};

struct DSU {
    vll par, sz, nn, bioPar;
    vector <vll> anc;
    vll tin, tout;
    ll timer;
    ll n;
    vii funnyEgs;
    vector <vll> adj;
    function <ll(ll, ll)> a;

    ll newId () {
        ll id = par.size();
        par.push_back(id);
        bioPar.push_back(id);
        sz.push_back(1);
        adj.push_back(vll({}));
        nn.push_back(-15);
        tin.push_back(-15);
        tout.push_back(-15);
        anc.push_back(vll({}));
        return id;
    }

    DSU (ll n, function <ll(ll, ll)> a): par(n), sz(n, 1), nn(n), bioPar(n), tin(n, -15), tout(n, -15), timer(0), anc(n), /* qs(n, vii({})), */ funnyEgs(0), adj(n, vll({})), a(a), n(n) { iota(par.begin(), par.end(), 0); iota(nn.begin(), nn.end(), 0); }

    ll find (ll u) { return u == par[u] ? u : par[u] = find(par[u]); }

    void join (ll u, ll v) {
        u = find(u);
        v = find(v);
        if (u == v) return;
        ll id = newId();
        adj[id].push_back(u);
        adj[id].push_back(v);
        bioPar[id] = id;
        bioPar[u] = par[u] = id;
        bioPar[v] = par[v] = id;
        nn[id] = a(nn[u], nn[v]);
    }

    ll findRoot (ll u, ll x) {
        for (ll bit = 17; bit >= 0; bit--) {
            if (a(nn[anc[u][bit]], x) != x) continue;
            u = anc[u][bit];
        }
        assert(a(nn[u], x) == x);
        if (u != anc[u][0]) assert(a(nn[anc[u][0]], x) != x);
        return u;
    }

    vll doDfs (ll u, ll x) {
        ll ahm = findRoot(u, x);
        vll ans;
        dfs(ahm, ans);
        return ans;
    }

    ii getRange (ll u, ll x) {
        ll ahm = findRoot(u, x);
        return { tin[ahm], tout[ahm] };
    }

    void buildBinLift () {
        for (ll bit = 1; bit < 18; bit++) {
            for (ll u = 0; u < anc.size(); u++) {
                assert(anc[u].size() == bit);
                anc[u].push_back(anc[anc[u][bit-1]][bit-1]);
            }
        }
    }

    void dfs (ll u, vll &sub) {
        tin[u] = timer;
        if (u < n) timer++;
        anc[u] = { bioPar[u] };
        for (ll v : adj[u])
            dfs(v, sub);
        tout[u] = timer-1;
    }
};

struct SegTree {
    vector <vll> tree;
    ll n;

    SegTree (ll n): tree(2*n, vll({})), n(n) {}

    void update (ll id, ll val) {
        for (id += n; id > 0; id >>= 1)
            tree[id].push_back(val);
    }

    void build () {
        for (ll id = 0; id < 2*n; id++)
            sort(tree[id].begin(), tree[id].end());
    }

    bool query (ll ql, ll qr, ll l, ll r) {
        bool ans = false;
        for (ql += n, qr += n+1; ql < qr; ql >>= 1, qr >>= 1) {
            if (ql&1) { ans |= upper_bound(tree[ql].begin(), tree[ql].end(), r) - lower_bound(tree[ql].begin(), tree[ql].end(), l); ql++; }
            if (qr&1) { --qr; ans |= upper_bound(tree[qr].begin(), tree[qr].end(), r) - lower_bound(tree[qr].begin(), tree[qr].end(), l); }
        }
        return ans;
    }
};

vi check_validity (int n, vi us, vi vs, vi s, vi e, vi vl, vi vr) {
    ll Q = s.size();
    vector <Edge> egsMn, egsMx;
    for (ll i = 0; i < us.size(); i++) {
        egsMn.push_back({ us[i], vs[i], min(us[i], vs[i]) });
        egsMx.push_back({ us[i], vs[i], max(us[i], vs[i]) });
    }
    sort(egsMn.begin(), egsMn.end(), [&](Edge a, Edge b) { return a.w > b.w; });
    sort(egsMx.begin(), egsMx.end(), [&](Edge a, Edge b) { return a.w < b.w; });
    DSU dsuMn(n, [&](ll a, ll b) { return min(a, b); });
    for (auto [u, v, w] : egsMn) dsuMn.join(u, v);
    DSU dsuMx(n, [&](ll a, ll b) { return max(a, b); });
    for (auto [u, v, w] : egsMx) dsuMx.join(u, v);
    vll ordMn, ordMx;
    dsuMn.dfs(dsuMn.par.size()-1, ordMn);
    dsuMn.buildBinLift();
    dsuMx.dfs(dsuMx.par.size()-1, ordMx);
    dsuMx.buildBinLift();
    SegTree st(n);
    for (ll u = 0; u < n; u++)
        st.update(dsuMx.tin[u], dsuMn.tin[u]);
    st.build();
    vi ans(Q, -15);
    for (ll qid = 0; qid < Q; qid++) {
        auto [lMn, rMn] = dsuMn.getRange(s[qid], vl[qid]);
        auto [lMx, rMx] = dsuMx.getRange(e[qid], vr[qid]);
        ans[qid] = st.query(lMx, rMx, lMn, rMn);
    }
    return ans;
}

Compilation message

werewolf.cpp: In constructor 'DSU::DSU(ll, std::function<long long int(long long int, long long int)>)':
werewolf.cpp:20:8: warning: 'DSU::timer' will be initialized after [-Wreorder]
   20 |     ll timer;
      |        ^~~~~
werewolf.cpp:18:18: warning:   'std::vector<std::vector<long long int> > DSU::anc' [-Wreorder]
   18 |     vector <vll> anc;
      |                  ^~~
werewolf.cpp:39:5: warning:   when initialized here [-Wreorder]
   39 |     DSU (ll n, function <ll(ll, ll)> a): par(n), sz(n, 1), nn(n), bioPar(n), tin(n, -15), tout(n, -15), timer(0), anc(n), /* qs(n, vii({})), */ funnyEgs(0), adj(n, vll({})), a(a), n(n) { iota(par.begin(), par.end(), 0); iota(nn.begin(), nn.end(), 0); }
      |     ^~~
werewolf.cpp:24:27: warning: 'DSU::a' will be initialized after [-Wreorder]
   24 |     function <ll(ll, ll)> a;
      |                           ^
werewolf.cpp:21:8: warning:   'll DSU::n' [-Wreorder]
   21 |     ll n;
      |        ^
werewolf.cpp:39:5: warning:   when initialized here [-Wreorder]
   39 |     DSU (ll n, function <ll(ll, ll)> a): par(n), sz(n, 1), nn(n), bioPar(n), tin(n, -15), tout(n, -15), timer(0), anc(n), /* qs(n, vii({})), */ funnyEgs(0), adj(n, vll({})), a(a), n(n) { iota(par.begin(), par.end(), 0); iota(nn.begin(), nn.end(), 0); }
      |     ^~~
werewolf.cpp: In member function 'void DSU::buildBinLift()':
werewolf.cpp:80:30: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |             for (ll u = 0; u < anc.size(); u++) {
      |                            ~~^~~~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from werewolf.cpp:2:
werewolf.cpp:81:38: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   81 |                 assert(anc[u].size() == bit);
      |                        ~~~~~~~~~~~~~~^~~~~~
werewolf.cpp: In function 'vi check_validity(int, vi, vi, vi, vi, vi, vi)':
werewolf.cpp:126:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |     for (ll i = 0; i < us.size(); i++) {
      |                    ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 13 ms 5980 KB Output is correct
11 Correct 12 ms 5820 KB Output is correct
12 Correct 10 ms 5980 KB Output is correct
13 Correct 13 ms 5984 KB Output is correct
14 Correct 13 ms 6096 KB Output is correct
15 Correct 12 ms 6144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1419 ms 380820 KB Output is correct
2 Correct 1736 ms 384368 KB Output is correct
3 Correct 1688 ms 382448 KB Output is correct
4 Correct 1461 ms 381556 KB Output is correct
5 Correct 1545 ms 381680 KB Output is correct
6 Correct 1583 ms 381296 KB Output is correct
7 Correct 1459 ms 380848 KB Output is correct
8 Correct 1559 ms 384288 KB Output is correct
9 Correct 1229 ms 382240 KB Output is correct
10 Correct 1076 ms 381456 KB Output is correct
11 Correct 1122 ms 381828 KB Output is correct
12 Correct 1233 ms 381292 KB Output is correct
13 Correct 1683 ms 384720 KB Output is correct
14 Correct 1904 ms 384596 KB Output is correct
15 Correct 1885 ms 384704 KB Output is correct
16 Correct 1870 ms 384632 KB Output is correct
17 Correct 1532 ms 380804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 13 ms 5980 KB Output is correct
11 Correct 12 ms 5820 KB Output is correct
12 Correct 10 ms 5980 KB Output is correct
13 Correct 13 ms 5984 KB Output is correct
14 Correct 13 ms 6096 KB Output is correct
15 Correct 12 ms 6144 KB Output is correct
16 Correct 1419 ms 380820 KB Output is correct
17 Correct 1736 ms 384368 KB Output is correct
18 Correct 1688 ms 382448 KB Output is correct
19 Correct 1461 ms 381556 KB Output is correct
20 Correct 1545 ms 381680 KB Output is correct
21 Correct 1583 ms 381296 KB Output is correct
22 Correct 1459 ms 380848 KB Output is correct
23 Correct 1559 ms 384288 KB Output is correct
24 Correct 1229 ms 382240 KB Output is correct
25 Correct 1076 ms 381456 KB Output is correct
26 Correct 1122 ms 381828 KB Output is correct
27 Correct 1233 ms 381292 KB Output is correct
28 Correct 1683 ms 384720 KB Output is correct
29 Correct 1904 ms 384596 KB Output is correct
30 Correct 1885 ms 384704 KB Output is correct
31 Correct 1870 ms 384632 KB Output is correct
32 Correct 1532 ms 380804 KB Output is correct
33 Correct 1966 ms 382064 KB Output is correct
34 Correct 342 ms 46548 KB Output is correct
35 Correct 2395 ms 385400 KB Output is correct
36 Correct 1803 ms 382072 KB Output is correct
37 Correct 2309 ms 384452 KB Output is correct
38 Correct 1917 ms 382840 KB Output is correct
39 Correct 1971 ms 387588 KB Output is correct
40 Correct 1675 ms 397996 KB Output is correct
41 Correct 1511 ms 383648 KB Output is correct
42 Correct 1178 ms 382148 KB Output is correct
43 Correct 1975 ms 392236 KB Output is correct
44 Correct 1865 ms 384292 KB Output is correct
45 Correct 1539 ms 388388 KB Output is correct
46 Correct 1650 ms 387660 KB Output is correct
47 Correct 1715 ms 385156 KB Output is correct
48 Correct 1650 ms 384884 KB Output is correct
49 Correct 1656 ms 384884 KB Output is correct
50 Correct 1575 ms 384620 KB Output is correct
51 Correct 1411 ms 398104 KB Output is correct
52 Correct 1373 ms 398328 KB Output is correct