提交记录 9940


用户 题目 状态 得分 用时 内存 语言 代码长度
realSpongeBob noip18f. 【NOIP2018】保卫王国 Accepted 100 350.786 ms 37812 KB C++11 4.78 KB
提交时间 评测时间
2019-07-24 16:04:53 2020-08-01 01:59:02
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#define int long long

const int MAXN = 1e5 + 10, MAXM = 1e5 + 10, INF = 2e9;

int n, m, sum, alarm, cnt;
int a[MAXN], head[MAXN], sz[MAXN], fa[MAXN], son[MAXN], dep[MAXN], dfn[MAXN], bel[MAXN], top[MAXN], est[MAXN];
int ldp[MAXN][2], dp[MAXN][2];

struct Edge {
    int link, to;
} edge[MAXN << 1];

struct Matrix {
    int data[2][2];
    Matrix() {
        for (int i = 0; i < 2; ++i)
            for (int j = 0; j < 2; ++j)
                data[i][j] = -INF;
    }
    inline int *operator[] (int x) {
        return data[x];
    }
    inline void fill(int val) {
        for (int i = 0; i < 2; ++i)
            for (int j = 0; j < 2; ++j)
                data[i][j] = val;
        return;
    }
    friend inline Matrix operator* (Matrix A, Matrix B) {
        Matrix C;
        for (int i = 0; i < 2; ++i)
            for (int k = 0; k < 2; ++k)
                for (int j = 0; j < 2; ++j)
                    C[i][j] = std::max(C[i][j], A[i][k] + B[k][j]);
        return C;
    }
};
Matrix val[MAXN];

inline Matrix Wi(int a, int b, int c, int d) {
    Matrix A;
    A[0][0] = a, A[0][1] = b, A[1][0] = c, A[1][1] = d;
    return A;
}

template <int N>
struct SegmentTree {
    static const int M = N << 2;
    Matrix data[M];
    #define lc ((x) << 1)
    #define rc ((x) << 1 | 1)
    void build(int x, int L, int R) {
        if (L == R) {
            int u = bel[L];
            data[x] = val[u] = Wi(ldp[u][0], ldp[u][0], ldp[u][1], -INF);
            return;
        }
        int mid = L + R >> 1;
        build(lc, L, mid), build(rc, mid + 1, R), data[x] = data[lc] * data[rc];
        return;
    }
    void update(int x, int L, int R, int pos) {
        if (L == R) return (void) (data[x] = val[bel[pos]]);
        int mid = L + R >> 1;
        pos <= mid ? update(lc, L, mid, pos) : update(rc, mid + 1, R, pos);
        data[x] = data[lc] * data[rc];
        return;
    }
    Matrix query(int x, int L, int R, int ql, int qr) {
        if (L >= ql && R <= qr) return data[x];
        int mid = L + R >> 1;
        if (ql > mid) return query(rc, mid + 1, R, ql, qr);
        else if (qr <= mid) return query(lc, L, mid, ql, qr);
        else return query(lc, L, mid, ql, qr) * query(rc, mid + 1, R, ql, qr);
    }
    #undef lc
    #undef rc
};
SegmentTree <MAXN> T;

inline void add(int u, int v) {
    edge[++sum] = (Edge) { v, head[u] }, head[u] = sum;
    return;
}

void dfs1(int u, int fafa) {
    dep[u] = dep[fafa] + 1, sz[u] = 1, fa[u] = fafa;
    for (int i = head[u]; i; i = edge[i].to) {
        int v = edge[i].link;
        if (v == fafa) continue;
        dfs1(v, u), sz[u] += sz[v];
        if (sz[v] > sz[son[u]]) son[u] = v;
    }
    return;
}

void dfs2(int u, int tp) {
    dfn[u] = est[tp] = ++alarm, bel[alarm] = u, top[u] = tp;
    if (!son[u]) return; dfs2(son[u], tp);
    for (int i = head[u]; i; i = edge[i].to) {
        int v = edge[i].link;
        if (v == fa[u] || v == son[u]) continue;
        dfs2(v, v);
    }
    return;
}

void dfs3(int u) {
    for (int i = head[u]; i; i = edge[i].to) {
        int v = edge[i].link;
        if (v == fa[u] || v == son[u]) continue;
        dfs3(v);
        ldp[u][0] += std::max(dp[v][0], dp[v][1]), ldp[u][1] += dp[v][0];
    }
    dp[u][0] += ldp[u][0], dp[u][1] += ldp[u][1];
    if (!son[u]) return;
    dfs3(son[u]);
    dp[u][0] += std::max(dp[son[u]][0], dp[son[u]][1]), dp[u][1] += dp[son[u]][0];
    return;
}

inline void change(int u, int w) {
    val[u][1][0] += w, a[u] += w;
    while (u) {
        int now = top[u];
        Matrix lst = T.query(1, 1, n, dfn[now], est[now]);
        T.update(1, 1, n, dfn[u]);
        Matrix nxt = T.query(1, 1, n, dfn[now], est[now]);
        u = fa[now];
        val[u][0][0] += std::max(nxt[0][0], nxt[1][0]) - std::max(lst[0][0], lst[1][0]);
        val[u][0][1] = val[u][0][0];
        val[u][1][0] += nxt[0][0] - lst[0][0];
    }
    return;
}

inline int query() {
    Matrix ans = T.query(1, 1, n, dfn[1], est[1]);
    return std::max(ans[0][0], ans[1][0]);
}

signed main() {
    scanf("%lld%lld%*s", &n, &m);
    for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]), ldp[i][1] = a[i], cnt += a[i];
    for (int i = 1, u, v; i < n; ++i) scanf("%lld%lld", &u, &v), add(u, v), add(v, u);
    dfs1(1, 0), dfs2(1, 1), dfs3(1), T.build(1, 1, n);
    for (int a, b, x, y; m--;) {
        scanf("%lld%lld%lld%lld", &a, &x, &b, &y);
        if ((fa[a] == b || fa[b] == a) && !x && !y) {
            puts("-1");
            continue;
        }
        change(a, x ? -INF : INF), change(b, y ? -INF : INF);
        printf("%lld\n", cnt - query() + (x ? 0 : INF) + (y ? 0 : INF));
        change(a, x ? INF : -INF), change(b, y ? INF : -INF);
    }
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #11.954 ms15 MB + 336 KBAcceptedScore: 4

Testcase #21.96 ms15 MB + 336 KBAcceptedScore: 4

Testcase #31.956 ms15 MB + 336 KBAcceptedScore: 4

Testcase #41.96 ms15 MB + 336 KBAcceptedScore: 4

Testcase #52.02 ms15 MB + 360 KBAcceptedScore: 4

Testcase #62.078 ms15 MB + 360 KBAcceptedScore: 4

Testcase #72.15 ms15 MB + 356 KBAcceptedScore: 4

Testcase #84.308 ms15 MB + 776 KBAcceptedScore: 4

Testcase #94.158 ms15 MB + 776 KBAcceptedScore: 4

Testcase #106.023 ms15 MB + 708 KBAcceptedScore: 4

Testcase #115.673 ms15 MB + 708 KBAcceptedScore: 4

Testcase #12156.908 ms36 MB + 948 KBAcceptedScore: 4

Testcase #13156.36 ms36 MB + 948 KBAcceptedScore: 4

Testcase #14132.965 ms36 MB + 752 KBAcceptedScore: 4

Testcase #15133.699 ms36 MB + 756 KBAcceptedScore: 4

Testcase #16133.508 ms36 MB + 756 KBAcceptedScore: 4

Testcase #17180.143 ms36 MB + 948 KBAcceptedScore: 4

Testcase #18317.768 ms30 MB + 56 KBAcceptedScore: 4

Testcase #19314.11 ms30 MB + 60 KBAcceptedScore: 4

Testcase #20263.362 ms33 MB + 432 KBAcceptedScore: 4

Testcase #21282.576 ms33 MB + 432 KBAcceptedScore: 4

Testcase #22227.375 ms33 MB + 236 KBAcceptedScore: 4

Testcase #23343.766 ms33 MB + 428 KBAcceptedScore: 4

Testcase #24334.816 ms33 MB + 432 KBAcceptedScore: 4

Testcase #25350.786 ms33 MB + 444 KBAcceptedScore: 4


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:35:27 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠