QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#718880#9492. 树上简单求和jjh201007300 1173ms47828kbC++142.8kb2024-11-06 21:40:352024-11-06 21:40:35

Judging History

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

  • [2024-11-06 21:40:35]
  • 评测
  • 测评结果:0
  • 用时:1173ms
  • 内存:47828kb
  • [2024-11-06 21:40:35]
  • 提交

answer

#include<bits/stdc++.h>
#define ull unsigned long long
using namespace std;
const int N=2e5+5,M=500;
ull a[N],tag[M],sum[M][M],cnt[M];
int n,m,B,sz1[N],fa1[N],d1[N],son1[N],dfn1[N],rk1[N],tp1[N],cnt1=0,sz2[N],fa2[N],d2[N],son2[N],dfn2[N],rk2[N],tp2[N],cnt2=0,bl[N],L[M],R[M],head[N],ecnt=0;
struct edge {int to,nxt;}e[N<<1];
void add(int u,int v) {e[++ecnt]=(edge){v,head[u]};head[u]=ecnt;}
void dfs1(int u,int pre,int dep,int *sz,int *fa,int *d,int *son) {
	int mx=0;
	sz[u]=1,fa[u]=pre,d[u]=dep;
	for(int i=head[u]; i; i=e[i].nxt) {
		int v=e[i].to;
		if(v==pre) continue;
		dfs1(v,u,dep+1,sz,fa,d,son);
		sz[u]+=sz[v];
		if(sz[v]>mx) {
			mx=sz[v];
			son[u]=v;
		}
	}
}
void dfs2(int u,int top,int &cnt,int son[],int fa[],int *dfn,int *rk,int *tp) {
	dfn[++cnt]=u,rk[u]=cnt,tp[u]=top;
	if(!son[u]) return;
	dfs2(son[u],top,cnt,son,fa,dfn,rk,tp);
	for(int i=head[u]; i; i=e[i].nxt) {
		int v=e[i].to;
		if(v==son[u]||v==fa[u]) continue;
		dfs2(v,v,cnt,son,fa,dfn,rk,tp);
	}
}
void modify(int l,int r,int k) {
	int ll=bl[l-1]+1,rr=bl[r+1]-1;
	for(int i=ll; i<=rr; i++) tag[i]+=k;
	for(int i=l; i<L[ll]; i++) a[dfn1[i]]+=k,cnt[bl[rk2[dfn1[i]]]]+=k;
	for(int i=R[rr]+1; i<=r; i++) a[dfn1[i]]+=k,cnt[bl[rk2[dfn1[i]]]]+=k;
}
ull calc(int x) {
	ull ans=0;
	int xx=bl[x+1]-1;
	for(int i=1; i<=(n-1)/B+1; i++) ans+=tag[i]*sum[i][xx];
	for(int i=1; i<=xx; i++) ans+=cnt[i];
	for(int i=R[xx]+1; i<=x; i++) ans+=a[dfn2[i]]+tag[bl[i]];
	return ans;
}
ull query(int l,int r) {return calc(r)-calc(l-1);}
int main() {
	cin>>n>>m;B=sqrt(n);
	for(int i=1; i<=n; i++) cin>>a[i];
	for(int i=1; i<n; i++) {
		int u,v;
		cin>>u>>v;
		add(u,v),add(v,u);
	}
	dfs1(1,0,1,sz1,fa1,d1,son1);
	dfs2(1,1,cnt1,son1,fa1,dfn1,rk1,tp1);
	memset(head,0,sizeof(head)),ecnt=0;
	for(int i=1; i<n; i++) {
		int u,v;
		cin>>u>>v;
		add(u,v),add(v,u);
	}
	dfs1(1,0,1,sz2,fa2,d2,son2);
	dfs2(1,1,cnt2,son2,fa2,dfn2,rk2,tp2);
	for(int i=1; i<=n; i++) bl[i]=(i-1)/B+1;
	bl[0]=0,bl[n+1]=(n-1)/B+2;
	for(int i=1; i<=(n-1)/B+1; i++) L[i]=(i-1)*B+1,R[i]=min(i*B,n);
	for(int i=1; i<=(n-1)/B+1; i++) {
		for(int j=L[i]; j<=R[i]; j++) sum[i][bl[rk2[dfn1[j]]]]++;
		for(int j=1; j<=(n-1)/B+1; j++) sum[i][j]+=sum[i][j-1];
	}
	for(int i=1; i<=(n-1)/B+1; i++) for(int j=L[i]; j<=R[i]; j++) cnt[i]+=a[dfn2[j]];
	while(m--) {
		int x,y,k;
		cin>>x>>y>>k;
		int keepx=x,keepy=y;
		while(tp1[x]!=tp1[y]) {
			if(d1[tp1[x]]<d1[tp1[y]]) swap(x,y);
			modify(rk1[tp1[x]],rk1[x],k);
			x=fa1[tp1[x]];
		}
		if(d1[x]>d1[y]) swap(x,y);
		modify(rk1[x],rk1[y],k);
		ull ans=0;
		x=keepx,y=keepy;
		while(tp2[x]!=tp2[y]) {
			if(d2[tp2[x]]<d2[tp2[y]]) swap(x,y);
			ans+=query(rk2[tp2[x]],rk2[x]);
			x=fa2[tp2[x]];
		}
		if(d2[x]>d2[y]) swap(x,y);
		ans+=query(rk2[x],rk2[y]);
		cout<<ans<<endl;
	}
	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: 16228kb

input:

3000 3000
7236742292501328495 17973811477309806363 16075782662531676171 17971236571771878676 11392080645527132110 3685563455925680459 9773593720088356683 8313828403245053795 7736401634567449043 1634817828009987181 6951124933529719486 12775126714635387213 15460977209223753216 397573676785925632 31372...

output:

18084912179840619805
1175617982790387238
1175617995675289120
1175618008560191002
1175618021445092884
1175618034329994766
1175618047214896648
1175618060099798530
1175618072984700412
1175618085869602294
1175618098754504176
1175618111639406058
1175618124524307940
1175618137409209822
1175618150294111704...

result:

wrong answer 1st lines differ - expected: '12105153858659381124', found: '18084912179840619805'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Wrong Answer

Test #21:

score: 0
Wrong Answer
time: 915ms
memory: 24144kb

input:

200000 200000
622783158027686223 2242697872372232537 8481648430436878777 10092474834140799044 15403999682625301609 12614289513474949582 9180944589267018841 7823784919308285798 8257785171198951273 5134508521895120821 8041682272181381093 3835432206618893170 2653803171409877650 5589823419153460372 1007...

output:

6485477968993470025
3214897227015735261
3214897235605669849
3214897244195604437
3214897252785539025
3214897261375473613
3214897269965408201
3214897278555342789
3214897287145277377
3214897295735211965
3214897304325146553
3214897312915081141
3214897321505015729
3214897330094950317
3214897338684884905
...

result:

wrong answer 1st lines differ - expected: '9042998055336671259', found: '6485477968993470025'

Subtask #5:

score: 0
Wrong Answer

Test #27:

score: 0
Wrong Answer
time: 728ms
memory: 41820kb

input:

200000 200000
1958469220619413759 14991498002015735322 6054491201406941902 18206143187746582567 15082377615826460430 2936248617457291604 10073577150351675920 16534472678586906457 2207599132486246393 10301540360769075442 1492580560381080472 551692353431379140 13238280352539145808 8462626987240986565 ...

output:

3540333235425455155
16229511681237843394
16229549867792054348
16229588054346265302
16229626240900476256
16229664427454687210
16229702614008898164
16229740800563109118
16229778987117320072
16229817173671531026
16229855360225741980
16229893546779952934
16229931733334163888
16229969919888374842
1623000...

result:

wrong answer 1st lines differ - expected: '11479812171669345085', found: '3540333235425455155'

Subtask #6:

score: 0
Wrong Answer

Test #34:

score: 0
Wrong Answer
time: 1173ms
memory: 47828kb

input:

200000 200000
6794776813641982926 1561596256197101737 10910039723053043515 7892247858295192798 12233819960547881004 17695389034783066733 9173201689566865598 17626618141377486739 7358781671024283919 6787559733384974662 3884392438269280436 14872846228351316833 9037842441501571648 14299818404271084016 ...

output:

14296169351305268647
14296189718040176795
14296210084775084943
14296230451509993091
14296250818244901239
14296271184979809387
14296291551714717535
14296311918449625683
14296332285184533831
14296352651919441979
14296373018654350127
14296393385389258275
14296413752124166423
14296434118859074571
142964...

result:

wrong answer 1st lines differ - expected: '5519324519442957729', found: '14296169351305268647'

Subtask #7:

score: 0
Skipped

Dependency #1:

0%