QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#718905#9492. 树上简单求和jjh201007300 5039ms63168kbC++142.9kb2024-11-06 21:45:182024-11-06 21:45:18

Judging History

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

  • [2024-11-06 21:45:18]
  • 评测
  • 测评结果:0
  • 用时:5039ms
  • 内存:63168kb
  • [2024-11-06 21:45:18]
  • 提交

answer

#include<bits/stdc++.h>
#define int unsigned long long
#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[rk1[dfn2[i]]]];
	return ans;
}
ull query(int l,int r) {return calc(r)-calc(l-1);}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr),cout.tie(nullptr);
	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<<'\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: 7ms
memory: 27324kb

input:

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

output:

12612623046134627989
7035685508839877758
5575426931485132980
12156935014863203247
13224759158765614281
15343885741584479210
13823714847888564127
5003306904392857445
7823561902712243059
9892694887781122348
16105581096339752479
5847582647486932880
17445076558103197611
16510354684413356813
603469313333...

result:

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

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: 5039ms
memory: 39836kb

input:

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

output:

9042998055336671259
2995490647871155589
8880026602228249211
9187084356907537870
14518950441018580424
9219186988735346339
10116706076532255540
12690879307813237987
5917253141596246012
7624506052035275047
4123355048479005697
1002496066594784074
7509879984029666240
7358780405470329225
27905324845430221...

result:

wrong answer 2nd lines differ - expected: '11611293489264521142', found: '2995490647871155589'

Subtask #5:

score: 0
Wrong Answer

Test #27:

score: 0
Wrong Answer
time: 2117ms
memory: 57468kb

input:

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

output:

11479812171669345085
7612644482907856514
7664363696211351499
10419050713553268082
7115244954460011045
9683711549165598600
15714069303067445091
5098969076555779384
17312050420753525411
13302302653999024684
15237835478514966949
1011923303415334401
15280591493481885526
11613220426756932450
109080667232...

result:

wrong answer 46th lines differ - expected: '3839081475788915102', found: '15548500711743103247'

Subtask #6:

score: 0
Wrong Answer

Test #34:

score: 0
Wrong Answer
time: 1512ms
memory: 63168kb

input:

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

output:

11082601764155624927
17943698850361276596
10771074320958783812
11047746330492802746
1946210316099059293
15665062496856138025
982094321057825497
7026106695763026701
3833321460079407785
2276685322252027636
5669392562626599691
10139309256527812835
13320838441559213372
15689108474330915507
5922132948166...

result:

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

Subtask #7:

score: 0
Skipped

Dependency #1:

0%