QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#137204 | #5020. 举办乘凉州喵,举办乘凉州谢谢喵 | ckiseki | 8 | 602ms | 97208kb | C++23 | 7.6kb | 2023-08-10 04:08:01 | 2023-08-10 04:08:04 |
Judging History
answer
// An AC a day keeps the doctor away.
// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#ifdef local
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(args...) qqbx(#args, args)
#define orange(args...) danb(#args, args)
using std::cerr;
template <typename ...T> void qqbx(const char *s, T ...args) {
int cnt = sizeof...(T);
((cerr << "\e[1;32m(" << s << ") = ("), ..., (cerr << args << (--cnt ? ", " : ")\e[0m\n")));
}
template <typename I> void danb(const char *s, I L, I R) {
cerr << "\e[1;32m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++L) cerr << (f++ ? ", " : "") << *L;
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) ((void)0)
#define orange(...) ((void)0)
#endif // local
#define all(v) begin(v),end(v)
using namespace std;
using lld = int64_t;
struct Centroid {
vector<vector<int>> &g;
vector<vector<pair<int, lld&>>> qs;
vector<int> vis, sz;
vector<int> dis;
Centroid(vector<vector<int>> &t_g) : g(t_g), qs(g.size()),
vis(g.size()), sz(g.size()), dis(g.size()) {
}
void solve() {
decomp(0, 0);
}
void add_query(int i, int d, lld &a) {
qs[i].emplace_back(d, a);
}
void get_size(vector<int> &s, int i) {
s.emplace_back(i);
vis[i] = true;
sz[i] = 1;
for (int j: g[i]) if (!vis[j]) {
get_size(s, j);
sz[i] += sz[j];
}
}
void dfs(vector<int> &s, int i) {
s.emplace_back(i);
vis[i] = true;
for (int j: g[i]) if (!vis[j]) {
dis[j] = dis[i] + 1;
dfs(s, j);
}
}
void gao(const vector<int> &vtx, int coef) {
int mx = 0;
for (int x: vtx) mx = max(mx, dis[x]);
vector<int> pre(mx + 1);
for (int x: vtx) {
pre[dis[x]] += 1;
mx = max(mx, dis[x]);
}
for (int i = 1; i <= mx; i++) pre[i] += pre[i - 1];
for (int x: vtx) {
for (auto &[d, p]: qs[x]) {
// #{ y | dis[x] + dis[y] <= d }
int cnt = d - dis[x] < 0 ? 0 : pre[min(mx, d - dis[x])];
p += coef * cnt;
}
}
}
void decomp(int i, int d) {
vector<int> s;
get_size(s, i);
int c = -1;
for (int j: s) {
if (sz[j] * 2 >= sz[i])
c = j;
vis[j] = false;
}
vis[c] = true;
vector<int> total;
vector<vector<int>> sub;
for (int j: g[c]) {
if (vis[j]) continue;
dis[j] = 1;
sub.emplace_back();
dfs(sub.back(), j);
for (int x: sub.back())
total.emplace_back(x);
}
dis[c] = 0;
total.emplace_back(c);
gao(total, 1);
for (const auto &t: sub) gao(t, -1);
for (int j: s) vis[j] = false;
vis[c] = true;
for (int j: g[c]) {
if (vis[j]) continue;
decomp(j, d + 1);
}
}
};
struct HLD {
vector<vector<int>> &g;
vector<vector<tuple<int, int, lld&>>> g_qs;
vector<vector<tuple<int, int, lld&>>> f_qs;
vector<int> vis, sz, mxs, top, pa, dep;
int dfc;
vector<int> tin, tou, ord;
int64_t totsum;
vector<int64_t> sum;
HLD(vector<vector<int>> &t_g) : g(t_g),
g_qs(g.size()), f_qs(g.size()),
vis(g.size()), sz(g.size()), mxs(g.size()), top(g.size()), pa(g.size()), dep(g.size()),
dfc(0), tin(g.size()), tou(g.size()), ord(g.size()),
totsum(0), sum(g.size() + 1) {
get_size(0, -1);
make_chain(0, -1, 0);
}
void get_size(int i, int f) {
sz[i] = 1; mxs[i] = -1;
for (int j: g[i]) {
if (j == f) continue;
pa[j] = i;
dep[j] = dep[i] + 1;
get_size(j, i);
sz[i] += sz[j];
if (mxs[i] == -1 || sz[mxs[i]] < sz[j])
mxs[i] = j;
}
}
void make_chain(int i, int f, int t) {
ord[dfc] = i;
tin[i] = dfc++;
top[i] = t;
if (mxs[i] != -1) make_chain(mxs[i], i, t);
for (int j: g[i]) {
if (j == f) continue;
if (j == mxs[i]) continue;
make_chain(j, i, j);
}
tou[i] = dfc;
}
void add_f_query(int u, int d, int coef, lld &ans) {
if (d < 0) return;
// ans += coef * (sz[u] - 1);
// f_qs[tou[u]-1].emplace_back(dep[u] + d + 1, -coef, ans);
// f_qs[tin[u]].emplace_back(dep[u] + d + 1, coef, ans);
for (int t = tin[u] + 1; t < tou[u]; t++) {
int v = ord[t];
if (dep[v] - dep[u] <= d) {
ans += coef;
}
}
}
void do_f_query(int n) {
return;
vector<int64_t> cnt(n);
for (int i = 0; i < n; i++) {
cnt[ dep[ord[i]] ] += sz[ord[i]];
for (auto &[d, coef, ans]: f_qs[i])
ans += coef * (d >= 0 && d < n ? cnt[d] : 0);
}
}
int add_query(int x, int y, int d, lld &ans) {
g_qs[x].emplace_back(d, 1, ans);
g_qs[y].emplace_back(d, 1, ans);
int org_x = x, org_y = y;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
int t = top[x];
add_f_query(t, d - 1, -1, ans);
add_f_query(mxs[pa[t]], d - 1, 1, ans);
x = pa[t];
}
int z = dep[x] < dep[y] ? x : y;
g_qs[z].emplace_back(d, -2, ans);
for (int w: {org_x, org_y})
if (w != z && mxs[w] != -1)
add_f_query(mxs[w], d - 1, 1, ans);
return z;
}
void dfs_g(int i, int f) {
totsum += sz[i];
for (int j: g[i]) {
if (j == f || j == mxs[i]) continue;
for (int t = tin[j]; t < tou[j]; t++) {
int u = ord[t];
sum[ dep[u] - dep[i] ] += sz[u];
}
}
for (auto &[d, coef, ans]: g_qs[i]) {
int64_t cur = totsum - dep[i] - sum[d + 1];
debug(ans, coef, cur);
ans += coef * cur;
}
for (int j: g[i]) {
if (j == f) continue;
dfs_g(j, i);
}
totsum -= sz[i];
for (int j: g[i]) {
if (j == f || j == mxs[i]) continue;
for (int t = tin[j]; t < tou[j]; t++) {
int u = ord[t];
sum[ dep[u] - dep[i] ] -= sz[u];
}
}
}
void solve() {
dfs_g(0, -1);
do_f_query(g.size());
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> g(n);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
int q;
cin >> q;
vector<lld> ans(q);
Centroid cent(g);
HLD hld(g);
for (int i = 0; i < q; i++) {
int x, y, d;
cin >> x >> y >> d;
--x, --y;
int z = hld.add_query(x, y, d, ans[i]);
cent.add_query(z, d, ans[i]);
debug(x+1, y+1, z+1);
// hld.add_query(z, d, -2, ans[i]);
// hld.add_query(x, d, 1, ans[i]);
// hld.add_query(y, d, 1, ans[i]);
}
cent.solve();
hld.solve();
for (int i = 0; i < q; i++)
cout << ans[i] << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 35ms
memory: 5040kb
input:
4988 1 2 1 3 1 4 4 5 1 6 3 7 3 8 5 9 4 10 6 11 3 12 9 13 11 14 8 15 11 16 1 17 7 18 8 19 18 20 7 21 10 22 15 23 18 24 4 25 24 26 9 27 23 28 3 29 3 30 30 31 11 32 18 33 2 34 32 35 29 36 29 37 19 38 9 39 6 40 25 41 16 42 31 43 6 44 42 45 32 46 27 47 32 48 44 49 7 50 10 51 24 52 46 53 30 54 46 55 39 56...
output:
7620 6603 14092 2594 1717 9385 8743 6702 13158 5289 3886 2429 12215 3802 11433 12136 12718 3036 12207 2281 5760 11537 12994 8262 2115 3409 13540 12094 2763 8127 2192 8836 3162 6220 2585 8602 4821 787 2821 10820 8165 2591 2595 11107 13119 4675 11536 12980 3945 2761 3148 1827 2849 6968 12774 10668 372...
result:
wrong answer 1st numbers differ - expected: '3226', found: '7620'
Subtask #2:
score: 8
Accepted
Test #9:
score: 8
Accepted
time: 439ms
memory: 65984kb
input:
199995 1 2 2 3 2 4 1 5 3 6 5 7 6 8 4 9 2 10 5 11 5 12 1 13 1 14 1 15 13 16 1 17 10 18 16 19 11 20 8 21 17 22 4 23 19 24 7 25 22 26 8 27 14 28 1 29 9 30 3 31 3 32 21 33 19 34 26 35 34 36 5 37 29 38 22 39 5 40 13 41 28 42 8 43 35 44 22 45 14 46 12 47 32 48 11 49 8 50 18 51 23 52 18 53 4 54 6 55 10 56 ...
output:
757 69428 2793 181264 91707 182 32496 199183 6399 15975 11640 119051 236 689 15 9532 41 36592 178936 56 45424 193403 90248 3417 949 68 34133 60471 199861 188090 75088 127 1 6 4 3 3 11 61157 199860 199153 155706 196287 192862 53742 51862 179199 428 196282 199989 3613 26 99007 134461 198159 20382 7518...
result:
ok 199996 numbers
Test #10:
score: 0
Accepted
time: 355ms
memory: 65744kb
input:
199993 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 8 16 8 17 9 18 9 19 10 20 10 21 11 22 11 23 12 24 12 25 13 26 13 27 14 28 14 29 15 30 15 31 16 32 16 33 17 34 17 35 18 36 18 37 19 38 19 39 20 40 20 41 21 42 21 43 22 44 22 45 23 46 23 47 24 48 24 49 25 50 25 51 26 52 26 53 27 54 2...
output:
22 31743 62 30 510 6079 94 24063 190 4079 382 30 62 12159 1022 2043 8063 62 4 3063 4079 30 254 46 10 22 6111 12159 16127 22 1 12031 1 94 382 766 4063 254 46 766 1022 62 766 1 22 46 30 8063 8063 254 3063 22 62 30 1 62 254 4 10 15871 1022 46 2039 6079 22 254 1022 16127 30 766 8127 14 14 10 46 1 62 406...
result:
ok 199995 numbers
Test #11:
score: 0
Accepted
time: 602ms
memory: 77584kb
input:
199993 25163 125238 125238 19096 19096 88864 88864 113505 113505 12722 12722 56225 56225 8736 8736 74926 74926 38529 38529 80231 80231 19719 19719 198784 198784 75213 75213 190174 190174 163340 163340 62363 62363 144160 144160 130912 130912 3919 3919 21218 21218 85281 85281 187312 187312 79930 79930...
output:
97095 57496 116181 132482 144412 69917 174677 96334 37980 80902 148979 22074 166530 153392 43419 163281 148526 62703 79363 199993 153733 152298 5085 156125 117973 61925 36346 95741 124148 102890 20093 5408 77598 176994 177809 169850 144418 43786 189237 69098 5167 199993 103575 105198 197612 38829 20...
result:
ok 199994 numbers
Test #12:
score: 0
Accepted
time: 513ms
memory: 97208kb
input:
200000 3219 118490 118490 61372 61372 74390 74390 88375 88375 63918 63918 37580 37580 33219 33219 170480 170480 81737 81737 153202 153202 93921 93921 149109 149109 88339 88339 167037 167037 67099 67099 58363 58363 6784 6784 109386 109386 11895 11895 14872 14872 65638 65638 43958 43958 181669 181669 ...
output:
59633 108235 72863 144596 90365 57521 48069 163045 124462 18633 39115 111210 59413 80420 86945 182373 99188 57011 148702 53778 132289 68037 69705 50797 91155 77852 27499 106082 87174 122445 78221 71755 10125 93101 163451 16175 104215 50130 81182 30091 44299 81429 91045 111890 73099 72278 74017 59177...
result:
ok 199992 numbers
Test #13:
score: 0
Accepted
time: 399ms
memory: 73036kb
input:
199992 1 2 1 3 2 4 2 5 4 6 1 7 5 8 5 9 3 10 6 11 4 12 4 13 6 14 5 15 9 16 11 17 17 18 13 19 18 20 14 21 18 22 17 23 21 24 19 25 25 26 25 27 27 28 21 29 20 30 29 31 23 32 31 33 33 34 34 35 28 36 33 37 28 38 38 39 30 40 33 41 33 42 38 43 41 44 44 45 44 46 41 47 45 48 41 49 47 50 49 51 42 52 50 53 44 5...
output:
40732 40074 40815 13444 89657 22422 23494 61358 102922 66209 62228 32272 77095 68562 27799 74336 45129 71632 68525 13022 71347 63735 92178 64200 90446 50728 83632 61441 43695 10496 35481 81587 75266 77943 56182 14188 46302 108160 84166 3192 52959 522 57676 28165 97982 15371 95012 3437 53633 49240 55...
result:
ok 199998 numbers
Test #14:
score: 0
Accepted
time: 333ms
memory: 84940kb
input:
199990 1 2 1 3 4 2 5 2 6 4 7 4 8 6 9 6 10 8 11 8 12 10 13 10 14 12 15 12 16 14 17 14 18 16 19 16 20 18 21 18 22 20 23 20 24 22 25 22 26 24 27 24 28 26 29 26 30 28 31 28 32 30 33 30 34 32 35 32 36 34 37 34 38 36 39 36 40 38 41 38 42 40 43 40 44 42 45 42 46 44 47 44 48 46 49 46 50 48 51 48 52 50 53 50...
output:
59224 71441 128293 104009 42824 136779 12532 93560 81095 108628 176617 63487 103752 175849 36193 178489 73311 34313 46423 75989 76344 145231 20076 127399 81148 17356 39455 99025 44904 3548 78503 135455 28 136931 58140 53161 33288 134084 67877 26048 51868 74301 139992 165315 126781 136117 28112 86333...
result:
ok 199996 numbers
Test #15:
score: 0
Accepted
time: 251ms
memory: 63908kb
input:
199997 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52 5...
output:
65710 203 400 304 388 328 441 19 417 49 311 329 193 71968 895 179 152645 282 7600 185 150933 150045 444 3693 56770 34420 8765 335 73751 4665 3 203 380 44479 392 356 113 373 46663 143312 234 265 386 378 339 184360 404 21904 9861 393 445 441 91 299 9 213 24586 65263 22235 121 35761 169 36121 435 40035...
result:
ok 199995 numbers
Test #16:
score: 0
Accepted
time: 601ms
memory: 66496kb
input:
199997 1 64008 2 4187 3 154904 4 191579 5 107782 6 29053 7 123085 8 191601 9 95335 10 164039 11 171804 12 145532 13 104884 14 19820 15 74055 16 14172 17 98802 18 144668 19 188276 20 173096 21 62815 22 133749 23 65035 24 161785 25 191028 26 84730 27 176488 28 173295 29 110316 30 121532 31 81037 32 81...
output:
135622 123942 47301 113894 93180 10469 9237 13166 2896 20323 182669 26483 168662 47202 7900 7785 129591 1577 17943 5638 16670 16980 32760 153668 394 142656 30298 1801 167880 25099 10860 39103 28660 158337 55 126816 57661 17387 11147 95051 3 13130 28040 163801 141 109445 110915 13173 56634 20527 6325...
result:
ok 199993 numbers
Subtask #3:
score: 0
Wrong Answer
Dependency #2:
100%
Accepted
Test #17:
score: 0
Wrong Answer
time: 551ms
memory: 65344kb
input:
199996 1 2 2 3 3 4 1 5 1 6 4 7 1 8 5 9 4 10 4 11 7 12 11 13 7 14 10 15 2 16 1 17 9 18 10 19 9 20 13 21 8 22 14 23 21 24 8 25 20 26 23 27 17 28 6 29 9 30 9 31 14 32 14 33 6 34 7 35 35 36 31 37 37 38 2 39 12 40 16 41 18 42 3 43 27 44 4 45 7 46 24 47 16 48 44 49 10 50 11 51 9 52 16 53 39 54 30 55 4 56 ...
output:
190372 676 8 494 70 5 180064 3 9256 4 7850 83 19850 100186 66815 28662 39647 66801 34 12 957 2552 7 68333 414 96 275 51 44 261 12925 1526 54676 1695 33 124103 54 12283 173383 11 37 21523 147541 70 50 5346 183404 67291 4330 21 172421 502 155118 113529 252 180 1 497 172594 1100 807 13628 39545 3737 75...
result:
wrong answer 1st numbers differ - expected: '190330', found: '190372'
Subtask #4:
score: 0
Time Limit Exceeded
Test #25:
score: 0
Time Limit Exceeded
input:
199991 1 2 2 3 3 4 3 5 5 6 3 7 1 8 8 9 8 10 10 11 1 12 1 13 13 14 4 15 12 16 13 17 17 18 8 19 3 20 9 21 16 22 10 23 1 24 7 25 6 26 12 27 4 28 21 29 27 30 30 31 21 32 19 33 20 34 17 35 7 36 13 37 24 38 37 39 30 40 31 41 15 42 9 43 32 44 41 45 18 46 38 47 8 48 35 49 13 50 35 51 47 52 35 53 48 54 44 55...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #1:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
0%