QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#431735#7563. Fun on TreepandapythonerWA 2398ms81852kbC++206.7kb2024-06-06 00:32:382024-06-06 00:32:38

Judging History

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

  • [2024-06-06 00:32:38]
  • 评测
  • 测评结果:WA
  • 用时:2398ms
  • 内存:81852kb
  • [2024-06-06 00:32:38]
  • 提交

answer

#include <bits/stdc++.h>

#define int long long
#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()


using namespace std;

const ll inf = 1e18;
mt19937 rnd(234);


struct SGT {
    int n;
    vector<pair<ll, int>> t;
    vector<ll> d;


    void apply(int v, ll x) {
        t[v].first += x;
        d[v] += x;
    }


    void push(int v) {
        apply(v + v, d[v]);
        apply(v + v + 1, d[v]);
        d[v] = 0;
    }


    void upd(int v) {
        t[v] = max(t[v + v], t[v + v + 1]);
    }


    void build(int v, int tl, int tr, vector<pair<ll, int>>& a) {
        if (tl == tr) {
            t[v] = a[tl];
            return;
        }
        int tm = (tl + tr) / 2;
        build(v + v, tl, tm, a);
        build(v + v + 1, tm + 1, tr, a);
        upd(v);
    }


    void build(vector<pair<ll, int>> a) {
        n = (int)a.size();
        t.assign(4 * n, make_pair(-inf, -1));
        d.assign(4 * n, 0);
        build(1, 0, n - 1, a);
    }


    void add(int v, int tl, int tr, int l, int r, ll x) {
        if (tr < l or r < tl) {
            return;
        }
        if (l <= tl and tr <= r) {
            apply(v, x);
            return;
        }
        int tm = (tl + tr) / 2;
        push(v);
        add(v + v, tl, tm, l, r, x);
        add(v + v + 1, tm + 1, tr, l, r, x);
        upd(v);
    }


    void add(int l, int r, ll x) {
        add(1, 0, n - 1, l, r, x);
    }


    void assign(int v, int tl, int tr, int i, ll x, int j) {
        if (tr < i or i < tl) {
            return;
        }
        if (tl == tr) {
            t[v] = { x, j };
            return;
        }
        int tm = (tl + tr) / 2;
        push(v);
        assign(v + v, tl, tm, i, x, j);
        assign(v + v + 1, tm + 1, tr, i, x, j);
        upd(v);
    }


    void assign(int i, ll x, int j) {
        assign(1, 0, n, i, x, j);
    }


    pair<ll, int> get(int v, int tl, int tr, int l, int r) {
        if (tr < l or r < tl) {
            return make_pair(-inf, -1);
        }
        if (l <= tl and tr <= r) {
            return t[v];
        }
        int tm = (tl + tr) / 2;
        push(v);
        return max(get(v + v, tl, tm, l, r), get(v + v + 1, tm + 1, tr, l, r));
    }


    pair<ll, int> get(int l, int r) {
        return get(1, 0, n - 1, l, r);
    }
};


struct edge {
    int to;
    ll w;
};


int n, q;
vector<ll> a;
vector<vector<edge>> g;
vector<int> forth, back;
vector<int> up, h, depth, par, tout;
vector<int> sz;
SGT sgt, sgt_side;


pair<ll, int> get_vertex_value(int v, int baned = -1) {
    pair<ll, int> val;
    if (baned == -1) {
        val = sgt.get(v, tout[v] - 1);
    } else {
        assert(par[baned] == v);
        val = max(sgt.get(v, baned - 1), sgt.get(tout[baned], tout[v] - 1));
    }
    return make_pair(val.first - depth[v] * 2, val.second);
}


void dfs1(int v) {
    sz[v] = 1;
    int mx_sz = -1;
    int mx_szi = -1;
    for (int i = 0; i < (int)g[v].size(); i += 1) {
        auto [to, w] = g[v][i];
        dfs1(to);
        sz[v] += sz[to];
        if (sz[to] > mx_sz) {
            mx_sz = sz[to];
            mx_szi = i;
        }
    }
    if (mx_szi != -1) {
        swap(g[v][0], g[v][mx_szi]);
    }
}


void dfs2(int v, ll mh, ll mdpth, ll mup, int p) {
    forth[v] = back.size();
    back.push_back(v);
    h[forth[v]] = mh;
    depth[forth[v]] = mdpth;
    up[forth[v]] = forth[mup];
    if (p == -1) {
        par[forth[v]] = -1;
    } else {
        par[forth[v]] = forth[p];
    }
    for (auto [to, w] : g[v]) {
        int toup = mup;
        if (to != g[v][0].to) {
            toup = to;
        }
        dfs2(to, mh + 1, mdpth + w, toup, v);
    }
    tout[forth[v]] = (int)back.size();
}

void reorder() {
    sz.resize(n);
    dfs1(0);
    forth.resize(n);
    back.clear();
    back.reserve(n);
    h.resize(n);
    depth.resize(n);
    up.resize(n);
    par.resize(n);
    tout.resize(n);
    dfs2(0, 0, 0, 0, -1);
    vector<vector<edge>> ng(n);
    for (int v = 0; v < n; v += 1) {
        for (auto [to, w] : g[v]) {
            ng[forth[v]].push_back(edge{ forth[to], w });
        }
    }
    g = ng;
}


void build() {
    reorder();
    vector<pair<ll, int>> sgt_init(n), sgt_side_init(n, make_pair(-inf, -1));
    for (int v = 0; v < n; v += 1) {
        sgt_init[v] = make_pair(depth[v] - a[back[v]], -back[v]);
    }
    sgt.build(sgt_init);
    sgt_side.build(sgt_side_init);
    for (int v = 0; v < n; v += 1) {
        int baned = -1;
        if (!g[v].empty()) {
            baned = g[v][0].to;
        }
        auto val = get_vertex_value(v, baned);
        sgt_side.assign(v, val.first, val.second);
    }
}


void change(int v, ll x) {
    v = forth[v];
    x = -x;
    sgt.add(v, tout[v] - 1, x);
    sgt_side.add(v, tout[v] - 1, x);
    while (v != -1) {
        v = up[v];
        v = par[v];
        if (v == -1) {
            break;
        }
        assert(!g[v].empty());
        int baned = g[v][0].to;
        auto val = get_vertex_value(v, baned);
        sgt_side.assign(v, val.first, val.second);
    }
}


pair<ll, int> get(int root) {
    root = forth[root];
    int v = root;
    pair<ll, int> rs = make_pair(-inf, -1);
    auto val = get_vertex_value(root);
    rs = max(rs, make_pair(val.first + depth[root], val.second));
    while (v != -1) {
        val = sgt_side.get(up[v], v - 1);
        rs = max(rs, make_pair(val.first + depth[root], val.second));
        v = up[v];
        if (par[v] == -1) {
            break;
        }
        val = get_vertex_value(par[v], v);
        rs = max(rs, make_pair(val.first + depth[root], val.second));
        v = par[v];
    }
    return make_pair(rs.first, -rs.second);
}





int32_t main() {
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }
    cin >> n >> q;
    a.resize(n);
    for (int i = 0; i < n; i += 1) {
        cin >> a[i];
    }
    g.assign(n, vector<edge>());
    for (int i = 1; i < n; i += 1) {
        int p;
        ll w;
        cin >> p >> w;
        --p;
        g[p].push_back(edge{ i, w });
    }
    build();
    for (int itr = 0; itr < q; itr += 1) {
        int x, y;
        ll val;
        cin >> x >> y >> val;
        --x;
        --y;
        change(y, val);
        auto [goodness, v] = get(x);
        cout << v + 1 << " " << goodness << "\n";
    }
    return 0;
}


/*
6 6
1 1 4 5 1 4
1 5
2 0
3 2
4 1
5 6
3 2 -100000
1 2 100000
1 1 0
2 2 66
3 1 5
4 4 -3

5 6
-10 0 2 -4 8
1 7
1 1
2 2
2 -2
1 1 100
2 1 -100
1 1 0
4 3 10
2 5 3
5 2 2
*/

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3544kb

input:

6 6
1 1 4 5 1 4
1 5
2 0
3 2
4 1
5 6
3 2 -100000
1 2 100000
1 1 0
2 2 66
3 1 5
4 4 -3

output:

6 100005
6 10
6 10
1 4
1 -1
1 1

result:

ok 6 lines

Test #2:

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

input:

5 6
-10 0 2 -4 8
1 7
1 1
2 2
2 -2
1 1 100
2 1 -100
1 1 0
4 3 10
2 5 3
5 2 2

output:

4 -87
1 17
4 13
1 19
1 17
1 15

result:

ok 6 lines

Test #3:

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

input:

6 3
0 0 0 0 0 0
1 10
1 10
1 -100
4 10
4 11
1 1 0
4 1 0
1 4 1000

output:

2 10
6 11
2 10

result:

ok 3 lines

Test #4:

score: 0
Accepted
time: 1ms
memory: 3572kb

input:

2 0
1 1
1 3

output:


result:

ok 0 lines

Test #5:

score: 0
Accepted
time: 1949ms
memory: 81644kb

input:

200000 200000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...

output:

119017 15000000000
120167 17000000000
119017 15000000000
119017 15000000000
120167 17000000000
120167 15000000000
120167 16000000000
119017 17000000000
119017 16000000000
119017 12000000000
119017 17000000000
120167 16000000000
120167 14000000000
120167 17000000000
120167 18000000000
120167 16000000...

result:

ok 200000 lines

Test #6:

score: 0
Accepted
time: 2398ms
memory: 81832kb

input:

200000 200000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...

output:

169355 88000000000
171273 95000000000
171273 100000000000
169355 88000000000
169355 93000000000
169355 97000000000
169355 93000000000
171273 78000000000
171273 86000000000
169355 90000000000
169355 84000000000
169355 80000000000
169355 78000000000
171273 84000000000
169355 89000000000
171273 8400000...

result:

ok 200000 lines

Test #7:

score: 0
Accepted
time: 1926ms
memory: 81620kb

input:

200000 200000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...

output:

60406 17000000000
163359 19000000000
163359 18000000000
163359 17000000000
163359 18000000000
60406 17000000000
163359 16000000000
60406 16000000000
60406 18000000000
163359 17000000000
163359 16000000000
163359 15000000000
163359 18000000000
163359 16000000000
60406 13000000000
163359 15000000000
6...

result:

ok 200000 lines

Test #8:

score: 0
Accepted
time: 2383ms
memory: 81852kb

input:

200000 200000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...

output:

158239 95000000000
170228 90000000000
170228 91000000000
158239 97000000000
158239 90000000000
170228 97000000000
170228 95000000000
158239 89000000000
170228 84000000000
158239 97000000000
158239 88000000000
170228 81000000000
158239 89000000000
170228 84000000000
170228 84000000000
158239 83000000...

result:

ok 200000 lines

Test #9:

score: -100
Wrong Answer
time: 1936ms
memory: 81836kb

input:

200000 200000
-999999197 -999999547 -999999355 -999999799 -999999360 -999999720 -999999365 -999999419 -999999958 -999999574 -999999169 -999999422 -999999695 -999999971 -999999820 -999999822 -999999107 -999999420 -999999519 -999999970 -999999601 -999999521 -999999134 -999999638 -999999338 -999999570 ...

output:

2841 999999254
109445 999999369
58340 999999315
117049 999999782
118472 999999076
24148 999999131
47844 999999290
113056 999999668
46680 999999228
109091 999999634
68365 999999666
156304 999999674
39689 999999729
103343 999999322
123506 999999661
189258 999999210
142816 999999890
88985 999999626
120...

result:

wrong answer 566th lines differ - expected: '141263 -476', found: '76407 230'