QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#190521#6735. Treeucup-team004WA 1620ms89020kbC++205.5kb2023-09-29 01:59:492023-09-29 01:59:50

Judging History

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

  • [2023-09-29 01:59:50]
  • 评测
  • 测评结果:WA
  • 用时:1620ms
  • 内存:89020kb
  • [2023-09-29 01:59:49]
  • 提交

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);
    std::vector<i64> sub(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) {
        val[x].extract({a, getmax(chain[x], std::make_pair(a, c), 2)});
        chain[x].emplace(std::make_pair(a, c), v);
        val[x].emplace(a, getmax(chain[x], std::make_pair(a, c), 2));
    };
    
    auto del = [&](int x, int a, int c, i64 v) {
        val[x].extract({a, getmax(chain[x], std::make_pair(a, c), 2)});
        chain[x].extract({std::make_pair(a, c), v});
        val[x].emplace(a, getmax(chain[x], std::make_pair(a, c), 2));
    };
    
    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]);
        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]);
        val[y].extract({-1, res.ans});
    };
    
    auto update = [&](int x) {
        Info v;
        sub[x] = 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({sub[x], 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 = [&]() {
        auto res = get();
        std::cout << std::max(res.vl, res.ans) << "\n";
    };
    
    answer();
    while (q--) {
        int x, y;
        std::cin >> x >> y;
        x--;
        
        change(x, y);
        
        answer();
    }
    
    return 0;
}

详细

Test #1:

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

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: 3896kb

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: 1620ms
memory: 88060kb

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: 1232ms
memory: 78508kb

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: 1602ms
memory: 89020kb

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: 1193ms
memory: 77800kb

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: 1443ms
memory: 83484kb

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: 1185ms
memory: 78024kb

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: 1193ms
memory: 78320kb

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: 1507ms
memory: 83284kb

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: 1213ms
memory: 78920kb

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: 1586ms
memory: 87100kb

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: 1434ms
memory: 71428kb

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'