QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#74752 | #5094. 小 H 分糖果 | zuytong | 0 | 1141ms | 531996kb | C++14 | 5.4kb | 2023-02-03 19:47:03 | 2023-10-04 02:50:48 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define max(x...) max({x})
#define min(x...) min({x})
#define FOR(i, x, y) for(int i = (x); i <= (y); i++)
#define ROF(i, x, y) for(int i = (x); i >= (y); i--)
inline LL rd()
{
LL sign = 1, re = 0; char c = getchar();
while(!isdigit(c)){if(c == '-') sign = -1; c = getchar();}
while(isdigit(c)){re = re * 10 + (c - '0'); c = getchar();}
return sign * re;
}
const int N = 1e5 + 5;
__int128 Sum;
int n, q, a[N];
int dfn[N], tn[N], dcnt;
int sz[N], st[N][17], dep[N];
vector<int> e[N];
void dfs(int now, int fa)
{
dfn[now] = ++dcnt, tn[dcnt] = now;
st[now][0] = fa, sz[now] = 1, dep[now] = dep[fa] + 1;
FOR(i, 1, 16)
{
int la = st[now][i - 1];
if(!la) break;
st[now][i] = st[la][i - 1];
}
for(int to : e[now]) if(to != fa)
{
dfs(to, now);
sz[now] += sz[to];
}
}
inline int LCA(int x, int y)
{
if(dep[x] < dep[y]) swap(x, y);
ROF(i, 16, 0) if(dep[st[x][i]] >= dep[y])
x = st[x][i];
if(x == y) return x;
ROF(i, 16, 0) if(st[x][i] != st[y][i])
x = st[x][i], y = st[y][i];
return st[x][0];
}
struct Node {int ty, x, y; LL w;} que[N];
int ss[N << 1], m = 1;
unordered_map<int, int> mp;
namespace Seg
{
inline int lb(int x) {return x & (-x);}
int tot2;
int ls[N << 9], rs[N << 9], rt[N << 1];
LL tr[N << 9]; __int128 sum[N << 9]; int cnt[N << 9];
void modify_region(int &now, int l, int r, int L, int R, int val, int ty)
{
if(!now) now = ++tot2;
if(L <= l && r <= R)
{
tr[now] += 1ll * ty * val;
sum[now] += (__int128)ty * val * val;
cnt[now] += ty;
return;
}
int mid = (l + r) >> 1;
if(L <= mid) modify_region(ls[now], l, mid, L, R, val, ty);
if(mid < R) modify_region(rs[now], mid + 1, r, L, R, val, ty);
}
void modify(int id, int val, int ty)
{
int now = mp[val];
while(now <= m)
{
modify_region(rt[now], 1, n, id, id + sz[tn[id]] - 1, val, ty);
now += lb(now);
}
}
void query(int now, int l, int r, int id, int ty, LL &nsum, __int128 &nsum2, int &ncnt)
{
if(!now) return;
nsum += tr[now] * ty;
nsum2 += sum[now] * ty;
ncnt += cnt[now] * ty;
if(l == r) return;
int mid = (l + r) >> 1;
if(id <= mid) query(ls[now], l, mid, id, ty, nsum, nsum2, ncnt);
else query(rs[now], mid + 1, r, id, ty, nsum, nsum2, ncnt);
}
inline int erfen(int l, int r, LL w, LL sum, __int128 sum2, int cnt)
{
int re = r;
while(l <= r)
{
int mid = (l + r) >> 1;
if(sum - 1ll * mid * cnt <= w) re = mid, r = mid - 1;
else l = mid + 1;
}
return re;
}
void Div(int u, int v, int lca, int falca, LL w, __int128 &sub)
{
LL sum = 0; __int128 sum2 = 0; int cnt = 0;
int now = 0;
ROF(i, __lg(m), 0)
{
int nxt = now | (1 << i);
if(nxt < m)
{
int ncnt = 0; LL nsum = 0; __int128 nsum2 = 0;
query(rt[nxt], 1, n, u, 1, nsum, nsum2, ncnt);
query(rt[nxt], 1, n, v, 1, nsum, nsum2, ncnt);
query(rt[nxt], 1, n, lca, -1, nsum, nsum2, ncnt);
if(falca) query(rt[nxt], 1, n, falca, -1, nsum, nsum2, ncnt);
if(sum + nsum - 1ll * (ss[nxt]) * (cnt + ncnt) <= w)
sum += nsum, sum2 += nsum2, cnt += ncnt, now = nxt;
}
}
int val = erfen(ss[now + 1], ss[now], w, sum, sum2, cnt);
if(!val) return void(sub = Sum);
sub = sum2 - (__int128)cnt * val * val;
w -= sum - 1ll * cnt * val;
if(w) sub += (__int128)w * (2 * val - 1);
}
} using namespace Seg;
inline void print(__int128 x)
{
if(!x) return void(puts("0"));
int bit[30], cnt = 0;
while(x) bit[++cnt] = x % 10, x /= 10;
ROF(i, cnt, 1) putchar(bit[i] + '0');
puts("");
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
n = rd(), q = rd();
FOR(i, 2, n)
{
int u = rd(), v = rd();
e[u].emplace_back(v), e[v].emplace_back(u);
}
FOR(i, 1, n)
a[i] = rd(),
Sum += 1ll * a[i] * a[i],
ss[++m] = a[i];
FOR(i, 1, q)
{
int ty = rd(), x = rd(), y = rd();
if(ty == 1) que[i] = (Node){ty, x, y, 0}, ss[++m] = y;
else que[i] = (Node){ty, x, y, rd()};
}
sort(ss + 1, ss + 1 + m); m = unique(ss + 1, ss + 1 + m) - ss - 1;
reverse(ss + 1, ss + 1 + m); FOR(i, 1, m) mp[ss[i]] = i;
dfs(1, 0);
FOR(i, 1, n) modify(dfn[i], a[i], 1);
FOR(i, 1, q)
{
if(que[i].ty == 1)
{
modify(dfn[que[i].x], a[que[i].x], -1);
Sum += 1ll * que[i].y * que[i].y - 1ll * a[que[i].x] * a[que[i].x];
modify(dfn[que[i].x], a[que[i].x] = que[i].y, 1);
}
else
{
__int128 sub = 0;
int lca = LCA(que[i].x, que[i].y);
Div(dfn[que[i].x], dfn[que[i].y], dfn[lca], dfn[st[lca][0]], que[i].w, sub);
print(Sum - sub);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 5ms
memory: 13172kb
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:
285125508 285374449 285871392 285072359 284419704 284843737 284692039 284936099 285944374 285174668 285019779 284651455 287282253 287175619 284878507 285369672 284880507 285404741 284913527 286053317 288622563 286960150 287194443 288326074 286937403 287883097 288535226 288195055 288643208 288632989 ...
result:
wrong answer 175th lines differ - expected: '285080403', found: '0'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #6:
score: 0
Wrong Answer
time: 1141ms
memory: 531996kb
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:
27217464773998101198216 27222683135365131711066 27215685950441383375941 27221607244120669838311 27219047117137492446677 27222635053035794978138 27218848172360084265818 27217641965048032442370 27217075857038185043354 27219505943263517662069 27219987830714690994915 27216425553487126261338 272156845500...
result:
wrong answer 16641st lines differ - expected: '27252018049702134434859', found: '0'
Subtask #4:
score: 0
Skipped
Dependency #1:
0%