QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#320646#5094. 小 H 分糖果Harry271820 3ms4356kbC++143.9kb2024-02-03 19:25:232024-02-03 19:25:23

Judging History

你现在查看的是最新测评结果

  • [2024-02-03 19:25:23]
  • 评测
  • 测评结果:0
  • 用时:3ms
  • 内存:4356kb
  • [2024-02-03 19:25:23]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define i128 __int128
using namespace std;
struct edge{int v,nxt;}e[200005];
int cnt,h[100005],dep[100005],f[100005][20],dfn[100005],dfx,siz[100005]; 
int n,c[200005],a[100005],pos[100005],id[100005],tot,bl[200005],L[1005],R[1005];
int val0[505][505],num[505][100005],m,u,v;
ll val1[505][505];i128 val2[505][505];mt19937 rnd(19260817);
struct node{int op,x,y;ll z;}q[100005];
void add(int u,int v){e[++cnt]={v,h[u]};h[u]=cnt;}
void dfs(int u,int fa)
{
	dep[u]=dep[fa]+1;f[u][0]=fa;dfn[u]=++dfx;siz[u]=1;
	for(int i=1;(1<<i)<=n;i++)f[u][i]=f[f[u][i-1]][i-1];
	for(int i=h[u];i;i=e[i].nxt)
	{
		int v=e[i].v;
		if(v==fa)continue;
		dfs(v,u);siz[u]+=siz[v];
	}
}
int lca(int u,int v)
{
	if(dep[u]<dep[v])swap(u,v);
	for(int i=16;i>=0;i--)if(dep[f[u][i]]>=dep[v])u=f[u][i];
	if(u==v)return u;
	for(int i=16;i>=0;i--)if(f[u][i]!=f[v][i])u=f[u][i],v=f[v][i];
	return f[u][0];
}
int calc(int val,int x)
{
	if(c[a[x]]>=val)return c[a[x]]-val;
	else return 0;
}
ll solve(int val,int x)
{
	ll res=0;
	while(x&&!pos[x])
	{
		res+=calc(val,x);
		x=f[x][0];
	} 
	if(!x)return res;
	int p=upper_bound(c+1,c+tot+1,val)-c;
	for(int i=bl[p]+1;i<=bl[tot];i++)res+=val1[pos[x]][i]-1ll*val0[pos[x]][i]*val;
	for(int i=p;i<=R[bl[p]];i++)res+=1ll*num[pos[x]][i]*(c[i]-val);
	return res;
}
ll get(int val,int x)
{
	if(c[a[x]]>=val)return 1ll*val*val;
	else return 1ll*c[a[x]]*c[a[x]];
}
i128 getres(int val,int x)
{
	i128 res=0;
	while(x&&!pos[x])
	{
		res+=get(val,x);
		x=f[x][0];
	} 
	if(!x)return res;
	int p=upper_bound(c+1,c+tot+1,val)-c;
	for(int i=bl[p]+1;i<=bl[tot];i++)res+=(i128)val0[pos[x]][i]*val*val;
	for(int i=p;i<=R[bl[p]];i++)res+=(i128)num[pos[x]][i]*val*val;
	for(int i=L[bl[p]];i<p;i++)res+=(i128)num[pos[x]][i]*c[i]*c[i];
	for(int i=1;i<bl[p];i++)res+=val2[pos[x]][i];
	return res;
}
void write(i128 x)
{
	if(!x)return;
	write(x/10);
	cout<<char(x%10+'0');
}
int main()
{
	cin.tie(0)->sync_with_stdio(0);
	cin>>n>>m;
	for(int i=1;i<n;i++)
	{
		cin>>u>>v;
		add(u,v);add(v,u);
	}
	for(int i=1;i<=n;i++)cin>>a[i],c[++tot]=a[i];
	for(int i=1;i<=m;i++)
	{
		cin>>q[i].op;
		if(q[i].op==1)cin>>q[i].x>>q[i].y,c[++tot]=q[i].y;
		else cin>>q[i].x>>q[i].y>>q[i].z;
	}
	sort(c+1,c+tot+1);tot=unique(c+1,c+tot+1)-c-1;
	for(int i=1;i<=n;i++)a[i]=lower_bound(c+1,c+tot+1,a[i])-c;
	for(int i=1;i<=m;i++)if(q[i].op==1)q[i].y=lower_bound(c+1,c+tot+1,q[i].y)-c;
	for(int i=1;i<=n;i++)id[i]=i;
	shuffle(id+1,id+n+1,rnd);
	int B=sqrt(n),B2=sqrt(tot); 
	for(int i=1;i<=B;i++)pos[id[i]]=i;
	dfs(1,0);
	for(int i=1;i<=tot;i++)
	{
		bl[i]=(i-1)/B2+1;
		if(bl[i]!=bl[i-1])L[bl[i]]=i,R[bl[i-1]]=i-1;
	}
	R[bl[tot]]=tot;
	for(int i=1;i<=B;i++)
	{
		int x=id[i];
		while(x)
		{
			val0[i][bl[a[x]]]++;
			val1[i][bl[a[x]]]+=c[a[x]];
			val2[i][bl[a[x]]]+=1ll*c[a[x]]*c[a[x]];
			num[i][a[x]]++;x=f[x][0];
		}
	}
	for(int i=1;i<=m;i++)
	{
		if(q[i].op==1)
		{
			int x=q[i].x,w=q[i].y;
			for(int j=1;j<=B;j++)
			{
				if(dfn[x]<=dfn[id[j]]&&dfn[id[j]]<=dfn[x]+siz[x]-1)
				{
					val0[j][bl[a[x]]]--;
					val1[j][bl[a[x]]]-=c[a[x]];
					val2[j][bl[a[x]]]-=1ll*c[a[x]]*c[a[x]];
					num[j][a[x]]--;
					val0[j][bl[w]]++;
					val1[j][bl[w]]+=c[w];
					val2[j][bl[w]]+=1ll*c[w]*c[w];
					num[j][w]++;
				}
			}
			a[q[i].x]=q[i].y;
		}
		else 
		{
			int u=q[i].x,v=q[i].y,Lca=lca(u,v);
			int l=0,r=c[tot],pos=0;
			while(l<=r)
			{
				int mid=(l+r)>>1;
				if(solve(mid,u)+solve(mid,v)-2*solve(mid,Lca)+calc(mid,Lca)<=q[i].z)r=mid-1,pos=mid;
				else l=mid+1;
			}
			if(pos==0){cout<<0<<'\n';continue;}
			ll now=solve(pos,u)+solve(pos,v)-2*solve(pos,Lca)+calc(pos,Lca);
			i128 res=getres(pos,u)+getres(pos,v)-2*getres(pos,Lca)+get(pos,Lca);
			res-=(i128)(q[i].z-now)*(2*pos-1);
			if(!res)cout<<0<<'\n';
			else write(res),cout<<'\n';
		}
	}
	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: 3ms
memory: 4356kb

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:

9583803
13180285
17509104
2455597
13999981
4820571
17446478
2557426
2073697
6296998
2222887
11461155
18566527
7013592
12109416
16425750
8793427
9394786
13795216
20892214
14526998
18578913
7682249
9083307
18909129
16067125
4153603
3033979
6284873
8456379
13750189
17898990
14581028
8793094
10807969
19...

result:

wrong answer 1st lines differ - expected: '285125508', found: '9583803'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Time Limit Exceeded

Test #6:

score: 0
Time Limit Exceeded

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:

19188097639274284023930
15558433608684772336822
2125566937114548802323
18946181954274646745994
843581296228103831532
15109267916292161003441
19514528939528451013339
2380087557003660886469
17854833045961408365415
9540268533442803246630
236577072092958033989
20219298935847536346543
1395912479524452664...

result:


Subtask #4:

score: 0
Skipped

Dependency #1:

0%