Submission #952831

# Submission time Handle Problem Language Result Execution time Memory
952831 2024-03-25T01:54:06 Z gaga999 Mountains and Valleys (CCO20_day1problem3) C++17
3 / 25
27 ms 92804 KB
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx,avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define lowbit(x) ((x) & -(x))
#define ml(a, b) ((1ll * (a) * (b)) % M)
#define tml(a, b) (a) = ((1ll * (a) * (b)) % M)
#define ad(a, b) ((0ll + (a) + (b)) % M)
#define tad(a, b) (a) = ((0ll + (a) + (b)) % M)
#define mi(a, b) ((0ll + M + (a) - (b)) % M)
#define tmi(a, b) (a) = ((0ll + M + (a) - (b)) % M)
#define tmin(a, b) (a) = min((a), (b))
#define tmax(a, b) (a) = max((a), (b))
#define iter(a) (a).begin(), (a).end()
#define riter(a) (a).rbegin(), (a).rend()
#define init(a, b) memset((a), (b), sizeof(a))
#define cpy(a, b) memcpy((a), (b), sizeof(a))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define size(x) (int)x.size()
#define pb emplace_back
#define mpr make_pair
#define ls(i) ((i) << 1)
#define rs(i) ((i) << 1 | 1)
#define INF 0x3f3f3f3f
#define NIF 0xc0c0c0c0
#define eps 1e-9
#define F first
#define S second
#define int long long
#define AC cin.tie(0)->sync_with_stdio(0)
using namespace std;
typedef long long llt;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<llt, llt> pll;
typedef complex<double> cd;
// const int M = 998244353;

// random_device rm;
// mt19937 rg(rm());
// default_random_engine rg(rm());
// uniform_int_distribution<int> rd(INT_MIN, INT_MAX);
// uniform_real_distribution<double> rd(0, M_PI);

void db() { cerr << "\n"; }
template <class T, class... U>
void db(T a, U... b) { cerr << a << " ", db(b...); }

inline char gc()
{
    const static int SZ = 1 << 16;
    static char buf[SZ], *p1, *p2;
    if (p1 == p2 && (p2 = buf + fread(p1 = buf, 1, SZ, stdin), p1 == p2))
        return -1;
    return *p1++;
}
void rd() {}
template <typename T, typename... U>
void rd(T &x, U &...y)
{
    x = 0;
    bool f = 0;
    char c = gc();
    while (!isdigit(c))
        f ^= !(c ^ 45), c = gc();
    while (isdigit(c))
        x = (x << 1) + (x << 3) + (c ^ 48), c = gc();
    f && (x = -x), rd(y...);
}

template <typename T>
void prt(T x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        prt(x / 10);
    putchar((x % 10) ^ 48);
}

const int N = 5e5 + 5, M = 2e6 + 6;
vector<int> eg[N];
int xr[M], yr[M], wr[M], a1[M];
int in[N], ou[N], ct, ac[20][N];
int mx[N], sc[N], th[N], dp[N], ans;
vector<int> q1[N], q2[N];

void dfs(int u, int fa)
{
    in[u] = ++ct;
    for (int v : eg[u])
    {
        if (v == fa)
            continue;
        ac[0][v] = u, dp[v] = dp[u] + 1;
        for (int i = 1; i < 20; i++)
            ac[i][v] = ac[i - 1][ac[i - 1][v]];
        dfs(v, u);
        tmax(th[u], mx[v] + 1);
        if (th[u] > sc[u])
            swap(th[u], sc[u]);
        if (sc[u] > mx[u])
            swap(mx[u], sc[u]);
    }
    tmax(ans, mx[u] + sc[u]), ou[u] = ct;
}

inline bool isa(int a, int p)
{
    return in[a] <= in[p] && ou[p] <= ou[a];
}

inline int lca(int u, int v)
{
    if (isa(u, v))
        return u;
    for (int i = 19; i >= 0; i--)
        if (ac[i][u] && !isa(ac[i][u], v))
            u = ac[i][u];
    return ac[0][u];
}

inline int jp(int a, int u)
{
    for (int i = 19; i >= 0; i--)
        if (ac[i][u] && !isa(ac[i][u], a))
            u = ac[i][u];
    return u;
}

struct P
{
    int vp, vn, ans;
    P() { vp = vn = ans = -INF; }
    inline void ini() { vp = vn = ans = -INF; }
    P operator+(const P &rh)
    {
        P res;
        res.ans = max({ans, vp + rh.vn, rh.ans});
        res.vp = max(vp, rh.vp);
        res.vn = max(vn, rh.vn);
        return res;
    }
} tr[N << 2], vv;

void cg(int l, int r, int id, int p, int v)
{
    if (l == r)
    {
        tr[id].vp = v + p;
        tr[id].vn = v - p;
        tr[id].ans = -INF;
        return;
    }
    int m = (l + r) >> 1;
    if (p <= m)
        cg(l, m, ls(id), p, v);
    else
        cg(m + 1, r, rs(id), p, v);
    tr[id] = tr[ls(id)] + tr[rs(id)];
}

void qy(int l, int r, int id, int ql, int qr)
{
    assert(ql <= qr);
    if (ql <= l && r <= qr)
    {
        vv = vv + tr[id];
        return;
    }
    int m = (l + r) >> 1;
    if (ql <= m)
        qy(l, m, ls(id), ql, qr);
    if (qr > m)
        qy(m + 1, r, rs(id), ql, qr);
}

void slv(int u, int fa)
{
    for (int v : eg[u])
    {
        if (v == fa)
            continue;
        cg(1, ct, 1, dp[u], mx[v] + 1 == mx[u] ? sc[u] : mx[u]);
        slv(v, u);
    }
    cg(1, ct, 1, dp[u], mx[u]);
    for (int i : q1[u])
    {
        vv.ini(), qy(1, ct, 1, dp[xr[i]] + 1, dp[u]);
        tmin(ans, wr[i] - (vv.ans + dp[u] - dp[xr[i]]));
        // vv.ini(), qy(1, ct, 1, dp[xr[i]] + 1, dp[u]);
        int tp = vv.vn + dp[u] + dp[xr[i]];
        vv.ini(), qy(1, ct, 1, 1, dp[xr[i]]);
        tmin(ans, wr[i] - (tp + vv.vn));
    }
    for (int i : q2[u])
    {
        int p = lca(xr[i], yr[i]);
        // vv.ini(), qy(1, ct, 1, dp[p], dp[u]);
        // db(dp[p], p, u, xr[i], yr[i], ans);
        vv.ini(), qy(1, ct, 1, dp[p] + 1, dp[u]);
        tmin(ans, wr[i] - (vv.ans + dp[xr[i]] + dp[yr[i]] - (dp[p] << 1)));
        if (xr[i] == u)
            a1[i] = vv.vn;
        else
            tmin(ans, wr[i] - (vv.vn + a1[i] + dp[xr[i]] + dp[yr[i]]));
        int tp = dp[xr[i]] + dp[yr[i]] + vv.vn;
        int v1 = mx[jp(p, xr[i])] + 1, v2 = mx[jp(p, yr[i])] + 1;
        if (v1 < v2)
            swap(v1, v2);
        if (v1 != mx[p])
            tmin(ans, wr[i] - (tp - dp[p] + mx[p]));
        else if (v2 != sc[p])
            tmin(ans, wr[i] - (tp - dp[p] + sc[p]));
        else
            tmin(ans, wr[i] - (tp - dp[p] + th[p]));
        if (ac[0][p])
        {
            vv.ini(), qy(1, ct, 1, 1, dp[p] - 1);
            tmin(ans, wr[i] - (tp + vv.vn));
        }
    }
}

signed main()
{
    int n, m;
    rd(n, m);
    // assert(n == 6);
    for (int i = 0; i < m; i++)
    {
        rd(xr[i], yr[i], wr[i]), xr[i]++, yr[i]++;
        if (wr[i] == 1)
            eg[xr[i]].pb(yr[i]), eg[yr[i]].pb(xr[i]);
    }
    int rt = 1;
    dp[rt] = 1, dfs(rt, -1), ans = ((n - 1) << 1) - ans;
    int pans = ans;
    for (int i = 0; i < m; i++)
    {
        assert(xr[i] != yr[i]);
        if (wr[i] == 1)
            continue;
        wr[i] += (n - 2) << 1;
        if (in[xr[i]] > in[yr[i]])
            swap(xr[i], yr[i]);
        if (isa(xr[i], yr[i]))
            q1[yr[i]].pb(i);
        else
            q2[xr[i]].pb(i), q2[yr[i]].pb(i);
    }
    slv(rt, -1);
    assert(ans <= pans);
    prt(ans), putchar('\n');
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 89180 KB Output is correct
2 Correct 17 ms 89180 KB Output is correct
3 Correct 16 ms 89096 KB Output is correct
4 Correct 16 ms 89180 KB Output is correct
5 Correct 16 ms 89176 KB Output is correct
6 Correct 16 ms 89176 KB Output is correct
7 Correct 17 ms 89176 KB Output is correct
8 Correct 17 ms 89180 KB Output is correct
9 Correct 16 ms 91228 KB Output is correct
10 Correct 16 ms 89180 KB Output is correct
11 Correct 17 ms 89180 KB Output is correct
12 Correct 16 ms 89180 KB Output is correct
13 Correct 17 ms 89216 KB Output is correct
14 Correct 16 ms 89180 KB Output is correct
15 Correct 17 ms 91288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 89180 KB Output is correct
2 Correct 17 ms 89180 KB Output is correct
3 Correct 16 ms 89096 KB Output is correct
4 Correct 16 ms 89180 KB Output is correct
5 Correct 16 ms 89176 KB Output is correct
6 Correct 16 ms 89176 KB Output is correct
7 Correct 17 ms 89176 KB Output is correct
8 Correct 17 ms 89180 KB Output is correct
9 Correct 16 ms 91228 KB Output is correct
10 Correct 16 ms 89180 KB Output is correct
11 Correct 17 ms 89180 KB Output is correct
12 Correct 16 ms 89180 KB Output is correct
13 Correct 17 ms 89216 KB Output is correct
14 Correct 16 ms 89180 KB Output is correct
15 Correct 17 ms 91288 KB Output is correct
16 Correct 16 ms 89180 KB Output is correct
17 Correct 17 ms 91228 KB Output is correct
18 Incorrect 17 ms 89180 KB Output isn't correct
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 23 ms 90968 KB Output is correct
2 Correct 22 ms 90972 KB Output is correct
3 Correct 27 ms 90972 KB Output is correct
4 Correct 24 ms 90712 KB Output is correct
5 Correct 25 ms 90716 KB Output is correct
6 Correct 21 ms 90460 KB Output is correct
7 Correct 22 ms 91008 KB Output is correct
8 Correct 24 ms 90972 KB Output is correct
9 Correct 23 ms 90976 KB Output is correct
10 Correct 23 ms 90712 KB Output is correct
11 Correct 24 ms 90972 KB Output is correct
12 Correct 23 ms 90716 KB Output is correct
13 Correct 24 ms 90972 KB Output is correct
14 Correct 24 ms 92804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 89180 KB Output is correct
2 Correct 17 ms 89180 KB Output is correct
3 Correct 16 ms 89096 KB Output is correct
4 Correct 16 ms 89180 KB Output is correct
5 Correct 16 ms 89176 KB Output is correct
6 Correct 16 ms 89176 KB Output is correct
7 Correct 17 ms 89176 KB Output is correct
8 Correct 17 ms 89180 KB Output is correct
9 Correct 16 ms 91228 KB Output is correct
10 Correct 16 ms 89180 KB Output is correct
11 Correct 17 ms 89180 KB Output is correct
12 Correct 16 ms 89180 KB Output is correct
13 Correct 17 ms 89216 KB Output is correct
14 Correct 16 ms 89180 KB Output is correct
15 Correct 17 ms 91288 KB Output is correct
16 Correct 16 ms 89180 KB Output is correct
17 Correct 17 ms 91228 KB Output is correct
18 Incorrect 17 ms 89180 KB Output isn't correct
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 89180 KB Output is correct
2 Correct 17 ms 89180 KB Output is correct
3 Correct 16 ms 89096 KB Output is correct
4 Correct 16 ms 89180 KB Output is correct
5 Correct 16 ms 89176 KB Output is correct
6 Correct 16 ms 89176 KB Output is correct
7 Correct 17 ms 89176 KB Output is correct
8 Correct 17 ms 89180 KB Output is correct
9 Correct 16 ms 91228 KB Output is correct
10 Correct 16 ms 89180 KB Output is correct
11 Correct 17 ms 89180 KB Output is correct
12 Correct 16 ms 89180 KB Output is correct
13 Correct 17 ms 89216 KB Output is correct
14 Correct 16 ms 89180 KB Output is correct
15 Correct 17 ms 91288 KB Output is correct
16 Correct 16 ms 89180 KB Output is correct
17 Correct 17 ms 91228 KB Output is correct
18 Incorrect 17 ms 89180 KB Output isn't correct
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 89180 KB Output is correct
2 Correct 17 ms 89180 KB Output is correct
3 Correct 16 ms 89096 KB Output is correct
4 Correct 16 ms 89180 KB Output is correct
5 Correct 16 ms 89176 KB Output is correct
6 Correct 16 ms 89176 KB Output is correct
7 Correct 17 ms 89176 KB Output is correct
8 Correct 17 ms 89180 KB Output is correct
9 Correct 16 ms 91228 KB Output is correct
10 Correct 16 ms 89180 KB Output is correct
11 Correct 17 ms 89180 KB Output is correct
12 Correct 16 ms 89180 KB Output is correct
13 Correct 17 ms 89216 KB Output is correct
14 Correct 16 ms 89180 KB Output is correct
15 Correct 17 ms 91288 KB Output is correct
16 Correct 16 ms 89180 KB Output is correct
17 Correct 17 ms 91228 KB Output is correct
18 Incorrect 17 ms 89180 KB Output isn't correct
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 89180 KB Output is correct
2 Correct 17 ms 89180 KB Output is correct
3 Correct 16 ms 89096 KB Output is correct
4 Correct 16 ms 89180 KB Output is correct
5 Correct 16 ms 89176 KB Output is correct
6 Correct 16 ms 89176 KB Output is correct
7 Correct 17 ms 89176 KB Output is correct
8 Correct 17 ms 89180 KB Output is correct
9 Correct 16 ms 91228 KB Output is correct
10 Correct 16 ms 89180 KB Output is correct
11 Correct 17 ms 89180 KB Output is correct
12 Correct 16 ms 89180 KB Output is correct
13 Correct 17 ms 89216 KB Output is correct
14 Correct 16 ms 89180 KB Output is correct
15 Correct 17 ms 91288 KB Output is correct
16 Correct 16 ms 89180 KB Output is correct
17 Correct 17 ms 91228 KB Output is correct
18 Incorrect 17 ms 89180 KB Output isn't correct
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 89180 KB Output is correct
2 Correct 17 ms 89180 KB Output is correct
3 Correct 16 ms 89096 KB Output is correct
4 Correct 16 ms 89180 KB Output is correct
5 Correct 16 ms 89176 KB Output is correct
6 Correct 16 ms 89176 KB Output is correct
7 Correct 17 ms 89176 KB Output is correct
8 Correct 17 ms 89180 KB Output is correct
9 Correct 16 ms 91228 KB Output is correct
10 Correct 16 ms 89180 KB Output is correct
11 Correct 17 ms 89180 KB Output is correct
12 Correct 16 ms 89180 KB Output is correct
13 Correct 17 ms 89216 KB Output is correct
14 Correct 16 ms 89180 KB Output is correct
15 Correct 17 ms 91288 KB Output is correct
16 Correct 16 ms 89180 KB Output is correct
17 Correct 17 ms 91228 KB Output is correct
18 Incorrect 17 ms 89180 KB Output isn't correct
19 Halted 0 ms 0 KB -