QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#285699 | #5249. Bandits | light_ink_dots# | RE | 0ms | 0kb | C++14 | 4.4kb | 2023-12-16 21:34:20 | 2023-12-16 21:34:21 |
answer
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
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;
scanf("%d", &n);
static int U[maxn], V[maxn];
for (int i = 1; i < n; i++) {
int u, v;
long long w;
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;
scanf("%d", &q);
while (q--) {
char ch = getchar();
while (ch != '+' && ch != '?') ch = getchar();
if (ch == '+') {
int x, r;
scanf("%d %d", &x, &r);
insert(x, r);
} else {
int y;
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 << 7];
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) {
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; l--)
if (dis1[u] - dis1[anc[l][v]] <= x)
v = anc[l][v];
del[fa1[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: 0
Runtime Error
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 72 78 77 73 85 76 86 82 85 76 82 8...