QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#186599#5249. BanditsRobeZHWA 1136ms138240kbC++144.8kb2023-09-24 06:01:272023-09-24 06:01:27

Judging History

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

  • [2023-09-24 06:01:27]
  • 评测
  • 测评结果:WA
  • 用时:1136ms
  • 内存:138240kb
  • [2023-09-24 06:01:27]
  • 提交

answer

#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define subnb true
#define Lnb true
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

template<class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int INF = (int)1e9;
const int N = (int)1e5 + 50, LOGN = 18;
//const int N = (int)1e3, LOGN = 18;
struct edge {
    int to, cost;
};


int n, q;
vector<edge> g[N];

struct LCA {
    int parent[LOGN][N];
    int depth[N];
    ll dr[N];

    void dfs(int v, int p, int d, ll d1) {
        parent[0][v] = p;
        depth[v] = d;
        dr[v] = d1;
        for (auto e : g[v]) {
            int nxt = e.to;
            if(nxt != p) dfs(nxt, v, d + 1,d1 + e.cost);
        }
    }

    void init() {
        dfs(0, -1, 0, 0);
        rep(k, 0, LOGN - 1) {
            rep(v, 0, n) parent[k + 1][v] =
                    parent[k][v] < 0 ? -1 : parent[k][parent[k][v]];
        }
    }

    int lca(int u, int v) {
        if(depth[u] > depth[v]) swap(u, v);
        rep(k, 0, LOGN) {
            if((depth[v] - depth[u]) >> k & 1) v = parent[k][v];
        }
        if(u == v) return u;
        for (int k = LOGN - 1; k >= 0; k--) {
            if(parent[k][u] != parent[k][v]) {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }

    ll min_dis(int u, int v) {
        int ca = lca(u, v);
        return dr[u] + dr[v] - 2 * dr[ca];
    }
} lca;


set<int> G[N];
int sub[N], par[N];
ll d1par[N], d0par[N];
Tree<pll> Sp[N], Sc[N];
int rt = -1;
pii es[N];

int dfs1(int v, int p) {
    sub[v] = 1;
    for (int nxt : G[v])
        if(nxt != p) sub[v] += dfs1(nxt,  v);
    return sub[v];
}

int dfs2(int v, int p, int nn) {
    for (int nxt : G[v]) {
        if(nxt != p && sub[nxt] > nn / 2) return dfs2(nxt, v, nn);
    }
    return v;
}

void decompose(int v, int p) {
    dfs1(v, -1);
    int centroid = dfs2(v, -1, sub[v]);
    if(p == -1) rt = centroid;
    par[centroid] = p;
    d1par[centroid] = lca.min_dis(centroid, p);
    if(p != -1) d0par[centroid] = d0par[p] + 1;
    for (int nxt : G[centroid]) {
        G[nxt].erase(centroid);
        decompose(nxt, centroid);
    }
    G[centroid].clear();
}



int main() {
//    ios::sync_with_stdio(false);
//    cin.tie(NULL);

    cin >> n;
    rep(i, 0, n - 1) {
        int u, v, w; cin >> u >> v >> w; u--, v--;
        G[u].insert(v);
        G[v].insert(u);
        g[u].push_back({v, w});
        g[v].push_back({u, w});
        es[i] = {u, v};
    }
    lca.init();
    decompose(0, -1);
//    rep(i, 0, n) {
//        cout << i + 1 << " par: " << par[i] + 1 << " d0:" << d0par[i] << " d1:" << d1par[i] << endl;
//    }
    cin >> q;
    rep(qid, 0, q) {
        char c; cin >> c;
        if(c == '+') {
            int v; ll w; cin >> v >> w; v--;
            int pv = -1;
            while(w >= 0) {
                Sp[v].insert({w, qid});
                if(pv != -1) Sc[pv].insert({w, qid});
//                cout << "adding " << w << ", " << qid << " to " << v + 1 << ", " << pv + 1 <<    endl;
                if(v == rt) break;
                w -= d1par[v];
                pv = v;
                v = par[v];
            }
        } else {
            int eid; cin >> eid; eid--;
            int u, v;
            tie(u, v) = es[eid];

//            cout << "query: edge " << u + 1 << " " << v + 1 << endl;
            if(d0par[u] < d0par[v]) swap(u, v);

//            int cur = u, pv = -1;
//            int cur = u;
            while(par[u] != v) u = par[u];
            assert(par[u] == v);




            ll res = 0;

            res += sz(Sc[u]) - Sc[u].order_of_key({0, -INF});
//            cout << "sepcial" << u + 1 << ", " << par[u] + 1 << endl;
//            cout << u + 1 << " has " << res << endl;
//            res += sz(Sp[u]) - Sp[u].order_of_key({dissum, -INF});
            int cur = v, pv = u;
            ll dissum = d1par[u];

            while(cur != -1) {
                ll c0 = sz(Sp[cur]) - Sp[cur].order_of_key({dissum, -INF});
                ll c1 = sz(Sc[pv]) - Sc[pv].order_of_key({dissum, -INF});

//                cout << "at " << "cur = " << cur + 1 << ", pv = " << pv + 1 << ": " << c0 - c1 << endl;
//                cout << "u"
                res += c0 - c1;
                if(cur == rt) break;
                dissum += d1par[cur];
                pv = cur;
                cur = par[cur];
            }
            cout << res << '\n';
        }
    }




}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 1136ms
memory: 138240kb

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
1
1
4
1
1
2
3
3
6
7
7
9
9
12
11
11
8
8
9
9
8
9
8
9
8
12
9
11
11
11
13
10
14
14
9
14
13
12
19
14
16
19
19
14
15
18
21
19
23
22
20
24
25
28
27
28
31
30
31
34
35
37
34
32
35
38
36
37
40
39
40
42
38
40
48
45
47
49
52
55
52
55
55
54
56
56
56
57
59
60
62
63
62
64
65
67
64
67
64
67
67
66
66
65
66
67
...

result:

wrong answer 4th lines differ - expected: '2', found: '1'