QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#182923 | #6737. Neighbourhood | ucup-team004# | ML | 0ms | 32912kb | C++20 | 3.3kb | 2023-09-18 19:10:51 | 2023-09-18 19:10:51 |
Judging History
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 (y == par[d][x]) {
continue;
}
par[d][y] = x;
dfs(d, y);
siz[x] += siz[y];
}
out[d][x] = cur[d];
}
int solve(int d, int r) {
int t = cur[d];
par[d][r] = -1;
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 32912kb
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
Memory 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 ...