QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#441147 | #5094. 小 H 分糖果 | Doqe | 0 | 0ms | 0kb | C++17 | 4.2kb | 2024-06-14 13:16:51 | 2024-06-14 13:16:52 |
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef __int128 lll;
int n,q;
struct node
{
int c;long long v;lll v2;inline bool any()const{return c||v;}
node operator+(const node&s)const{return {c+s.c,v+s.v,v2+s.v2};}
node operator-(const node&s)const{return {c-s.c,v-s.v,v2-s.v2};}
}tr[N];
void mdf(int k,const node&v){while(k<=n)tr[k]=tr[k]+v,k+=k&(-k);}
void mdf(int l,int r,int k){int sgn=k<0?-1:1;mdf(l,{sgn,k,1ll*sgn*k*k}),mdf(r+1,{-sgn,-k,-1ll*sgn*k*k});}
node ask(int k){node ans={0,0,0};while(k)ans=ans+tr[k],k-=k&(-k);return ans;}
struct mes{int t,x,k;}qx[N*4];
lll ans[N];
struct awa{int t,u,v,l,lc,rc;long long rm;}qy[N],t1[N];
vector<int>to[N];
int dfn[N],sz[N],DP[N],tx,ty,a[N],tt,fa[N],son[N],top[N];
void dfs(int u,int f)
{
DP[u]=DP[fa[u]=f]+1;sz[u]=1;
for(int v:to[u])if(v!=f)
{
dfs(v,u),sz[u]+=sz[v];
if(sz[son[u]]<sz[v])son[u]=v;
}
}
void dfs1(int u,int f)
{
top[u]=f;dfn[u]=++tt;if(son[u])dfs1(son[u],f);
for(int v:to[u])if(v!=fa[u]&&v!=son[u])dfs1(v,v);
}
int LCA(int u,int v)
{
while(top[u]!=top[v])
{
if(DP[top[u]]<DP[top[v]])swap(u,v);
u=fa[top[u]];
}
return DP[u]<DP[v]?u:v;
}
void solve(int l,int r,int lx,int rx,int ly,int ry)
{
if(ly>ry)return;
if(l==r)
{
for(int i=ly;i<=ry;++i)
{
auto&A=qy[i];
int d=A.lc;
ans[A.t]-=(lll)A.rm*(l+l-1);
}
return;
}
int mid=l+r>>1;
int px=lx,py1=ly,py2=ry,px1=lx,px2=rx;
sort(qx+lx,qx+rx+1,[&](const mes&A,const mes&B){return A.t<B.t;});
sort(qy+ly,qy+ry+1,[&](const awa&A,const awa&B){return A.t<B.t;});
for(int i=ly;i<=ry;++i)
{
while(px<=rx&&qx[px].t<=qy[i].t)
{
const auto&A=qx[px];
if(abs(A.k)>mid)
{
int u=A.x;
mdf(dfn[u],dfn[u]+sz[u]-1,A.k);
}
++px;
}
auto&A=qy[i];
node z= ask(dfn[A.u])+ask(dfn[A.v])-
ask(dfn[A.l])-ask(dfn[fa[A.l]]);
if(A.rm>=1ll*(r-mid)*A.rc+z.v-1ll*mid*z.c)
{
ans[A.t]-=(lll)(1ll*r*r-1ll*mid*mid)*A.rc+z.v2-(lll)z.c*mid*mid;
A.rm-=1ll*(r-mid)*A.rc+z.v-1ll*mid*z.c;A.rc+=z.c;t1[py1++]=A;
}
else
{
A.lc=A.rc+z.c;
t1[py2--]=A;
}
}
for(int i=lx;i<px;++i)
{
const auto&A=qx[i];
if(abs(A.k)>mid)
{
int u=A.x;
mdf(dfn[u],dfn[u]+sz[u]-1,-A.k);
}
}
sort(qx+lx,qx+rx+1,[&](const mes&A,const mes&B){return abs(A.k)<abs(B.k);});
px1=lx;while(px1<=rx&&abs(qx[px1].k)<=mid)++px1;
for(int i=ly;i<=ry;++i)qy[i]=t1[i];
solve(l,mid,lx,px1-1,ly,py1-1);
solve(mid+1,r,px1,rx,py1,ry);
}
void print(lll x)
{
if(x>9)print(x/10);
putchar(x%10+'0');
}
int main()
{
freopen("PanzerSupply.in","r",stdin);
freopen("PanzerSupply.out","w",stdout);
cin.tie(0)->sync_with_stdio(0);
cin>>n>>q;
for(int i=1,u,v;i<n;++i)cin>>u>>v,to[u].push_back(v),to[v].push_back(u);
for(int i=1;i<=n;++i)cin>>a[i],qx[++tx]={0,i,a[i]};dfs(1,0);dfs1(1,1);
for(int i=1;i<=n;++i)mdf(dfn[i],dfn[i]+sz[i]-1,qx[i].k);
memset(ans,-1,sizeof ans);
int mx=0;
lll S=0;
for(int i=1;i<=n;++i)S+=1ll*a[i]*a[i];
for(int i=1;i<=q;++i)
{
int o,u,v,x;
long long m;
cin>>o;
if(o==1)
{
cin>>u>>x;mx=max({mx,a[i],x});
qx[++tx]={i,u,-a[u]};
S-=1ll*a[u]*a[u];
mdf(dfn[u],dfn[u]+sz[u]-1,qx[tx].k);
qx[++tx]={i,u,x};
S+=1ll*x*x;
mdf(dfn[u],dfn[u]+sz[u]-1,qx[tx].k);
a[u]=x;
}
else
{
cin>>u>>v>>m;int l=LCA(u,v);
auto z= ask(dfn[u])+ask(dfn[v])-
ask(dfn[l])-ask(dfn[fa[l]]);
m=min(m,z.v);ans[i]=S;
qy[++ty]={i,u,v,l,z.c,0,m};
}
}
memset(tr,0,sizeof tr);
solve(0,mx,1,tx,1,ty);
for(int i=1;i<=q;++i)if(ans[i]!=-1)print(ans[i]),putchar('\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%