답안 #637847

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
637847 2022-09-03T12:09:24 Z fatemetmhr Cats or Dogs (JOI18_catdog) C++17
100 / 100
793 ms 28740 KB
//  ~Be name khoda~  //
 
#include "catdog.h"
#include<bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second
 
const int maxn  =  1e6   + 10;
const int maxn5 =  1e5   + 10;
const int maxnt =  4e5   + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   +  7;
const ll  inf   =  1e18;
 
int n, id[maxn5], ti = -1, top[maxn5], par[maxn5];
int retnode[maxn5], a[maxn5], av[maxn5][3], big[maxn5];
int sz[maxn5], ans = 0;
vector <int> adj[maxn5];

struct catdog{
    int add[3], fnres[3];
    bool empty;
    catdog(){
        empty = false;
        add[0] = add[1] = add[2] = fnres[1] = fnres[2] = fnres[0] = 0;
    }
} seg[maxnt], empty_node, res;

catdog operator +(catdog a, catdog b){
    if(a.empty)
        return b;
    if(b.empty)
        return a;
    for(int i = 0; i < 3; i++){
        res.fnres[i] = b.fnres[a.fnres[i]];
        res.add[i] = b.add[a.fnres[i]] + a.add[i];
    }
    return res;
}

void update(int l, int r, int id, int v){
    if(r - l == 1){
        if(a[l] == 1){
            seg[v].fnres[0] = seg[v].fnres[1] = seg[v].fnres[2] = 1;
            seg[v].add[0] = seg[v].add[1] = seg[v].add[2] = av[l][2];
            seg[v].add[2]++;
        }
        if(a[l] == 2){
            seg[v].fnres[0] = seg[v].fnres[1] = seg[v].fnres[2] = 2;
            seg[v].add[0] = seg[v].add[1] = seg[v].add[2] = av[l][1];
            seg[v].add[1]++;
        }
        if(a[l] == 0){
            for(int i = 0; i < 3; i++){
                av[l][i]++;
                if(av[l][1] < av[l][2]){
                    seg[v].fnres[i] = 2;
                    seg[v].add[i] = av[l][1];
                }
                if(av[l][1] > av[l][2]){
                    seg[v].fnres[i] = 1;
                    seg[v].add[i] = av[l][2];
                }
                if(av[l][1] == av[l][2]){
                    seg[v].fnres[i] = 0;
                    seg[v].add[i] = av[l][1];
                }
                av[l][i]--;
            }
     
        }
        return;
    }

    int mid = (l + r) >> 1;
    if(id < mid)
        update(l, mid, id, v * 2);
    else
        update(mid, r, id, v * 2 + 1);
    seg[v] = seg[v * 2 + 1] + seg[v * 2];
    return;
}

catdog get(int l, int r, int lq, int rq, int v){
    if(rq <= l || r <= lq)
        return empty_node;
    if(lq <= l && r <= rq)
        return seg[v];
    int mid = (l + r) >> 1;
    return get(mid, r, lq, rq, v * 2 + 1) + get(l, mid, lq, rq, v * 2);
}

void build(int l, int r, int v){
    if(r - l == 1){
        seg[v].fnres[1] = 1;
        seg[v].fnres[2] = 2;
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, v * 2);
    build(mid, r, v * 2 + 1);
    seg[v] = seg[v * 2 + 1] + seg[v * 2];
    return;
}
 
void upd(int v){
    auto last = get(0, n, id[top[v]], id[retnode[top[v]]] + 1, 1);
    ans -= last.add[0];
    update(0, n, id[v], 1);
    auto cur = get(0, n, id[top[v]], id[retnode[top[v]]] + 1, 1);
    ans += cur.add[0];
    if(par[top[v]] == -1)
        return;
    av[id[par[top[v]]]][last.fnres[0]]--;
    av[id[par[top[v]]]][cur.fnres[0]]++;
    upd(par[top[v]]);
    return;
}

void dfs_det(int v){
    sz[v] = 1;
    big[v] = -1;
    for(auto u : adj[v]) if(u != par[v]){
        par[u] = v;
        dfs_det(u);
        sz[v] += sz[u];
        if(big[v] == -1 || sz[u] > sz[big[v]])
            big[v] = u;
    }
    return;
}

void dfs_hld(int v){
    id[v] = ++ti;
    retnode[top[v]] = v;
    if(big[v] == -1)
        return;
    top[big[v]] = top[v];
    dfs_hld(big[v]);
    for(auto u : adj[v]) if(u != big[v] && u != par[v]){
        top[u] = u;
        dfs_hld(u);
    }
    return;
}

int cat(int v) {
    v--;
    a[id[v]] = 1;
    upd(v);
    return ans;
}
 
int dog(int v) {
    v--;
    a[id[v]] = 2;
    upd(v);
    return ans;
}
 
int neighbor(int v) {
    v--;
    a[id[v]] = 0;
    upd(v);
    return ans;
}
 
void initialize(int N, std::vector<int> A, std::vector<int> B) {
    n = N;
    empty_node.empty = true;
    build(0, n, 1);
    for(int i = 0; i < n - 1; i++){
        A[i]--; B[i]--;
        adj[A[i]].pb(B[i]);
        adj[B[i]].pb(A[i]);
    }
    par[0] = -1;
    dfs_det(0);
    dfs_hld(0);
    return;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 13652 KB Output is correct
2 Correct 7 ms 13652 KB Output is correct
3 Correct 7 ms 13624 KB Output is correct
4 Correct 7 ms 13552 KB Output is correct
5 Correct 8 ms 13524 KB Output is correct
6 Correct 8 ms 13584 KB Output is correct
7 Correct 7 ms 13632 KB Output is correct
8 Correct 7 ms 13608 KB Output is correct
9 Correct 6 ms 13652 KB Output is correct
10 Correct 7 ms 13612 KB Output is correct
11 Correct 7 ms 13524 KB Output is correct
12 Correct 8 ms 13524 KB Output is correct
13 Correct 7 ms 13528 KB Output is correct
14 Correct 6 ms 13652 KB Output is correct
15 Correct 7 ms 13600 KB Output is correct
16 Correct 9 ms 13672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 13652 KB Output is correct
2 Correct 7 ms 13652 KB Output is correct
3 Correct 7 ms 13624 KB Output is correct
4 Correct 7 ms 13552 KB Output is correct
5 Correct 8 ms 13524 KB Output is correct
6 Correct 8 ms 13584 KB Output is correct
7 Correct 7 ms 13632 KB Output is correct
8 Correct 7 ms 13608 KB Output is correct
9 Correct 6 ms 13652 KB Output is correct
10 Correct 7 ms 13612 KB Output is correct
11 Correct 7 ms 13524 KB Output is correct
12 Correct 8 ms 13524 KB Output is correct
13 Correct 7 ms 13528 KB Output is correct
14 Correct 6 ms 13652 KB Output is correct
15 Correct 7 ms 13600 KB Output is correct
16 Correct 9 ms 13672 KB Output is correct
17 Correct 9 ms 13652 KB Output is correct
18 Correct 9 ms 13652 KB Output is correct
19 Correct 9 ms 13652 KB Output is correct
20 Correct 7 ms 13652 KB Output is correct
21 Correct 8 ms 13652 KB Output is correct
22 Correct 8 ms 13652 KB Output is correct
23 Correct 9 ms 13652 KB Output is correct
24 Correct 9 ms 13676 KB Output is correct
25 Correct 8 ms 13600 KB Output is correct
26 Correct 7 ms 13652 KB Output is correct
27 Correct 9 ms 13652 KB Output is correct
28 Correct 9 ms 13652 KB Output is correct
29 Correct 9 ms 13652 KB Output is correct
30 Correct 7 ms 13580 KB Output is correct
31 Correct 7 ms 13652 KB Output is correct
32 Correct 8 ms 13620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 13652 KB Output is correct
2 Correct 7 ms 13652 KB Output is correct
3 Correct 7 ms 13624 KB Output is correct
4 Correct 7 ms 13552 KB Output is correct
5 Correct 8 ms 13524 KB Output is correct
6 Correct 8 ms 13584 KB Output is correct
7 Correct 7 ms 13632 KB Output is correct
8 Correct 7 ms 13608 KB Output is correct
9 Correct 6 ms 13652 KB Output is correct
10 Correct 7 ms 13612 KB Output is correct
11 Correct 7 ms 13524 KB Output is correct
12 Correct 8 ms 13524 KB Output is correct
13 Correct 7 ms 13528 KB Output is correct
14 Correct 6 ms 13652 KB Output is correct
15 Correct 7 ms 13600 KB Output is correct
16 Correct 9 ms 13672 KB Output is correct
17 Correct 9 ms 13652 KB Output is correct
18 Correct 9 ms 13652 KB Output is correct
19 Correct 9 ms 13652 KB Output is correct
20 Correct 7 ms 13652 KB Output is correct
21 Correct 8 ms 13652 KB Output is correct
22 Correct 8 ms 13652 KB Output is correct
23 Correct 9 ms 13652 KB Output is correct
24 Correct 9 ms 13676 KB Output is correct
25 Correct 8 ms 13600 KB Output is correct
26 Correct 7 ms 13652 KB Output is correct
27 Correct 9 ms 13652 KB Output is correct
28 Correct 9 ms 13652 KB Output is correct
29 Correct 9 ms 13652 KB Output is correct
30 Correct 7 ms 13580 KB Output is correct
31 Correct 7 ms 13652 KB Output is correct
32 Correct 8 ms 13620 KB Output is correct
33 Correct 396 ms 18724 KB Output is correct
34 Correct 140 ms 19020 KB Output is correct
35 Correct 405 ms 17560 KB Output is correct
36 Correct 612 ms 22316 KB Output is correct
37 Correct 22 ms 16144 KB Output is correct
38 Correct 793 ms 23172 KB Output is correct
39 Correct 648 ms 23172 KB Output is correct
40 Correct 711 ms 23180 KB Output is correct
41 Correct 666 ms 23180 KB Output is correct
42 Correct 576 ms 23192 KB Output is correct
43 Correct 605 ms 23176 KB Output is correct
44 Correct 560 ms 23224 KB Output is correct
45 Correct 587 ms 23188 KB Output is correct
46 Correct 624 ms 23228 KB Output is correct
47 Correct 639 ms 23176 KB Output is correct
48 Correct 177 ms 20588 KB Output is correct
49 Correct 186 ms 22316 KB Output is correct
50 Correct 70 ms 15616 KB Output is correct
51 Correct 88 ms 17036 KB Output is correct
52 Correct 34 ms 15388 KB Output is correct
53 Correct 263 ms 22036 KB Output is correct
54 Correct 194 ms 17492 KB Output is correct
55 Correct 498 ms 20436 KB Output is correct
56 Correct 336 ms 18116 KB Output is correct
57 Correct 423 ms 21512 KB Output is correct
58 Correct 31 ms 16764 KB Output is correct
59 Correct 76 ms 16408 KB Output is correct
60 Correct 162 ms 20260 KB Output is correct
61 Correct 160 ms 20684 KB Output is correct
62 Correct 104 ms 19096 KB Output is correct
63 Correct 53 ms 19884 KB Output is correct
64 Correct 51 ms 21400 KB Output is correct
65 Correct 66 ms 25788 KB Output is correct
66 Correct 82 ms 16648 KB Output is correct
67 Correct 74 ms 22208 KB Output is correct
68 Correct 135 ms 25672 KB Output is correct
69 Correct 47 ms 14852 KB Output is correct
70 Correct 13 ms 13780 KB Output is correct
71 Correct 71 ms 19416 KB Output is correct
72 Correct 90 ms 24268 KB Output is correct
73 Correct 244 ms 28740 KB Output is correct
74 Correct 326 ms 25164 KB Output is correct
75 Correct 173 ms 28640 KB Output is correct
76 Correct 158 ms 27384 KB Output is correct
77 Correct 324 ms 25668 KB Output is correct