답안 #1097829

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1097829 2024-10-08T09:57:48 Z Neco_arc Cats or Dogs (JOI18_catdog) C++17
100 / 100
616 ms 36176 KB
#include<catdog.h>

#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define Neco "Cats or Dogs"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7

using namespace std;

const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
    return uniform_int_distribution<ll> (l, r)(rng);
}


int n;
int cl[maxn], last[maxn];
int In[maxn], nex[maxn], par[maxn], siz[maxn];
vector<int> edges[maxn];

int dfs_siz(int u) {
    siz[u] = 1;
    for(int &v : edges[u]) {
        edges[v].erase(find(all(edges[v]), u));
        siz[u] += dfs_siz(v);

        if(siz[v] > siz[edges[u][0]]) swap(v, edges[u][0]);
    }
    return siz[u];
}

void dfs_hld(int u) {
    static int Time = 0;
    In[u] = ++Time;
    for(int v : edges[u]) {
        par[v] = u;
        nex[v] = v == edges[u][0] ? nex[u] : v;
        dfs_hld(v);
    }
}

struct IT {
    struct Node {
        int a[2][2];
        Node() { a[0][0] = a[1][1] = 0, a[0][1] = a[1][0] = 1e9; }

    } st[4 * maxn];

    Node cb(const Node &x, const Node &y) {
        Node ans; memset(ans.a, 60, sizeof ans.a);
        fi(i, 0, 1) fi(j, 0, 1) fi(u, 0, 1) fi(v, 0, 1) {
            ans.a[i][v] = min(ans.a[i][v], x.a[i][j] + y.a[u][v] + (j != u));
        }
        return ans;
    }

    void update(int x, int val0, int val1, int id = 1, int l = 1, int r = n) {
        if(l > x || r < x) return;
        if(l == r) {
            st[id].a[0][0] += val0, st[id].a[1][1] += val1;
            return;
        }

        int mid = (l + r) >> 1;
        update(x, val0, val1, _left), update(x, val0, val1, _right);
        st[id] = cb(st[id * 2], st[id * 2 + 1]);
    }

    Node get(int u, int v, int id, int l, int r) {
        if(l > v || r < u) return Node();
        if(u <= l && r <= v) return st[id];

        int mid = (l + r) >> 1;
        return cb(get(u, v, _left), get(u, v, _right));
    }

    void get(int u, int v, int &A, int &B) {
        Node p = get(u, v, 1, 1, n);
        int x0 = min(p.a[0][1], p.a[0][0]);
        int x1 = min(p.a[1][0], p.a[1][1]);

        A = min(x0, x1 + 1);
        B = min(x0 + 1, x1);
    }

} St;

void update(int u, int col, int dt0, int dt1) {
    cl[u] = col;
    while(u) {
        int old0, old1, nw0, nw1;
        St.get(In[nex[u]], last[nex[u]], old0, old1);
        St.update(In[u], dt0, dt1);
        St.get(In[nex[u]], last[nex[u]], nw0, nw1);

        dt0 = nw0 - old0, dt1 = nw1 - old1;
        u = par[nex[u]];
    }
}

int get() {
    int ans0, ans1; St.get(In[1], last[1], ans0, ans1);
    return min(ans0, ans1);
}

int cat(int u) {
    update(u, 0, 0, 1e9);
    return get();
}

int dog(int u) {
    update(u, 1, 1e9, 0);
    return get();
}

int neighbor(int u) {
    if(cl[u]) update(u, -1, -1e9, 0);
    else update(u, -1, 0, -1e9);

    return get();
}

void initialize(int N, vector<int> A, vector<int> B) {
    n = N;
    fi(i, 0, n - 2) {
        edges[A[i]].push_back(B[i]), edges[B[i]].push_back(A[i]);
    }

    nex[1] = 1;
    dfs_siz(1), dfs_hld(1);
    fi(i, 1, n) last[nex[i]] = max(last[nex[i]], In[i]);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 21080 KB Output is correct
2 Correct 3 ms 21084 KB Output is correct
3 Correct 3 ms 21084 KB Output is correct
4 Correct 3 ms 21120 KB Output is correct
5 Correct 4 ms 21080 KB Output is correct
6 Correct 3 ms 21108 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 3 ms 20924 KB Output is correct
9 Correct 3 ms 21084 KB Output is correct
10 Correct 3 ms 21084 KB Output is correct
11 Correct 3 ms 21084 KB Output is correct
12 Correct 3 ms 21084 KB Output is correct
13 Correct 3 ms 21116 KB Output is correct
14 Correct 3 ms 21084 KB Output is correct
15 Correct 3 ms 21084 KB Output is correct
16 Correct 3 ms 21084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 21080 KB Output is correct
2 Correct 3 ms 21084 KB Output is correct
3 Correct 3 ms 21084 KB Output is correct
4 Correct 3 ms 21120 KB Output is correct
5 Correct 4 ms 21080 KB Output is correct
6 Correct 3 ms 21108 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 3 ms 20924 KB Output is correct
9 Correct 3 ms 21084 KB Output is correct
10 Correct 3 ms 21084 KB Output is correct
11 Correct 3 ms 21084 KB Output is correct
12 Correct 3 ms 21084 KB Output is correct
13 Correct 3 ms 21116 KB Output is correct
14 Correct 3 ms 21084 KB Output is correct
15 Correct 3 ms 21084 KB Output is correct
16 Correct 3 ms 21084 KB Output is correct
17 Correct 5 ms 21084 KB Output is correct
18 Correct 4 ms 21084 KB Output is correct
19 Correct 4 ms 21084 KB Output is correct
20 Correct 4 ms 21084 KB Output is correct
21 Correct 4 ms 21084 KB Output is correct
22 Correct 4 ms 21084 KB Output is correct
23 Correct 6 ms 21084 KB Output is correct
24 Correct 5 ms 21084 KB Output is correct
25 Correct 4 ms 21084 KB Output is correct
26 Correct 4 ms 21084 KB Output is correct
27 Correct 4 ms 21084 KB Output is correct
28 Correct 4 ms 21084 KB Output is correct
29 Correct 4 ms 21084 KB Output is correct
30 Correct 4 ms 21084 KB Output is correct
31 Correct 4 ms 21084 KB Output is correct
32 Correct 4 ms 21084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 21080 KB Output is correct
2 Correct 3 ms 21084 KB Output is correct
3 Correct 3 ms 21084 KB Output is correct
4 Correct 3 ms 21120 KB Output is correct
5 Correct 4 ms 21080 KB Output is correct
6 Correct 3 ms 21108 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 3 ms 20924 KB Output is correct
9 Correct 3 ms 21084 KB Output is correct
10 Correct 3 ms 21084 KB Output is correct
11 Correct 3 ms 21084 KB Output is correct
12 Correct 3 ms 21084 KB Output is correct
13 Correct 3 ms 21116 KB Output is correct
14 Correct 3 ms 21084 KB Output is correct
15 Correct 3 ms 21084 KB Output is correct
16 Correct 3 ms 21084 KB Output is correct
17 Correct 5 ms 21084 KB Output is correct
18 Correct 4 ms 21084 KB Output is correct
19 Correct 4 ms 21084 KB Output is correct
20 Correct 4 ms 21084 KB Output is correct
21 Correct 4 ms 21084 KB Output is correct
22 Correct 4 ms 21084 KB Output is correct
23 Correct 6 ms 21084 KB Output is correct
24 Correct 5 ms 21084 KB Output is correct
25 Correct 4 ms 21084 KB Output is correct
26 Correct 4 ms 21084 KB Output is correct
27 Correct 4 ms 21084 KB Output is correct
28 Correct 4 ms 21084 KB Output is correct
29 Correct 4 ms 21084 KB Output is correct
30 Correct 4 ms 21084 KB Output is correct
31 Correct 4 ms 21084 KB Output is correct
32 Correct 4 ms 21084 KB Output is correct
33 Correct 359 ms 25588 KB Output is correct
34 Correct 101 ms 25164 KB Output is correct
35 Correct 330 ms 24796 KB Output is correct
36 Correct 554 ms 28316 KB Output is correct
37 Correct 14 ms 22876 KB Output is correct
38 Correct 616 ms 29192 KB Output is correct
39 Correct 578 ms 28988 KB Output is correct
40 Correct 574 ms 29172 KB Output is correct
41 Correct 584 ms 29084 KB Output is correct
42 Correct 544 ms 28996 KB Output is correct
43 Correct 569 ms 29044 KB Output is correct
44 Correct 521 ms 29176 KB Output is correct
45 Correct 543 ms 28988 KB Output is correct
46 Correct 556 ms 29196 KB Output is correct
47 Correct 550 ms 29200 KB Output is correct
48 Correct 169 ms 26780 KB Output is correct
49 Correct 185 ms 28140 KB Output is correct
50 Correct 71 ms 22812 KB Output is correct
51 Correct 75 ms 23768 KB Output is correct
52 Correct 30 ms 22364 KB Output is correct
53 Correct 235 ms 27472 KB Output is correct
54 Correct 183 ms 24156 KB Output is correct
55 Correct 480 ms 26952 KB Output is correct
56 Correct 302 ms 25084 KB Output is correct
57 Correct 377 ms 27540 KB Output is correct
58 Correct 23 ms 23764 KB Output is correct
59 Correct 70 ms 23780 KB Output is correct
60 Correct 146 ms 27476 KB Output is correct
61 Correct 162 ms 27704 KB Output is correct
62 Correct 93 ms 26064 KB Output is correct
63 Correct 44 ms 27120 KB Output is correct
64 Correct 44 ms 28508 KB Output is correct
65 Correct 47 ms 32600 KB Output is correct
66 Correct 83 ms 24400 KB Output is correct
67 Correct 66 ms 29328 KB Output is correct
68 Correct 138 ms 32860 KB Output is correct
69 Correct 36 ms 22380 KB Output is correct
70 Correct 11 ms 21340 KB Output is correct
71 Correct 63 ms 26892 KB Output is correct
72 Correct 85 ms 31568 KB Output is correct
73 Correct 242 ms 36176 KB Output is correct
74 Correct 277 ms 32596 KB Output is correct
75 Correct 164 ms 35988 KB Output is correct
76 Correct 165 ms 34644 KB Output is correct
77 Correct 275 ms 32796 KB Output is correct