QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#483454 | #6737. Neighbourhood | ucup-team052# | TL | 0ms | 35740kb | C++14 | 4.6kb | 2024-07-18 17:30:42 | 2024-07-18 17:30:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef pair <int, int> pii;
typedef long long ll;
const int N = 2e5 + 5;
struct atom {
int id, x; ll y;
atom (int a = 0, int b = 0, ll c = 0) : id(a), x(b), y(c) {}
};
map <pii, int> mp;
vector <int> adj[N];
vector <pii> edg[N];
int u[N], v[N], w[N], tmp[N], bel[N], ans[N], isqry[N];
int n, q;
inline int getid(int u, int v) {
return mp[minmax(u, v)];
}
int tops[N], pre[N], sizz[N], siz[N], maxn[N], vis[N], fa[N];
int root, allsize, tot;
void getroot(int u, int fa) {
siz[u] = 1; maxn[u] = 0;
for (auto v : adj[u]) {
if (vis[v] || v == fa) continue;
getroot(v, u); siz[u] += siz[v];
maxn[u] = max(maxn[u], siz[v]);
}
maxn[u] = max(maxn[u], allsize - siz[u]);
if (maxn[u] < maxn[root]) root = u;
}
ll a[N], b[N], tag[N];
int bb[N], bl[N], br[N];
int B;
void rebuild(int id) {
for (int i = bl[id]; i <= br[id]; i++) b[i] = a[i];
sort(b + bl[id], b + br[id] + 1);
}
// 1 ~ tot
void init() {
B = floor(sqrt(tot) + 0.5);
for (int i = 1; i <= tot; i++) bl[i] = br[i] = tag[i] = 0;
for (int i = 1; i <= tot; i++) {
bb[i] = (i - 1) / B + 1;
if (!bl[bb[i]]) bl[bb[i]] = i;
br[bb[i]] = i;
}
for (int i = 1; i <= bb[tot]; i++) rebuild(i);
}
void add(int l, int r, ll v) {
// fprintf(stderr, "l = %d, r = %d, v = %lld\n", l, r, v);
if (bb[l] == bb[r]) {
for (int i = l; i <= r; i++) a[i] += v;
rebuild(bb[l]);
} else {
for (int i = l; i <= br[bb[l]]; i++) a[i] += v;
for (int i = bl[bb[r]]; i <= r; i++) a[i] += v;
for (int i = bb[l] + 1; i <= bb[r] - 1; i++) tag[i] += v;
rebuild(bb[l]); rebuild(bb[r]);
}
}
// <= v
int query(int l, int r, ll v) {
int ans = 0;
if (bb[l] == bb[r]) {
for (int i = l; i <= r; i++) {
if (a[i] + tag[bb[l]] <= v) ++ans;
}
} else {
for (int i = l; i <= br[bb[l]]; i++) {
if (a[i] + tag[bb[l]] <= v) ++ans;
}
for (int i = bl[bb[r]]; i <= r; i++) {
if (a[i] + tag[bb[r]] <= v) ++ans;
}
for (int i = bb[l] + 1; i <= bb[r] - 1; i++) {
ans += upper_bound(b + bl[i], b + br[i] + 1, v - tag[i]) - b - bl[i];
}
}
return ans;
}
void dfs1(int u, int rt, ll dis) {
tops[u] = ++tot; pre[tot] = u; sizz[u] = 1; bel[u] = rt; a[tot] = dis;
for (auto v : edg[u]) {
if (vis[v.first] || v.first == fa[u]) continue;
fa[v.first] = u; tmp[v.second] = w[v.second]; dfs1(v.first, rt, dis + w[v.second]); sizz[u] += sizz[v.first];
}
}
vector <atom> vec[N];
void solve(int u, vector <atom> qry) {
vis[u] = 1;
if (!qry.size()) return;
bel[u] = u; sizz[u] = 1; tops[u] = tot = 1; pre[1] = u; fa[u] = 0;
for (auto v : edg[u]) {
if (vis[v.first]) continue;
fa[v.first] = u; tmp[v.second] = w[v.second]; dfs1(v.first, v.first, w[v.second]); sizz[u] += sizz[v.first];
}
init();
for (auto v : adj[u]) vec[v].clear();
for (auto t : qry) {
if (t.id == 0) {
int u = ::u[t.x], v = ::v[t.x];
if (fa[v] == u) swap(u, v);
add(tops[u], tops[u] + sizz[u] - 1, t.y - tmp[t.x]); tmp[t.x] = t.y;
if (bel[u] == bel[v]) vec[bel[u]].push_back(t);
} else {
ans[t.id] += query(1, tot, t.y - a[tops[t.x]] - tag[bb[tops[t.x]]]);
if (t.x != u) {
ans[t.id] -= query(tops[bel[t.x]], tops[bel[t.x]] + sizz[bel[t.x]] - 1, t.y - a[tops[t.x]] - tag[bb[tops[t.x]]]);
vec[bel[t.x]].push_back(t);
}
}
}
for (auto v : adj[u]) {
if (vis[v]) continue;
root = 0; allsize = (siz[v] < siz[u] ? siz[v] : allsize - siz[u]);
getroot(v, u); solve(root, vec[v]);
}
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &u[i], &v[i], &w[i]);
adj[u[i]].push_back(v[i]);
adj[v[i]].push_back(u[i]);
edg[u[i]].emplace_back(v[i], i);
edg[v[i]].emplace_back(u[i], i);
mp[minmax(u[i], v[i])] = i;
}
vector <atom> vec;
for (int i = 1; i <= q; i++) {
int op, a; ll b;
scanf("%d%d%lld", &op, &a, &b);
if (op == 1) vec.push_back(atom(0, a, b));
else vec.push_back(atom(i, a, b)), isqry[i] = 1;
}
maxn[0] = n + 1; allsize = n; getroot(1, 0); solve(root, vec);
for (int i = 1; i <= q; i++) {
if (isqry[i]) printf("%d\n", ans[i]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 35740kb
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 ...