QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#285718#5249. Banditslight_ink_dots#WA 1180ms454768kbC++144.4kb2023-12-16 21:41:322023-12-16 21:41:33

Judging History

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

  • [2023-12-16 21:41:33]
  • 评测
  • 测评结果:WA
  • 用时:1180ms
  • 内存:454768kb
  • [2023-12-16 21:41:32]
  • 提交

answer

#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")

#include <bits/stdc++.h>
using namespace std;

const int maxn = 100010, maxlg = 20;
struct edge {
    int to;
    long long val;
};
vector<edge> G[maxn];
vector<pair<long long, int>> tag[maxn];
vector<int> vec[maxn];
bool vis[maxn];
int build(int, int);
inline void insert(int, int);
inline int query(int);
void init(int);
int dep1[maxn];

int main() {
    int n;
    // n = 100000;
    scanf("%d", &n);
    static int U[maxn], V[maxn];
    for (int i = 1; i < n; i++) {
        int u, v;
        long long w;
        // u = i, v = i + 1, w = 1;
        scanf("%d %d %lld", &u, &v, &w);
        G[u].push_back({ v, w });
        G[v].push_back({ u, w });
        U[i] = u, V[i] = v;
    }
    build(1, 0);
    init(n);
    int q;
    // q = 100000;
    scanf("%d", &q);
    while (q--) {
        char ch = getchar();
        // ch = q & 1 ? '+' : '?';
        while (ch != '+' && ch != '?') ch = getchar();
        if (ch == '+') {
            int x, r;
            // x = 10, r = 9;
            scanf("%d %d", &x, &r);
            insert(x, r);
        } else {
            int y;
            // y = 1;
            scanf("%d", &y);
            printf("%d\n", query(dep1[U[y]] > dep1[V[y]] ? U[y] : V[y]));
        }
    }
    return 0;
}

int fa[maxn], dep[maxn], st[maxn];
long long dis[maxlg][maxn];

int build(const int u, const int d) {
    static int siz[maxn];
    function<void(int, int)> dfs = [&dfs, r = u](const int u, const int fa) {
        siz[u] = 1, vec[r].push_back(u);
        for (auto e : G[u])
            if (e.to != fa && !vis[e.to])
                dfs(e.to, u), siz[u] += siz[e.to];
        return;
    };
    dfs(u, 0);
    int r, val = vec[u].size();
    for (int i : vec[u]) {
        int cur = vec[u].size() - siz[i];
        for (auto e : G[i])
            if (siz[e.to] < siz[i])
                cur = max(cur, siz[e.to]);
        if (val > cur)
            r = i, val = cur;
    }
    if (r != u)
        vec[r] = vec[u], vec[u].clear();
    function<void(int, int)> dfs2 = [&dfs2, d](const int u, const int fa) {
        for (auto e : G[u])
            if (e.to != fa && !vis[e.to])
                dis[d][e.to] = dis[d][u] + e.val, dfs2(e.to, u);
        return;
    };
    vis[r] = true, dep[r] = d, dis[d][r] = 0, dfs2(r, 0);
    for (auto e : G[r])
        if (!vis[e.to])
            fa[build(e.to, d + 1)] = r;
    return r;
}

struct node {
    int ls, rs;
    int val;
};
node t[maxn << 9];
int r1[maxn], r2[maxn], Cnt;

void modify(int& u, const int l, const int r, const int p, const int val) {
    if (!u)
        u = ++Cnt;
    t[u].val += val;
    if (l == r)
        return;
    int mid = (l + r) >> 1;
    if (p <= mid)
        modify(t[u].ls, l, mid, p, val);
    else
        modify(t[u].rs, mid + 1, r, p, val);
    return;
}

int query(const int u, const int l, const int r, const int p) {
    if (!u || r < p)
        return 0;
    if (p <= l)
        return t[u].val;
    int mid = (l + r) >> 1;
    return query(t[u].ls, l, mid, p) + query(t[u].rs, mid + 1, r, p);
}

int fa1[maxn];
int lg[maxn], anc[maxlg][maxn];
long long dis1[maxn];

void dfs1(const int u) {
    anc[0][u] = fa1[u];
    for (int l = 1; l <= lg[dep1[u]]; l++) anc[l][u] = anc[l - 1][anc[l - 1][u]];
    for (auto e : G[u])
        if (e.to != fa1[u]) {
            fa1[e.to] = u, dep1[e.to] = dep1[u] + 1;
            dis1[e.to] = dis1[u] + e.val;
            dfs1(e.to);
        }
    return;
}

void init(const int n) {
    for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
    dfs1(1);
    return;
}

const int lim = 1000000000;

int del[maxn];

inline void insert(int u, int x) {
    int v = u;
    for (int l = lg[dep1[u]]; l >= 0; l--)
        if (dis1[u] - dis1[anc[l][v]] <= x)
            v = anc[l][v];
    del[v]++;
    for (int d = dep[u], f = u, last = 0; d >= 0; d--, last = f, f = fa[f])
        if (dis[d][u] <= x) {
            modify(r1[f], 0, lim, x - dis[d][u], 1);
            if (last != 0)
                modify(r2[last], 0, lim, x - dis[d][u], 1);
        }
    return;
}

inline int query(int u) {
    int ret = 0;
    for (int d = dep[u], f = u, last = 0; d >= 0; d--, last = f, f = fa[f]) {
        ret += query(r1[f], 0, lim, dis[d][u]);
        if (last != 0)
            ret -= query(r2[last], 0, lim, dis[d][u]);
    }
    return ret - del[u];
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1180ms
memory: 454768kb

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:

ok 50000 lines

Test #2:

score: 0
Accepted
time: 563ms
memory: 283612kb

input:

100000
30038 18547 1594
65857 34063 4575
36600 72585 2328
99199 77595 1590
64257 48199 589
72301 40302 5083
69474 97536 606
60079 67381 9331
65982 39033 205
84122 65285 8508
18167 44905 3704
93490 94986 5941
27155 46374 6616
36232 62969 2212
79807 68652 7199
87352 59101 6880
94571 53224 3552
63610 8...

output:

0
1
3
3
3
3
4
6
10
10
12
14
14
16
18
19
19
19
19
22
22
23
23
23
23
24
25
26
26
26
26
26
26
28
31
31
31
31
31
34
34
36
40
41
41
42
42
42
42
42
44
44
44
47
48
48
48
48
48
48
48
48
48
49
50
50
53
54
54
55
55
56
56
56
56
56
59
62
62
62
62
62
62
62
62
62
62
63
65
67
69
69
69
73
73
73
74
76
76
76
76
76
79...

result:

ok 50000 lines

Test #3:

score: -100
Wrong Answer
time: 325ms
memory: 95324kb

input:

100000
61878 94907 27495452
40498 66053 163324081
9987 91760 269034612
88613 6169 634714395
87422 83687 263182872
22328 64374 595886322
57437 38976 201931120
75020 26926 516189886
88639 96262 269100132
88285 44915 173237252
88407 91931 174315082
12843 50312 641940581
13297 52746 120211351
89089 4638...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
1
0
0
0
...

result:

wrong answer 117th lines differ - expected: '0', found: '1'