QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#182926#6737. Neighbourhooducup-team004#TL 3ms32696kbC++203.6kb2023-09-18 19:13:072023-09-18 19:13:07

Judging History

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

  • [2023-09-18 19:13:07]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:32696kb
  • [2023-09-18 19:13:07]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr int N = 2E5;
constexpr int LogN = 20;

int n, q;
int x[N], y[N], w[N];
std::vector<int> adj[N];

int cdep[N], cpar[N], siz[N];
int par[LogN][N], in[LogN][N], out[LogN][N], seq[LogN][N];
int st[LogN][N];
i64 A[LogN][N], B[LogN][N];
bool vis[N];
int cur[LogN];
int blk[N + 1];

void dfs(int d, int x) {
    in[d][x] = cur[d]++;
    seq[d][in[d][x]] = x;
    siz[x] = 1;
    for (auto y : adj[x]) {
        if (vis[y] || y == par[d][x]) {
            continue;
        }
        par[d][y] = x;
        dfs(d, y);
        siz[x] += siz[y];
    }
    out[d][x] = cur[d];
}

int find(int x, int p, int s) {
    for (auto y : adj[x]) {
        if (vis[y] || y == p || 2 * siz[y] <= s) {
            continue;
        }
        return find(y, x, s);
    }
    return x;
}

int solve(int d, int r) {
    int t = cur[d];
    par[d][r] = -1;
    dfs(d, r);
    r = find(r, -1, siz[r]);
    par[d][r] = -1;
    cur[d] = t;
    dfs(d, r);
    cdep[r] = d;
    vis[r] = true;
    for (auto y : adj[r]) {
        if (vis[y]) {
            continue;
        }
        y = solve(d + 1, y);
        cpar[y] = r;
    }
    return r;
}

int lca(int u, int v) {
    if (u == v) {
        return u;
    }
    u = in[0][u];
    v = in[0][v];
    if (u > v) {
        std::swap(u, v);
    }
    int k = std::__lg(v - u);
    int a = st[k][u];
    int b = st[k][v - (1 << k)];
    return in[0][a] < in[0][b] ? a : b;
}

i64 getdep(int u) {
    return A[0][in[0][u]];
}

i64 dist(int u, int v) {
    int l = lca(u, v);
    return getdep(u) + getdep(v) - 2 * getdep(l);
}

void modify(int i, int c) {
    int X = x[i], Y = y[i];
    int u = X, v = Y;
    while (cdep[u] > cdep[v]) {
        u = cpar[u];
    }
    while (cdep[v] > cdep[u]) {
        v = cpar[v];
    }
    while (u != v) {
        u = cpar[u];
        v = cpar[v];
    }
    while (u != -1) {
        int d = cdep[u];
        int l = std::max(in[d][X], in[d][Y]);
        int r = std::min(out[d][X], out[d][Y]);
        for (int j = l; j < r; j++) {
            A[d][j] += c;
        }
        u = cpar[u];
    }
}

int query(int x, i64 L) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans += dist(x, i) <= L;
    }
    return ans;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    for (int i = 1; i <= N; i++) {
        blk[i] = std::sqrt(i);
    }
    
    std::cin >> n >> q;
    
    for (int i = 1; i < n; i++) {
        std::cin >> x[i] >> y[i] >> w[i];
        x[i]--, y[i]--;
        adj[x[i]].push_back(y[i]);
        adj[y[i]].push_back(x[i]);
    }
    cpar[solve(0, 0)] = -1;
    
    for (int i = 0; i < n; i++) {
        if (in[0][i] != 0) {
            st[0][in[0][i] - 1] = par[0][i];
        }
    }
    for (int k = 0; k + 1 < LogN; k++) {
        for (int i = 0; i + (2 << k) <= n - 1; i++) {
            int a = st[k][i];
            int b = st[k][i + (1 << k)];
            st[k + 1][i] = in[0][a] < in[0][b] ? a : b;
        }
    }
    
    for (int i = 1; i < n; i++) {
        modify(i, w[i]);
    }
    
    for (int _ = 0; _ < q; _++) {
        int o;
        std::cin >> o;
        
        if (o == 1) {
            int i, c;
            std::cin >> i >> c;
            modify(i, c - w[i]);
            w[i] = c;
        } else {
            int x;
            i64 d;
            std::cin >> x >> d;
            x--;
            std::cout << query(x, d) << "\n";
        }
    }
    
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 32696kb

input:

3 7
1 2 3
2 3 1
2 2 1
2 1 3
2 3 4
1 1 1
2 2 1
2 1 0
2 3 1

output:

2
2
3
3
1
2

result:

ok 6 numbers

Test #2:

score: -100
Time Limit Exceeded

input:

200000 200000
1 2 146181238
2 3 45037818
3 4 176924060
4 5 969365276
5 6 683948619
6 7 356268194
7 8 871634797
8 9 630254480
9 10 283061123
10 11 204904965
11 12 838381393
12 13 516373812
13 14 253862710
14 15 223572474
15 16 114452997
16 17 145251056
17 18 905638436
18 19 375445402
19 20 549829545
...

output:

219
62303
1358
5532
65345
682
11856
120285
4980
5689
2998
2314
18102
8014
20512
2827
113022
74534
159775
14517
17961
21855
8138
265
3336
3251
7023
35187
4932
151611
14338
101
899
117
64441
888
10380
1833
29381
1014
4806
10770
23734
236
37258
2280
14550
2196
38205
80950
80839
4517
74570
13972
95914
7...

result: