QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#96207#5249. Bandits8BQube#WA 1660ms141908kbC++174.0kb2023-04-13 17:06:512023-04-13 17:06:53

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-13 17:06:53]
  • 评测
  • 测评结果:WA
  • 用时:1660ms
  • 内存:141908kb
  • [2023-04-13 17:06:51]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back

const int N = 100005;
struct Cent_Dec {
    vector<pll> G[N];
    int n, pa[N], layer[N], sz[N], done[N];
    int counter = 2e9;
    ll dis[__lg(N) + 1][N];
    tree<pll, null_type, less<pll>, rb_tree_tag, tree_order_statistics_node_update> st[N], upst[N];
    void insert(tree<pll, null_type, less<pll>, rb_tree_tag, tree_order_statistics_node_update> &rbtree, ll w) { 
        rbtree.insert(pll(w, counter));
        counter--;
    }
    int leq(tree<pll, null_type, less<pll>, rb_tree_tag, tree_order_statistics_node_update> &rbtree, ll w) {
        return rbtree.order_of_key(pll(w, 0));
    }
    void init(int _n) {
        n = _n, layer[0] = -1;
        fill_n(pa + 1, n, 0), fill_n(done + 1, n, 0);
        for (int i = 1; i <= n; ++i) G[i].clear();
    }
    void add_edge(int a, int b, int w) {
        G[a].pb(pll(b, w)), G[b].pb(pll(a, w));
    }
    void get_cent(int u, int f, int &mx, int &c, int num) {
        int mxsz = 0;
        sz[u] = 1;
        for (pll e : G[u])
            if (!done[e.X] && e.X != f) {
                get_cent(e.X, u, mx, c, num);
                sz[u] += sz[e.X], mxsz = max(mxsz, sz[e.X]);
            }
        if (mx > max(mxsz, num - sz[u]))
            mx = max(mxsz, num - sz[u]), c = u;
    }
    void dfs(int u, int f, ll d, int org) {
        dis[layer[org]][u] = d;
        for (pll e : G[u])
            if (!done[e.X] && e.X != f)
                dfs(e.X, u, d + e.Y, org);
    }
    int cut(int u, int f, int num) {
        int mx = 1e9, c = 0;
        get_cent(u, f, mx, c, num);
        done[c] = 1, pa[c] = f, layer[c] = layer[f] + 1;
        for (pll e : G[c])
            if (!done[e.X]) {
                if (sz[e.X] > sz[c])
                    cut(e.X, c, num - sz[c]);
                else
                    cut(e.X, c, sz[e.X]);
                dfs(e.X, c, e.Y, c);
            }
        return done[c] = 0, c;
    }
    void build() { cut(1, 0, n); }
    void modify(int u, ll w) {
        for (int a = u, ly = layer[a]; a; a = pa[a], --ly) {
            if (w < dis[ly][a]) break;
            insert(st[a], w - dis[ly][u]);
            //cerr << "insert " << a << " " << w - dis[ly][u] << "\n";
            if (pa[a]) {
                insert(upst[a], w - dis[ly - 1][u]);
                //cerr << "up insert " << a << " " << w - dis[ly - 1][u] << "\n";
            }
        }
    }
    int query(int l, int r, ll d) {
        if (layer[l] < layer[r]) swap(l, r);
        int ans = 0;
        for (int a = l, ly = layer[l]; a != r; a = pa[a], --ly) {
            ans += SZ(st[a]) - leq(st[a], d + dis[ly][l]);
            if (pa[a] != r)
                ans -= SZ(upst[a]) - leq(upst[a], d + dis[ly - 1][l]);
            else
                ans -= SZ(upst[a]) - leq(upst[a], d);
        }
        for (int a = r, ly = layer[r]; a; a = pa[a], --ly) {
            ans += SZ(st[a]) - leq(st[a], d + dis[ly][r]);
            if (pa[a])
                ans -= SZ(upst[a]) - leq(upst[a], d + dis[ly - 1][r]);
        }
        return ans;
    }
} trees;

struct Edge {
    int u, v, w;
} edges[100005];

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n, q;
    cin >> n;
    trees.init(n);
    for (int i = 1; i < n; ++i) {
        cin >> edges[i].u >> edges[i].v >> edges[i].w;
        trees.add_edge(edges[i].u, edges[i].v, edges[i].w);
    }
    trees.build();
    cin >> q;
    while (q--) {
        char c;
        cin >> c;
        if (c == '+') {
            int x, r;
            cin >> x >> r;
            trees.modify(x, r);
        }
        else if (c == '?') {
            int x;
            cin >> x;
            cout << trees.query(edges[x].u, edges[x].v, edges[x].w) << "\n";
        }
        else {
            assert(0);
        }
    }
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 1660ms
memory: 141908kb

input:

100000
2670 75097 4080
87477 75802 1712
51835 36626 2883
19412 25923 5852
23976 19312 2520
82536 19514 2492
27160 66601 4483
99087 15088 3504
47050 58820 2964
37063 5696 9901
7717 1496 4891
79136 5448 4340
22575 81285 9289
96280 3803 9877
41980 32139 2855
44236 64938 3298
5983 99947 9666
95856 62545...

output:

0
0
0
2
2
5
2
2
3
4
4
7
8
9
11
10
14
12
12
10
11
10
10
9
10
11
11
9
15
11
14
13
14
16
11
17
15
13
15
14
14
20
15
20
22
22
20
17
23
23
24
29
24
26
30
31
36
28
37
39
35
34
45
39
46
45
43
46
42
49
44
50
43
47
52
50
49
57
51
56
61
58
68
66
69
69
61
63
67
63
72
74
78
72
73
78
77
73
85
76
86
82
85
76
82
8...

result:

wrong answer 487th lines differ - expected: '434', found: '433'