QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#190520#6735. Treeucup-team004RE 0ms3688kbC++205.5kb2023-09-29 01:59:212023-09-29 01:59:21

Judging History

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

  • [2023-09-29 01:59:21]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3688kb
  • [2023-09-29 01:59:21]
  • 提交

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 << 18;

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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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: -100
Runtime Error

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:


result: