QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#149119#5249. BanditsForever_YoungWA 490ms31208kbC++144.0kb2023-08-24 01:32:372023-08-24 01:32:40

Judging History

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

  • [2023-08-24 01:32:40]
  • 评测
  • 测评结果:WA
  • 用时:490ms
  • 内存:31208kb
  • [2023-08-24 01:32:37]
  • 提交

answer

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
typedef long long LL;
const int N = 100011;
int vst[N];
int tim = 0;
const int inf = 1e9 + 7;
struct E {
    int y, z, n;
} e[N * 2];
vector<int> vec[N];
int q[N][3];
LL dep[N];
int blg[N], idx[N], son[N], siz[N], ans[N], x[N], y[N], z[N];
int l = 1;
typedef tree<pair<LL, int> , null_type, less<pair<LL, int> >, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ordered_set mp, mps[N];

void get_size(int v, int fa = 0) {
    siz[v] = 1;
    son[v] = 0;
    int mx = -1;
    for(int p = idx[v]; p; p = e[p].n) {
        int y = e[p].y;
        if(y == fa) continue;
        get_size(y, v);
        siz[v] += siz[y];
        if(siz[y] > mx) {
            mx = siz[y];
            son[v] = y;
        }
    }
}
int get_root(int st) {
    get_size(st);
    int tot = siz[st];
    while(siz[son[st]] > tot / 2) {
        st = son[st];
    }
    return st;
}
void dfs(int v, int fa = 0) {
    for(int p = idx[v]; p; p = e[p].n) {
        int y = e[p].y;
        if(y == fa) continue;
        int z = e[p].z;
        dep[y] = dep[v] + z;
        blg[y] = fa == 0 ? y : blg[v];
        dfs(y, v);
    }
}
void build(int x, int y, int z) {
    e[++l].y = y;
    e[l].z = z;
    e[l].n = idx[x];
    idx[x] = l;
}
mt19937 gene(233);
void dvcq(int st, vector<int> qids) {
    int rt = get_root(st);
    dep[rt] = 0;
    dfs(rt);
    int cnt = 0;
    int lstv, lstlen;
    for(int id : qids) {
        if(q[id][0] == '+') {
            int v = q[id][1];
            int len = q[id][2];
            lstv = v;
            lstlen = len;
            if(siz[st] == 2) {
                mp.insert({len, id});
            }else {
                if(dep[v] <= len) {
                    mp.insert({len - dep[v], id});
                    if(v == rt) {

                    }else {
                        mps[blg[v]].insert({len - dep[v], id});
                        vec[blg[v]].pb(id);
                    }
                }else {
                    vec[blg[v]].pb(id);
                }
            }
        }else {            
            int eid = q[id][1];
            if(siz[st] == 2) {
                ans[id] += mp.size() - mp.order_of_key({::z[eid], -1});
            }else {
                int x = ::x[eid];
                int y = ::y[eid];
                if(dep[x] < dep[y]) {
                    x = y;
                }
                int b = x == rt ? blg[y] : blg[x];
                ans[id] += mp.size() - mp.order_of_key({dep[x], -1});
                ans[id] -= mps[b].size() - mps[b].order_of_key({dep[x], -1});
                vec[b].pb(id);
            }
        }
    }
    if(siz[st] == 2) return;
    mp.clear();
    for(int p = idx[rt]; p; p = e[p].n) {
        int y = e[p].y;
        mps[y].clear();
    }
    for(int p = idx[rt]; p; p = e[p].n) {
        int y = e[p].y;
        int bak = idx[rt];
        int nxt = e[p].n;
        idx[rt] = p;
        e[p].n = 0;

        dvcq(y, move(vec[y]));
        idx[rt] = bak;
        e[p].n = nxt;
    }
}
int main() {
    int n, Q;
    scanf("%d", &n);
    for(int i = 0; i < n - 1; i++) {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        build(x, y, z);
        build(y, x, z);
        ::x[i] = x;
        ::y[i] = y;
        ::z[i] = z;
    }
    scanf("%d", &Q);
    vector<int> queries;
    for(int i = 0; i < Q; i++) {
        queries.pb(i);
        static char st[111];
        scanf("%s", st);
        q[i][0] = st[0];
        if(q[i][0] == '+') {
            scanf("%d%d", &q[i][1], &q[i][2]);
        }else {
            scanf("%d", &q[i][1]);
            q[i][1] -= 1;
        }
    }
    dvcq(1, queries);
    int _ = 0;
    for(int i = 0; i < Q; i++) {
        if(q[i][0] == '?') {
            _++;
            printf("%d\n", ans[i]);
        }
    }
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 490ms
memory: 31208kb

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:

5
7
6
13
12
11
6
8
7
15
8
19
14
15
15
14
22
20
22
17
18
19
16
17
15
18
13
16
24
21
24
17
25
22
24
20
18
21
18
22
25
28
20
29
32
28
25
21
26
35
30
33
31
35
36
34
37
32
38
40
40
38
52
47
52
50
50
54
51
57
53
58
47
50
62
54
56
59
56
59
68
64
70
75
73
81
70
70
76
66
77
78
86
80
77
85
82
82
89
80
88
91
9...

result:

wrong answer 1st lines differ - expected: '0', found: '5'