Submission #792680

# Submission time Handle Problem Language Result Execution time Memory
792680 2023-07-25T08:02:47 Z fatemetmhr Werewolf (IOI18_werewolf) C++17
100 / 100
1382 ms 160756 KB
#include "werewolf.h"
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define pb     push_back
#define fi     first
#define se     second
#define mp     make_pair

typedef long long ll;

const int maxn5 = 5e5 + 10;
const int lg    = 20;

vector <int> ret, adj[maxn5];
vector <pair<int, pair<int, int>>> req[maxn5];
int n;

void gadd(int);
void grem(int);
void gans(int, int, int);

namespace fen{

    int fen[maxn5];

    void add(int id, int val){
        for(; id < maxn5; id += id & -id)
            fen[id] += val;
    }

    int get(int id){
        int ret = 0;
        for(; id; id -= id & -id)
            ret += fen[id];
        return ret;
    }

    int get(int l, int r){
        return get(r) - get(l - 1);
    }
}

struct dsu{

    int lcpar[lg][maxn5], par[maxn5], ti, st[maxn5], ft[maxn5];
    int ver[maxn5], sz[maxn5], newnode, val[maxn5];
    pair <int, int> ed[maxn5];
    bool mark[maxn5];


    dsu(){
        memset(lcpar, -1, sizeof lcpar);
        memset(par, -1, sizeof par);
        memset(mark, false, sizeof mark);
        memset(ed, -1, sizeof ed);
        ti = 2;
    }

    int get_par(int a){return par[a] == -1 ? a : par[a] = get_par(par[a]);}

    void add(int x){
        mark[x] = true;
        for(auto u : adj[x]) if(mark[u]){
            int a = get_par(u), b = get_par(x);
            //////cout << u << ' ' << x << ' ' << a << ' ' << b << endl;
            if(a == b)
                continue;
            //////cout << "ya merging " << a << ' ' << b << ' ' << newnode << endl; 
            par[a] = par[b] = lcpar[0][a] = lcpar[0][b] = newnode;
            ed[newnode] = {a, b};
            val[newnode] = x;
            newnode++;
        }
    }

    void dfs_det(int v){
        sz[v] = 1;
        for(int i = 1; i < lg && lcpar[i - 1][v] != -1; i++)
            lcpar[i][v] = lcpar[i - 1][lcpar[i - 1][v]];
        st[v] = ++ti;
        ver[ti] = v;
        if(ed[v].fi == -1){
            ft[v] = ti;
            return;
        }
        dfs_det(ed[v].fi);
        dfs_det(ed[v].se);
        sz[v] += sz[ed[v].fi] + sz[ed[v].se];
        if(sz[ed[v].fi] < sz[ed[v].se])
            swap(ed[v].fi, ed[v].se);
        ft[v] = ti;
    }

    void query(int id, int l, int r, int s, int e){
        int x = s;
        for(int i = lg - 1; i >= 0; i--) if(lcpar[i][x] != -1 && val[lcpar[i][x]] >= l)
            x = lcpar[i][x];
        //cout << "in quer of " << l << ' ' << r << ' ' << s << ' ' << e << ' ' << x << ' ' << val[x] << ' ' << newnode << endl;
        req[x].pb({id, {r, e}});
    }

    void inc(int id){
        //cout << "right " << st[id] << endl;
        fen::add(st[id], 1);
    }

    void dec(int id){
        fen::add(st[id], -1);
    }

    void answ(int id, int r, int e){
        int x = e;
        for(int i = lg - 1; i >= 0; i--) if(lcpar[i][x] != -1 && val[lcpar[i][x]] <= r)
            x = lcpar[i][x];
        //cout << "answering " << r << ' ' << e << ' ' << x << ' ' << lcpar[0][e] << ' ' << val[lcpar[0][e]] << endl;
        //cout << st[x] << ' ' << ft[x] << endl;
        ret[id] = fen::get(st[x], ft[x]) > 0;
    }

    void gooni(int v, bool keep){
        //cout << "allright " << v << ' ' << lcpar[0][v] << ' ' << keep << ' ' << val[v] << endl;
        if(ed[v].fi == -1){
            gadd(v);
            for(auto [id, p] : req[v])
                gans(id, p.fi, p.se);
            if(!keep)
                grem(v);
            return;
        }
        gooni(ed[v].se, false);
        gooni(ed[v].fi, true);
        gadd(v);
        for(int i = st[ed[v].se]; i <= ft[ed[v].se]; i++)
            gadd(ver[i]);
        for(auto [id, p] : req[v])
            gans(id, p.fi, p.se);
        if(!keep) 
            for(int i = st[v]; i <= ft[v]; i++) 
                grem(ver[i]);
        return;
    }

} dsu0, dsuN;


void gadd(int v){
    if(v >= n)
        return;
    //cout << "adding " << v << endl;
    dsu0.inc(v);
}

void grem(int v){
    if(v >= n)
        return;
    //cout << "removing " << v << endl;
    dsu0.dec(v);
}

void gans(int a, int b, int c){
    //cout << "going to ans query of " << a << ' ' << b << ' ' << c << endl;
    dsu0.answ(a, b, c);
}


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;
    int q = s.size();
    for(int i = 0; i < q; i++){
        ret.pb(0);
    }
    int m = x.size();
    for(int i = 0; i < m; i++){
        adj[x[i]].pb(y[i]);
        adj[y[i]].pb(x[i]);
    }
    dsu0.newnode = dsuN.newnode = n;
    for(int i = 0; i < n; i++){
        //////cout << "in dsu 0 " << endl;
        dsu0.add(i);
        //////cout << "in dsu N" << endl;
        dsuN.add(n - i - 1);
        dsu0.val[i] = dsuN.val[i] = i;
    }
    dsuN.dfs_det(dsuN.newnode - 1);
    dsu0.dfs_det(dsu0.newnode - 1);
    //////cout << dsu0.lcpar[0][1] << endl;
    for(int i = 0; i < q; i++)
        dsuN.query(i, l[i], r[i], s[i], e[i]);
    dsuN.gooni(dsuN.newnode - 1, true);
    return ret;
}

Compilation message

werewolf.cpp: In constructor 'dsu::dsu()':
werewolf.cpp:58:33: warning: 'void* memset(void*, int, size_t)' writing to an object of type 'struct std::pair<int, int>' with no trivial copy-assignment [-Wclass-memaccess]
   58 |         memset(ed, -1, sizeof ed);
      |                                 ^
In file included from /usr/include/c++/10/bits/stl_algobase.h:64,
                 from /usr/include/c++/10/vector:60,
                 from werewolf.h:3,
                 from werewolf.cpp:1:
/usr/include/c++/10/bits/stl_pair.h:211:12: note: 'struct std::pair<int, int>' declared here
  211 |     struct pair
      |            ^~~~
# Verdict Execution time Memory Grader output
1 Correct 45 ms 114868 KB Output is correct
2 Correct 39 ms 114824 KB Output is correct
3 Correct 41 ms 114832 KB Output is correct
4 Correct 40 ms 114796 KB Output is correct
5 Correct 42 ms 114796 KB Output is correct
6 Correct 39 ms 114848 KB Output is correct
7 Correct 41 ms 115004 KB Output is correct
8 Correct 39 ms 114832 KB Output is correct
9 Correct 37 ms 114824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 114868 KB Output is correct
2 Correct 39 ms 114824 KB Output is correct
3 Correct 41 ms 114832 KB Output is correct
4 Correct 40 ms 114796 KB Output is correct
5 Correct 42 ms 114796 KB Output is correct
6 Correct 39 ms 114848 KB Output is correct
7 Correct 41 ms 115004 KB Output is correct
8 Correct 39 ms 114832 KB Output is correct
9 Correct 37 ms 114824 KB Output is correct
10 Correct 42 ms 115644 KB Output is correct
11 Correct 44 ms 115604 KB Output is correct
12 Correct 42 ms 115460 KB Output is correct
13 Correct 43 ms 115636 KB Output is correct
14 Correct 44 ms 115524 KB Output is correct
15 Correct 44 ms 115660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 749 ms 154160 KB Output is correct
2 Correct 695 ms 155492 KB Output is correct
3 Correct 538 ms 153928 KB Output is correct
4 Correct 606 ms 152824 KB Output is correct
5 Correct 628 ms 153096 KB Output is correct
6 Correct 674 ms 153492 KB Output is correct
7 Correct 561 ms 152924 KB Output is correct
8 Correct 541 ms 155468 KB Output is correct
9 Correct 470 ms 153796 KB Output is correct
10 Correct 334 ms 152768 KB Output is correct
11 Correct 345 ms 152772 KB Output is correct
12 Correct 391 ms 152572 KB Output is correct
13 Correct 799 ms 157680 KB Output is correct
14 Correct 762 ms 157652 KB Output is correct
15 Correct 715 ms 157576 KB Output is correct
16 Correct 758 ms 157644 KB Output is correct
17 Correct 713 ms 153116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 114868 KB Output is correct
2 Correct 39 ms 114824 KB Output is correct
3 Correct 41 ms 114832 KB Output is correct
4 Correct 40 ms 114796 KB Output is correct
5 Correct 42 ms 114796 KB Output is correct
6 Correct 39 ms 114848 KB Output is correct
7 Correct 41 ms 115004 KB Output is correct
8 Correct 39 ms 114832 KB Output is correct
9 Correct 37 ms 114824 KB Output is correct
10 Correct 42 ms 115644 KB Output is correct
11 Correct 44 ms 115604 KB Output is correct
12 Correct 42 ms 115460 KB Output is correct
13 Correct 43 ms 115636 KB Output is correct
14 Correct 44 ms 115524 KB Output is correct
15 Correct 44 ms 115660 KB Output is correct
16 Correct 749 ms 154160 KB Output is correct
17 Correct 695 ms 155492 KB Output is correct
18 Correct 538 ms 153928 KB Output is correct
19 Correct 606 ms 152824 KB Output is correct
20 Correct 628 ms 153096 KB Output is correct
21 Correct 674 ms 153492 KB Output is correct
22 Correct 561 ms 152924 KB Output is correct
23 Correct 541 ms 155468 KB Output is correct
24 Correct 470 ms 153796 KB Output is correct
25 Correct 334 ms 152768 KB Output is correct
26 Correct 345 ms 152772 KB Output is correct
27 Correct 391 ms 152572 KB Output is correct
28 Correct 799 ms 157680 KB Output is correct
29 Correct 762 ms 157652 KB Output is correct
30 Correct 715 ms 157576 KB Output is correct
31 Correct 758 ms 157644 KB Output is correct
32 Correct 713 ms 153116 KB Output is correct
33 Correct 1122 ms 155068 KB Output is correct
34 Correct 233 ms 138856 KB Output is correct
35 Correct 1382 ms 158180 KB Output is correct
36 Correct 1358 ms 154392 KB Output is correct
37 Correct 1292 ms 157456 KB Output is correct
38 Correct 1310 ms 155056 KB Output is correct
39 Correct 1168 ms 157180 KB Output is correct
40 Correct 849 ms 160016 KB Output is correct
41 Correct 763 ms 156596 KB Output is correct
42 Correct 572 ms 154020 KB Output is correct
43 Correct 907 ms 160756 KB Output is correct
44 Correct 825 ms 157468 KB Output is correct
45 Correct 574 ms 157248 KB Output is correct
46 Correct 600 ms 156764 KB Output is correct
47 Correct 741 ms 158196 KB Output is correct
48 Correct 825 ms 158164 KB Output is correct
49 Correct 715 ms 158160 KB Output is correct
50 Correct 765 ms 158164 KB Output is correct
51 Correct 589 ms 160584 KB Output is correct
52 Correct 605 ms 160564 KB Output is correct