QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#190523#6735. Treeucup-team004WA 1375ms72652kbC++205.7kb2023-09-29 02:14:582023-09-29 02:14:59

Judging History

你现在查看的是最新测评结果

  • [2023-09-29 02:14:59]
  • 评测
  • 测评结果:WA
  • 用时:1375ms
  • 内存:72652kb
  • [2023-09-29 02:14:58]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr i64 inf = 1E18;

struct Info {
    i64 ans = 0;
    i64 len = -inf;
    i64 vl = -inf;
    i64 vr = -inf;
};

constexpr int N = 1 << 19;

Info info[N];

Info operator+(const Info &a, const Info &b) {
    Info c;
    c.ans = std::max({a.ans, b.ans, a.vr + b.vl});
    c.len = std::max(-inf, a.len + b.len);
    c.vl = std::max(a.vl, a.len + b.vl);
    c.vr = std::max(b.vr, a.vr + b.len);
    return c;
}

Info query(int p, int l, int r, int x, int y) {
    if (l >= y || r <= x) {
        return {};
    }
    if (l >= x && r <= y) {
        return info[p];
    }
    int m = (l + r) / 2;
    return query(2 * p, l, m, x, y) + query(2 * p + 1, m, r, x, y);
}

void pull(int p) {
    info[p] = info[2 * p] + info[2 * p + 1];
}

void modify(int p, int l, int r, int x, const Info &v) {
    if (r - l == 1) {
        info[p] = v;
        return;
    }
    int m = (l + r) / 2;
    if (x < m) {
        modify(2 * p, l, m, x, v);
    } else {
        modify(2 * p + 1, m, r, x, v);
    }
    pull(p);
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, q;
    std::cin >> n >> q;
    
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    
    std::vector<int> p(n);
    p[0] = -1;
    for (int i = 1; i < n; i++) {
        std::cin >> p[i];
        p[i]--;
    }
    
    std::vector<int> c(n);
    for (int i = 1; i < n; i++) {
        std::cin >> c[i];
    }
    
    std::vector<int> w(n);
    for (int i = 1; i < n; i++) {
        std::cin >> w[i];
    }
    
    std::vector<int> dep(n), siz(n), in(n), top(n), bot(n);
    std::vector<std::vector<int>> adj(n);
    for (int i = 1; i < n; i++) {
        adj[p[i]].push_back(i);
    }
    auto dfs1 = [&](auto self, int x) -> void {
        siz[x] = 1;
        for (auto &y : adj[x]) {
            dep[y] = dep[x] + 1;
            self(self, y);
            siz[x] += siz[y];
            if (siz[y] > siz[adj[x][0]]) {
                std::swap(y, adj[x][0]);
            }
        }
    };
    dfs1(dfs1, 0);
    
    std::vector<std::multiset<std::pair<std::pair<int, int>, i64>>> chain(n);
    std::vector<std::multiset<std::pair<int, i64>>> val(n);
    
    auto get = [&](int x = 0) {
        return query(1, 0, n, in[x], in[bot[x]] + 1);
    };
    
    auto getmax = [&](const auto &s, const auto &v, int cnt = 1) {
        i64 ans = 0;
        auto it = s.upper_bound({v, inf});
        while (cnt-- && it != s.begin()) {
            it--;
            if (it->first == v) {
                ans += it->second;
            }
        }
        return ans;
    };
    
    auto add = [&](int x, int a, int c, i64 v) {
        i64 res = getmax(chain[x], std::make_pair(a, c), 2);
        val[x].extract({a, res});
        chain[x].emplace(std::make_pair(a, c), v);
        res = getmax(chain[x], std::make_pair(a, c), 2);
        if (res) {
            val[x].emplace(a, res);
        }
    };
    
    auto del = [&](int x, int a, int c, i64 v) {
        i64 res = getmax(chain[x], std::make_pair(a, c), 2);
        val[x].extract({a, res});
        chain[x].extract({std::make_pair(a, c), v});
        res = getmax(chain[x], std::make_pair(a, c), 2);
        if (res) {
            val[x].emplace(a, res);
        }
    };
    
    auto insert = [&](int x) {
        if (!x) {
            return;
        }
        int y = p[x];
        auto res = get(x);
        add(y, a[x], c[x], std::max(0LL, res.vl) + w[x]);
        if (res.ans) {
            val[y].emplace(-1, res.ans);
        }
    };
    
    auto erase = [&](int x) {
        if (!x) {
            return;
        }
        int y = p[x];
        auto res = get(x);
        del(y, a[x], c[x], std::max(0LL, res.vl) + w[x]);
        if (res.ans) {
            val[y].extract({-1, res.ans});
        }
    };
    
    auto update = [&](int x) {
        Info v;
        i64 res = std::max(getmax(val[x], a[x]), getmax(val[x], -1));
        if (!adj[x].empty()) {
            int y = adj[x][0];
            if (a[x] == a[y]) {
                v.vr = getmax(chain[x], std::make_pair(a[y], c[y]));
            }
            if (a[y] == a[x] && c[y] == c[x]) {
                v.len = w[x];
            }
        }
        if (top[x] != x) {
            int y = p[x];
            if (a[y] == a[x]) {
                v.vl = w[x] + getmax(chain[x], std::make_pair(a[x], c[x]));
            }
        }
        v.ans = std::max({res, v.vl, v.vr});
        modify(1, 0, n, in[x], v);
    };
    
    int cur = 0;
    auto dfs2 = [&](auto self, int x) -> void {
        in[x] = cur++;
        for (auto y : adj[x]) {
            top[y] = y == adj[x][0] ? top[x] : y;
            self(self, y);
            if (y != adj[x][0]) {
                insert(y);
            }
        }
        bot[x] = adj[x].empty() ? x : bot[adj[x][0]];
        update(x);
    };
    dfs2(dfs2, 0);
    
    auto change = [&](int x, int y) {
        for (int i = x; i != -1; i = p[top[i]]) {
            erase(top[i]);
        }
        a[x] = y;
        if (top[x] != x) {
            update(p[x]);
        }
        if (!adj[x].empty()) {
            update(adj[x][0]);
        }
        for (int i = x; i != -1; i = p[top[i]]) {
            update(i);
            insert(top[i]);
        }
    };
    
    auto answer = [&]() {
        std::cout << get().ans << "\n";
    };
    
    answer();
    while (q--) {
        int x, y;
        std::cin >> x >> y;
        x--;
        
        change(x, y);
        
        answer();
    }
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3692kb

input:

5 5
5 4 3 4 5
1 2 3 1
2 2 2 2
4 9 2 6
2 5
4 5
5 4
3 5
2 1

output:

6
10
10
4
15
2

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 0ms
memory: 3676kb

input:

13 21
1 2 1 2 4 4 2 1 4 2 3 6 1
1 1 2 3 2 6 7 8 9 10 8 8
2 13 1 1 1 2 2 1 2 1 2 1
472868230 94771637 209247951 483753517 822923242 938504499 413445582 328056598 487969741 355938152 902390974 28610378
2 4
7 4
10 1
8 4
2 3
5 2
11 4
9 3
6 2
6 1
4 1
6 1
2 3
8 2
5 2
6 2
8 4
8 2
1 4
11 4
12 2

output:

209247951
822923242
938504499
938504499
1351950081
1351950081
1351950081
1351950081
1351950081
413445582
413445582
413445582
413445582
413445582
94771637
94771637
94771637
413445582
94771637
0
0
902390974

result:

ok 22 numbers

Test #3:

score: 0
Accepted
time: 1306ms
memory: 72596kb

input:

200000 199999
1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1...

output:

1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
1999164873
199...

result:

ok 200000 numbers

Test #4:

score: 0
Accepted
time: 938ms
memory: 64416kb

input:

199999 199998
1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1...

output:

1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
1999993380
199...

result:

ok 199999 numbers

Test #5:

score: 0
Accepted
time: 1312ms
memory: 72652kb

input:

200000 199999
1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1...

output:

1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
1997943637
199...

result:

ok 200000 numbers

Test #6:

score: 0
Accepted
time: 924ms
memory: 63532kb

input:

199999 200000
1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1...

output:

1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
1999990769
199...

result:

ok 200001 numbers

Test #7:

score: 0
Accepted
time: 1142ms
memory: 68308kb

input:

199999 200000
1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1...

output:

1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
1999694869
199...

result:

ok 200001 numbers

Test #8:

score: 0
Accepted
time: 919ms
memory: 64100kb

input:

200000 199998
1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1...

output:

1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
1999975784
199...

result:

ok 199999 numbers

Test #9:

score: 0
Accepted
time: 903ms
memory: 64068kb

input:

200000 199999
1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1...

output:

1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
1999979021
199...

result:

ok 200000 numbers

Test #10:

score: 0
Accepted
time: 1239ms
memory: 68900kb

input:

199998 200000
1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1...

output:

1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
1999727070
199...

result:

ok 200001 numbers

Test #11:

score: 0
Accepted
time: 898ms
memory: 64588kb

input:

199999 199998
1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1...

output:

1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
1999998737
199...

result:

ok 199999 numbers

Test #12:

score: 0
Accepted
time: 1293ms
memory: 71688kb

input:

199999 199998
1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1...

output:

1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
1998690305
199...

result:

ok 199999 numbers

Test #13:

score: -100
Wrong Answer
time: 1375ms
memory: 64560kb

input:

200000 200000
1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1064 1...

output:

7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
7347361948
734...

result:

wrong answer 1st numbers differ - expected: '10779640138', found: '7347361948'