Submission #1286595

#TimeUsernameProblemLanguageResultExecution timeMemory
1286595thdh__Cats or Dogs (JOI18_catdog)C++20
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
#define int ll

using namespace std;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());

/*
Competitive Programming notes that I need to study & fix my dumbass self:

1. Coding:
- Always be sure to check the memory of arrays (maybe use vectors), for loops
- Always try to maximize the memory if possible, even if you are going for subtasks
- Do not exploit #define int long long, it will kill you

2. Stress: 
- Always try generating big testcases and try if they run

3. Time management:
- Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked
- Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time

Time management schedule:
Offline / LAH days (4 problems - 3h):
15' thinking of solution / idea
1. no idea: skip
2. yes idea: continue thinking for <= 15'

+ implementing: <= 20'
+ brute-force: <= 5'
+ test generator: <= 5'

I hate offline because I am dumb
*/

typedef pair<int, int> ii;
const int N = 1e5+5;
const int B = 750;
const int mod = 1e9+7;
const int inf = 1e9;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;} 

struct Matrix {
    int a[2][2];

    Matrix() {
        a[0][0] = a[1][1] = 0;
        a[0][1] = a[1][0] = inf;
    }

    static Matrix cat() 
    {
        Matrix a;
        a.a[1][1] = inf;
        return a;
    }

    static Matrix dog() 
    {
        Matrix a;
        a.a[0][0] = inf;
        return a;
    }

    static Matrix rcat() 
    {
        Matrix a;
        a.a[1][1] = -inf;
        return a;
    }

    static Matrix rdog() 
    {
        Matrix a;
        a.a[0][0] = -inf;
        return a;
    }
};

Matrix merge(const Matrix &a, const Matrix &b) {
    Matrix c;
    for (int i=0;i<2;i++) for (int v=0;v<2;v++) c.a[i][v] = inf;
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
            if (a.a[i][j] >= inf) continue;
            for (int u = 0; u < 2; ++u) {
                for (int v = 0; v < 2; ++v) {
                    if (b.a[u][v] >= inf) continue;
                    c.a[i][v] = min(c.a[i][v], a.a[i][j] + b.a[u][v] + (j != u));
                }
            }
        }
    }
    return c;
}

struct Segtree {
    int n;
    vector<Matrix> st;

    void init(int _) 
    {
        n = _;
        st.assign(4*_ + 5, Matrix());
    }

    void Set(int id, int l, int r, int i, int tp) 
    {
        if (l == r) 
        {
            if (tp == 0) st[id] = Matrix();
            else if (tp == 1) st[id] = Matrix::cat();
            else st[id] = Matrix::dog();
            return;
        }
        int mid = l+r>>1;
        if (i <= mid) Set(id*2, l, mid, i, tp);
        else Set(id*2+1, mid+1, r, i, tp);
        st[id] = merge(st[id*2], st[id*2+1]);
    }

    void update(int id, int l, int r, int i, ii a) 
    {
        if (l == r) 
        {
            st[id].a[0][0] += a.fi, st[id].a[1][1] += a.se;
            return;
        }
        int mid = l+r>>1;
        if (i <= mid) update(id*2, l, mid, i, a);
        else update(id*2+1, mid+1, r, i, a);
        st[id] = merge(st[id*2], st[id*2+1]);
    }

    void upd(int i, ii v) 
    {
        update(1, 1, n, i, v);
    }

    void set(int i, int tp) 
    {
        Set(1, 1, n, i, tp);
    }
} st[N];

int n;
vector<int> adj[N];

int val[N];
int sz[N], par[N], heavy[N], head[N], len[N];
int tin[N], tout[N], timer = 0;

void reset() 
{
    timer = 0;
    for (int i = 1; i <= n; i++) 
    {
        val[i] = -1;
        sz[i] = par[i] = heavy[i] = head[i] = tin[i] = tout[i] = 0;
        adj[i].clear();
    }
}

void predfs(int u, int p) 
{
    sz[u] = 1;
    par[u] = p;
    for (auto v : adj[u]) 
    {
        if (v == p) continue;
        predfs(v, u);
        sz[u] += sz[v];
        if (sz[v] > sz[heavy[u]]) heavy[u] = v;
    }
}

void decompose(int u, int p) 
{
    len[p]++;
    head[u] = p;
    tin[u] = ++timer;
    if (heavy[u]) decompose(heavy[u], p);
    for (auto v : adj[u]) 
    {
        if (v == par[u] || v == heavy[u]) continue;
        decompose(v, v);
    }
    tout[u] = timer;
}

int id(int u) 
{
    return tin[u] - tin[head[u]] + 1;
}

ii delta(Matrix prv, Matrix cur) 
{
    ii c, p;
    c.fi = min({cur.a[0][0], cur.a[0][1], cur.a[1][1] + 1, cur.a[1][0] + 1});
    c.se = min({cur.a[1][0], cur.a[1][1], cur.a[0][0] + 1, cur.a[0][1] + 1});
    p.fi = min({prv.a[0][0], prv.a[0][1], prv.a[1][1] + 1, prv.a[1][0] + 1});
    p.se = min({prv.a[1][0], prv.a[1][1], prv.a[0][0] + 1, prv.a[0][1] + 1});
    return {c.fi - p.fi, c.se - p.se};
}

void del(int u) 
{
    Matrix prv = st[head[u]].st[1];
    if (val[u] == 1) st[head[u]].upd(id(u), {0, -inf});
    else st[head[u]].upd(id(u), {-inf, 0});
    Matrix cur = st[head[u]].st[1];
    while (true) 
    {
        u = head[u];
        if (!par[u]) break;
        Matrix pprv = st[head[par[u]]].st[1];
        st[head[par[u]]].upd(id(par[u]), delta(prv, cur));
        prv = pprv; cur = st[head[par[u]]].st[1];
        u = par[u];
    }
}

void add(int u) 
{
    Matrix prv = st[head[u]].st[1];
    if (val[u] == 1) st[head[u]].upd(id(u), {0, inf});
    else st[head[u]].upd(id(u), {inf, 0});
    Matrix cur = st[head[u]].st[1];
    while (true) 
    {
        u = head[u];
        if (!par[u]) break;
        Matrix pprv = st[head[par[u]]].st[1];
        st[head[par[u]]].upd(id(par[u]), delta(prv, cur));
        prv = pprv, cur = st[head[par[u]]].st[1];
        u = par[u];
    }
}

int cat(int u) 
{
    assert(val[u] == -1);
    val[u] = 1;
    add(u);
    Matrix res = st[1].st[1];
    return min({res.a[0][0], res.a[0][1], res.a[1][0], res.a[1][1]});
}

int dog(int u) 
{
    assert(val[u] == -1);
    val[u] = 2;
    add(u);
    Matrix res = st[1].st[1];
    return min({res.a[0][0], res.a[0][1], res.a[1][0], res.a[1][1]});
}

int neighbor(int u) 
{
    assert(val[u] != -1);
    del(u);
    val[u] = -1;
    Matrix res = st[1].st[1];
    return min({res.a[0][0], res.a[0][1], res.a[1][0], res.a[1][1]});
}

void initialize(int _, vector<int> A, vector<int> B) {
    n = _;
    reset();
    for (int i = 0; i < n-1; i++) 
    {
        adj[A[i]].pb(B[i]); adj[B[i]].pb(A[i]);
    }
    predfs(1, 0);
    decompose(1, 1);
    // for (int i = 1; i <= n; i++) cout<<head[i]<<" ";
    // cout<<endl;
    for (int i = 1; i <= n; i++) 
    {
        if (head[i] == i) 
        {
            st[i].init(len[i]);
        }
    }
}

/*
Go through the mistakes you usually make and revise your code, for god's sake...
*/

// signed main()
// {
// 	bruh
// 	int n = 5;
//     vector<int> A = {1, 2, 2, 4};
//     vector<int> B = {2, 3, 4, 5};
//     initialize(n, A, B);
//     cout<<cat(3)<<endl;
//     cout<<dog(5)<<endl;
//     cout<<cat(2)<<endl;
//     cout<<dog(1)<<endl;
//     cout<<neighbor(2)<<endl;
// }

Compilation message (stderr)

/usr/bin/ld: /tmp/ccvbQmRw.o: in function `main':
grader.cpp:(.text.startup+0x1d4): undefined reference to `initialize(int, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >)'
/usr/bin/ld: grader.cpp:(.text.startup+0x211): undefined reference to `neighbor(int)'
/usr/bin/ld: grader.cpp:(.text.startup+0x250): undefined reference to `dog(int)'
/usr/bin/ld: grader.cpp:(.text.startup+0x261): undefined reference to `cat(int)'
collect2: error: ld returned 1 exit status