QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#441098 | #5094. 小 H 分糖果 | yzy1 | 0 | 0ms | 0kb | C++23 | 9.7kb | 2024-06-14 12:46:19 | 2024-06-14 12:46:19 |
answer
#if defined(LOCAL)
// #define _GLIBCXX_DEBUG
#else
#define NDEBUG
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif
#include <bits/stdc++.h>
#if defined(LOCAL)
#define DBG_MACRO_NO_WARNING
#include <dbg.hpp>
#else
#define dbg(x...) (0)
#endif
using namespace std;
using ll = long long;
using i128 = __int128;
// #define int ll
#define rep(i, f, t) for (int i = (f), ed##i = (t); i <= ed##i; ++i)
#define re(i, t) rep (i, 1, t)
#define per(i, t, f) for (int i = (t), ed##i = (f); i >= ed##i; --i)
#define ste(i, f, t, s) for (int i = (f), ed##i = (t); i <= ed##i; i += s)
#define nxt(i, f, g) for (int i = g.h[f]; i; i = g.e[i].n)
#define umod(x) ((x) >= mo && ((x) -= mo))
#define dmod(x) ((x) < 0 && ((x) += mo))
#define y1 y1__
#define fio(x) (freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout))
template <class T, class E>
__attribute__((always_inline)) inline void up(T &x, E &&y) {
if (x < y) x = y;
}
template <class T, class E>
__attribute__((always_inline)) inline void down(T &x, E &&y) {
if (y < x) x = y;
}
inline std::ostream &operator<<(std::ostream &dest, __int128_t value) {
std::ostream::sentry s(dest);
if (s) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[128];
char *d = std::end(buffer);
do {
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while (tmp != 0);
if (value < 0) {
--d;
*d = '-';
}
int len = std::end(buffer) - d;
if (dest.rdbuf()->sputn(d, len) != len) {
dest.setstate(std::ios_base::badbit);
}
}
return dest;
}
const int N = 2e5 + 9;
int n, q, dfn[N], tim, fa[N][22], dep[N], sz[N], q2, Cnt[N], Cnt1[N], Ansp[N];
ll a[N];
i128 Ans2[N];
ll Ans1[N];
vector<ll> lsh;
template <class T>
struct Bit {
#define lb(x) ((x) & -(x))
T a[N];
inline void Add(int p, T x) {
while (p <= n) a[p] += x, p += lb(p);
}
inline T Ask(int p) {
T x = 0;
while (p) x += a[p], p -= lb(p);
return x;
}
};
Bit<int> bit1;
Bit<ll> biti;
Bit<i128> bita;
struct G {
int tot, h[N];
struct E {
int t, n;
} e[N];
inline void Add(int f, int t) { e[++tot] = {t, h[f]}, h[f] = tot; }
} g;
struct Q {
int tim, x, y, lca;
ll v;
} qq[N];
struct Op {
int tim, x;
bool fl;
int v;
};
vector<Op> op[N];
struct Oq {
int op, x;
ll y, z;
} oq[N];
inline void Dfs1(int f) {
dfn[f] = ++tim;
sz[f] = 1;
dep[f] = dep[fa[f][0]] + 1;
nxt (i, f, g) {
int t = g.e[i].t;
if (t == fa[f][0]) continue;
fa[t][0] = f;
re (j, 20)
if (!(fa[t][j] = fa[fa[t][j - 1]][j - 1])) break;
Dfs1(t);
sz[f] += sz[t];
}
}
inline int Lca(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
per (i, 20, 0)
if (dep[fa[y][i]] >= dep[x]) y = fa[y][i];
if (x == y) return x;
per (i, 20, 0)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
struct Pr {
int id, tim, x, f;
};
inline i128 Calc(ll x, ll cnt, ll rem) {
assert(cnt >= 0);
if (cnt == 0) return 0;
ll val = max(0ll, x - rem / cnt);
int cntval1 = val == 0 ? 0 : rem % cnt, cntval = cnt - cntval1;
// dbg(val, cntval1, cntval);
return i128(1) * cntval * val * val + i128(1) * cntval1 * (val - 1) * (val - 1);
}
inline void F(int l, int r, const vector<int> &vec) {
// dbg(l, r);
if (vec.empty()) return;
int mid = (l + r + 1) >> 1;
vector<Op> ops;
rep (i, mid, r) {
for (auto x : op[i]) ops.push_back(x);
}
// dbg(l, mid, r);
// dbg(ops.size());
sort(ops.begin(), ops.end(), [](const auto &x, const auto &y) { return x.tim < y.tim; });
vector<Pr> prs;
for (auto x : vec) {
Ans1[x] = 0;
if (r != (int)lsh.size())
Ans1[x] += 1ll * Cnt[x] * (lsh[r + 1 - 1] - lsh[mid - 1]);
else
assert(Cnt[x] == 0);
Cnt1[x] = 0;
prs.push_back({x, qq[x].tim, dfn[qq[x].x], 1});
prs.push_back({x, qq[x].tim, dfn[qq[x].y], 1});
prs.push_back({x, qq[x].tim, dfn[qq[x].lca], -1});
if (qq[x].lca != 1) prs.push_back({x, qq[x].tim, dfn[fa[qq[x].lca][0]], -1});
}
int p = 0;
for (auto [id, tim, x, f] : prs) {
while (p != (int)ops.size() && ops[p].tim <= tim) {
int ff = ops[p].fl ? 1 : -1;
bit1.Add(ops[p].x, ff);
// cerr << "Add " << ops[p].x << ' ' << ff << ' ' << ops[p].v << '\n';
biti.Add(ops[p].x, 1ll * ff * (lsh[ops[p].v - 1] - lsh[mid - 1]));
++p;
}
// cerr << "Ask " << x << ' ' << f << '\n';
Cnt1[id] += f * bit1.Ask(x);
Ans1[id] += 1ll * f * biti.Ask(x);
}
vector<int> vl, vr;
for (auto x : vec) {
if (Ans1[x] <= qq[x].v) {
Cnt[x] += Cnt1[x];
Ansp[x] = mid;
// dbg(qq[x].v);
qq[x].v -= Ans1[x];
// dbg(Ans1[x]);
// dbg(x, lsh[mid - 1], Cnt[x], qq[x].v);
Ans2[x] = Calc(lsh[mid - 1], Cnt[x], qq[x].v);
// dbg((ll)Ans2[x]);
vl.push_back(x);
} else {
vr.push_back(x);
}
}
rep (i, 0, p - 1) {
int ff = ops[i].fl ? 1 : -1;
bit1.Add(ops[i].x, -ff);
biti.Add(ops[i].x, -1ll * ff * (lsh[ops[i].v - 1] - lsh[mid - 1]));
}
if (l == r) return;
F(l, mid - 1, vl);
F(mid, r, vr);
}
struct Cop {
int id, tim, col, p, f;
};
vector<Cop> cop;
struct Cop2 {
int tim, col, f;
};
vector<Cop2> cop2;
inline void CDQ(int l, int r) {
if (l == r) return;
int mid = (l + r) >> 1;
CDQ(l, mid);
CDQ(mid + 1, r);
sort(cop.begin() + l, cop.begin() + mid + 1, [](const auto &x, const auto &y) {
if (x.col != y.col) return x.col > y.col;
return x.id < y.id;
});
sort(cop.begin() + mid + 1, cop.begin() + r + 1, [](const auto &x, const auto &y) {
if (x.col != y.col) return x.col > y.col;
return x.id < y.id;
});
// dbg(l, r);
int p = l;
rep (i, mid + 1, r) {
if (!cop[i].id) continue;
while (p != mid + 1 && cop[p].col >= cop[i].col) {
if (!cop[p].id) {
// cerr << "Add " << cop[p].p << ' ' << cop[p].f << ' ' << cop[p].col << '\n';
bita.Add(cop[p].p, i128(1) * cop[p].f * lsh[cop[p].col - 1] * lsh[cop[p].col - 1]);
}
++p;
}
i128 res = i128(1) * cop[i].f * bita.Ask(cop[i].p);
Ans2[cop[i].id] += res;
// cerr << "Ask " << cop[i].p << ' ' << res << '\n';
}
rep (i, l, p - 1) {
if (!cop[i].id)
bita.Add(cop[i].p, i128(-1) * cop[i].f * lsh[cop[i].col - 1] * lsh[cop[i].col - 1]);
}
}
signed main() {
#ifndef LOCAL
fio("PanzerSupply");
#endif
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> q;
re (i, n - 1) {
int f, t;
cin >> f >> t;
g.Add(f, t), g.Add(t, f);
}
re (i, n) cin >> a[i], lsh.push_back(a[i]);
Dfs1(1);
// cerr << "AAA\n";
re (i, q) {
cin >> oq[i].op;
if (oq[i].op == 1)
cin >> oq[i].x >> oq[i].y, lsh.push_back(oq[i].y);
else {
cin >> oq[i].x >> oq[i].y >> oq[i].z;
qq[++q2] = {i, oq[i].x, (int)oq[i].y, Lca(oq[i].x, oq[i].y), oq[i].z};
}
}
// cerr << "AAA\n";
// dbg(lsh.size());
sort(lsh.begin(), lsh.end());
lsh.erase(unique(lsh.begin(), lsh.end()), lsh.end());
re (i, n) {
// dbg(i);
a[i] = lower_bound(lsh.begin(), lsh.end(), a[i]) - lsh.begin() + 1;
op[a[i]].push_back({0, dfn[i], 1, (int)a[i]});
if (dfn[i] + sz[i] <= n) op[a[i]].push_back({0, dfn[i] + sz[i], 0, (int)a[i]});
cop.push_back({0, 0, (int)a[i], dfn[i], 1});
cop2.push_back({0, (int)a[i], 1});
if (dfn[i] + sz[i] <= n) cop.push_back({0, 0, (int)a[i], dfn[i] + sz[i], -1});
}
// cerr << "BBB\n";
re (i, q) {
if (oq[i].op == 1) {
oq[i].y = lower_bound(lsh.begin(), lsh.end(), oq[i].y) - lsh.begin() + 1;
ll &ai = a[oq[i].x];
op[ai].push_back({i, dfn[oq[i].x], 0, (int)ai});
if (dfn[oq[i].x] + sz[oq[i].x] <= n)
op[ai].push_back({i, dfn[oq[i].x] + sz[oq[i].x], 1, (int)ai});
cop.push_back({0, i, (int)ai, dfn[oq[i].x], -1});
cop2.push_back({i, (int)ai, -1});
if (dfn[oq[i].x] + sz[oq[i].x] <= n)
cop.push_back({0, i, (int)ai, dfn[oq[i].x] + sz[oq[i].x], 1});
ai = oq[i].y;
op[ai].push_back({i, dfn[oq[i].x], 1, (int)ai});
if (dfn[oq[i].x] + sz[oq[i].x] <= n)
op[ai].push_back({i, dfn[oq[i].x] + sz[oq[i].x], 0, (int)ai});
cop.push_back({0, i, (int)ai, dfn[oq[i].x], 1});
cop2.push_back({i, (int)ai, 1});
if (dfn[oq[i].x] + sz[oq[i].x] <= n)
cop.push_back({0, i, (int)ai, dfn[oq[i].x] + sz[oq[i].x], -1});
}
}
memset(Ans2, 0x3f, sizeof Ans2);
vector<int> vec(q2);
re (i, q2) vec[i - 1] = i;
F(1, lsh.size(), vec);
// re (i, q2) cerr << Ansp[i] << " \n"[i == edi];
// re (i, q2) cerr << Ans2[i] << " \n"[i == edi];
// cerr << "----------\n";
// exit(0);
int p = 0;
sort(cop2.begin(), cop2.end(), [](const auto &x, const auto &y) { return x.tim < y.tim; });
i128 Sum = 0;
re (i, q2) {
while (p != (int)cop2.size() && cop2[p].tim <= qq[i].tim) {
// cerr << "Add " << cop2[p].f << ' ' << cop2[p].col << '\n';
Sum += i128(1) * cop2[p].f * lsh[cop2[p].col - 1] * lsh[cop2[p].col - 1];
++p;
}
Ans2[i] += Sum;
// dbg((ll)Sum);
// dbg((ll)Ans2[i]);
cop.push_back({i, qq[i].tim, Ansp[i], dfn[qq[i].x], -1});
cop.push_back({i, qq[i].tim, Ansp[i], dfn[qq[i].y], -1});
cop.push_back({i, qq[i].tim, Ansp[i], dfn[qq[i].lca], 1});
if (qq[i].lca != 1) cop.push_back({i, qq[i].tim, Ansp[i], dfn[fa[qq[i].lca][0]], 1});
}
sort(cop.begin(), cop.end(), [](const auto &x, const auto &y) {
if (x.tim != y.tim) return x.tim < y.tim;
if (x.col != y.col) return x.col > y.col;
return x.id < y.id;
});
CDQ(0, cop.size() - 1);
re (i, q2) cout << Ans2[i] << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 0
Dangerous Syscalls
input:
866 841 1 864 4 153 9 559 10 410 11 336 12 417 14 666 18 241 21 184 22 849 23 40 25 783 26 189 28 329 29 216 31 864 34 581 40 131 42 625 45 744 47 723 50 633 51 447 52 454 53 88 55 619 60 259 62 680 67 126 72 371 73 742 80 196 81 536 82 647 85 254 87 172 88 489 93 708 94 227 95 340 96 7 7 91 97 594 ...
output:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Dangerous Syscalls
Test #6:
score: 0
Dangerous Syscalls
input:
87080 98363 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...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%