QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#74750 | #5094. 小 H 分糖果 | zuytong | 0 | 6ms | 10044kb | C++14 | 6.9kb | 2023-02-03 19:40:29 | 2023-02-03 19:40:31 |
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 << 6], rs[N << 6], rt[N << 1];
LL tr[N << 6], ttag[N << 6]; __int128 sum[N << 6], stag[N << 6]; int cnt[N << 6], ctag[N << 6];
inline void ud(int now)
{
tr[now] = tr[ls[now]] + tr[rs[now]];
sum[now] = sum[ls[now]] + sum[rs[now]];
cnt[now] = cnt[ls[now]] + cnt[rs[now]];
}
inline void dn(int now, int l, int r)
{
int mid = (l + r) >> 1;
if(ttag[now])
{
if(!ls[now]) ls[now] = ++tot2;
if(!rs[now]) rs[now] = ++tot2;
tr[ls[now]] += ttag[now] * (mid - l + 1), ttag[ls[now]] += ttag[now];
tr[rs[now]] += ttag[now] * (r - mid), ttag[rs[now]] += ttag[now];
ttag[now] = 0;
}
if(stag[now])
{
if(!ls[now]) ls[now] = ++tot2;
if(!rs[now]) rs[now] = ++tot2;
sum[ls[now]] += stag[now] * (mid - l + 1), stag[ls[now]] += stag[now];
sum[rs[now]] += stag[now] * (r - mid), stag[rs[now]] += stag[now];
stag[now] = 0;
}
if(ctag[now])
{
if(!ls[now]) ls[now] = ++tot2;
if(!rs[now]) rs[now] = ++tot2;
cnt[ls[now]] += ctag[now] * (mid - l + 1), ctag[ls[now]] += ctag[now];
cnt[rs[now]] += ctag[now] * (r - mid), ctag[rs[now]] += ctag[now];
ctag[now] = 0;
}
}
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 * (r - l + 1);
ttag[now] += ty * val;
sum[now] += (__int128)ty * val * val * (r - l + 1);
stag[now] += (__int128)ty * val * val;
cnt[now] += ty * (r - l + 1);
ctag[now] += ty;
return;
}
int mid = (l + r) >> 1; dn(now, l, r);
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);
ud(now);
}
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;
if(l == r)
{
nsum += tr[now] * ty;
nsum2 += sum[now] * ty;
ncnt += cnt[now] * ty;
return;
}
int mid = (l + r) >> 1; dn(now, l, r);
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;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 6ms
memory: 10044kb
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
Runtime Error
Test #6:
score: 0
Runtime Error
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%