QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#297697 | #6737. Neighbourhood | ucup-team087# | WA | 3554ms | 299104kb | C++14 | 7.8kb | 2024-01-04 23:53:34 | 2024-01-04 23:53:35 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
// get i
// add val to [a, b)
// count < val in [a, b)
// O(sqrt(n) log(n)) per query
template <class T> struct RangeAddRangeCount {
int n, l, m;
vector<T> ts, offset, sorted;
RangeAddRangeCount() {}
RangeAddRangeCount(const vector<T> &ts_) : ts(ts_) {
n = ts.size();
m = max<int>(sqrt(n), 1);
l = n / m;
offset.assign(l + 1, 0);
sorted.resize(l * m);
for (int x = 0; x < l; ++x) build(x);
}
void build(int x) {
assert(0 <= x); assert(x < l);
std::copy(ts.begin() + (x * m), ts.begin() + ((x + 1) * m), sorted.begin() + (x * m));
std::sort(sorted.begin() + (x * m), sorted.begin() + ((x + 1) * m));
}
T operator[](int i) const {
assert(0 <= i); assert(i < n);
return offset[i / m] + ts[i];
}
void add(int a, int b, T val) {
assert(0 <= a); assert(a <= b); assert(b <= n);
const int xA = (a + m - 1) / m;
const int xB = b / m;
if (xA <= xB) {
for (int i = xA * m; --i >= a; ) ts[i] += val;
if (xA > 0) build(xA - 1);
for (int x = xA; x < xB; ++x) offset[x] += val;
for (int i = xB * m; i < b; ++i) ts[i] += val;
if (xB < l) build(xB);
} else {
for (int i = a; i < b; ++i) ts[i] += val;
if (xB < l) build(xB);
}
}
int count(int a, int b, T val) const {
assert(0 <= a); assert(a <= b); assert(b <= n);
const int xA = (a + m - 1) / m;
const int xB = b / m;
int cnt = 0;
if (xA <= xB) {
for (int i = xA * m; --i >= a; ) if (offset[xA - 1] + ts[i] < val) ++cnt;
for (int x = xA; x < xB; ++x) {
cnt += (lower_bound(sorted.begin() + (x * m), sorted.begin() + ((x + 1) * m), val - offset[x]) - (sorted.begin() + (x * m)));
}
for (int i = xB * m; i < b; ++i) if (offset[xB] + ts[i] < val) ++cnt;
} else {
for (int i = a; i < b; ++i) if (offset[xB] + ts[i] < val) ++cnt;
}
return cnt;
}
};
////////////////////////////////////////////////////////////////////////////////
int N;
vector<int> A, B;
vector<Int> C;
vector<vector<int>> G;
vector<int> sz, del;
void dfsSz(int u, int p) {
sz[u] = 1;
for (const int i : G[u]) {
const int v = A[i] ^ B[i] ^ u;
if (p != v) {
dfsSz(v, u);
sz[u] += sz[v];
}
}
}
string dfsString(int u, int p) {
ostringstream oss;
oss << "[" << u;
for (const int i : G[u]) {
const int v = A[i] ^ B[i] ^ u;
if (!del[v] && p != v) {
oss << " " << dfsString(v, u);
}
}
oss << "]";
return oss.str();
}
/// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// initial
vector<vector<Int>> css;
// [r][j] -> [*, *)
vector<vector<pair<int, int>>> pss;
// vertex -> ((r, (j, dis)))
vector<vector<pair<int, pair<int, int>>>> V;
// edge -> ((r, [*, *)))
vector<vector<pair<int, pair<int, int>>>> E;
int zeit;
void dfs(int r, int j, int u, int p, Int c) {
css[r].push_back(c);
V[u].emplace_back(r, make_pair(j, zeit));
++zeit;
for (const int i : G[u]) {
const int v = A[i] ^ B[i] ^ u;
if (!del[v] && p != v) {
const int lb = zeit;
dfs(r, j, v, u, c + C[i]);
const int ub = zeit;
E[i].emplace_back(r, make_pair(lb, ub));
}
}
}
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void solveSubtree(int depth, int r) {
#ifdef LOCAL
cerr << string(2 * depth, ' ') << "solveSubtree " << dfsString(r, -1) << endl;
#endif
vector<int> is, vs;
for (const int i : G[r]) {
const int v = A[i] ^ B[i] ^ r;
if (!del[v]) {
is.push_back(i);
vs.push_back(v);
}
}
const int len = vs.size();
/// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
css[r].push_back(0);
pss[r].resize(len);
zeit = 0;
V[r].emplace_back(r, make_pair(-1, zeit));
++zeit;
for (int j = 0; j < len; ++j) {
const int i = is[j], v = vs[j];
const int lb = zeit;
dfs(r, j, v, r, C[i]);
const int ub = zeit;
pss[r][j] = make_pair(lb, ub);
E[i].emplace_back(r, make_pair(lb, ub));
}
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
void solveRec(int depth, int u) {
for (; ; ) {
int vm = -1;
for (const int i : G[u]) {
const int v = A[i] ^ B[i] ^ u;
if (!del[v]) {
if (!~vm || sz[vm] < sz[v]) {
vm = v;
}
}
}
if (!~vm || 2 * sz[vm] <= sz[u]) {
solveSubtree(depth, u);
del[u] = 1;
for (const int i : G[u]) {
const int v = A[i] ^ B[i] ^ u;
if (!del[v]) {
solveRec(depth + 1, v);
}
}
break;
} else {
sz[u] -= sz[vm];
sz[vm] += sz[u];
u = vm;
}
}
}
void centroidDecomp() {
sz.assign(N, 0);
dfsSz(0, -1);
del.assign(N, 0);
css.assign(N, {});
pss.assign(N, {});
V.assign(N, {});
E.assign(N - 1, {});
solveRec(0, 0);
}
int main() {
int Q;
for (; ~scanf("%d%d", &N, &Q); ) {
A.resize(N - 1);
B.resize(N - 1);
C.resize(N - 1);
for (int i = 0; i < N - 1; ++i) {
scanf("%d%d%lld", &A[i], &B[i], &C[i]);
--A[i];
--B[i];
}
G.assign(N, {});
for (int i = 0; i < N - 1; ++i) {
G[A[i]].push_back(i);
G[B[i]].push_back(i);
}
centroidDecomp();
/*
cerr<<"css = "<<css<<endl;
cerr<<"pss = "<<pss<<endl;
cerr<<"V = "<<V<<endl;
cerr<<"E = "<<E<<endl;
*/
vector<RangeAddRangeCount<Int>> fs(N);
for (int u = 0; u < N; ++u) {
fs[u] = RangeAddRangeCount<Int>(css[u]);
}
for (; Q--; ) {
int O;
scanf("%d", &O);
if (O == 1) {
int I;
Int CC;
scanf("%d%lld", &I, &CC);
--I;
// cerr<<COLOR("33")<<"change "<<I<<" "<<C<<COLOR()<<endl;
const Int dC = CC - C[I];
C[I] = dC;
for (const auto &e : E[I]) {
fs[e.first].add(e.second.first, e.second.second, dC);
}
} else {
int U;
Int D;
scanf("%d%lld", &U, &D);
--U;
// cerr<<COLOR("93")<<"count "<<U<<" "<<D<<COLOR()<<endl;
int ans = 0;
for (const auto &e : V[U]) {
const int r = e.first;
const int j = e.second.first;
const int pos = e.second.second;
const Int c = fs[r][pos];
if (D >= c) {
int here = 0;
here += fs[r].count(0, fs[r].n, D - c + 1);
if (~j) here -= fs[r].count(pss[r][j].first, pss[r][j].second, D - c + 1);
ans += here;
}
}
printf("%d\n", ans);
}
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4024kb
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
Wrong Answer
time: 3554ms
memory: 299104kb
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:
wrong answer 385th numbers differ - expected: '2829', found: '2828'