QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#718895#9492. 树上简单求和jjh201007300 4453ms63392kbC++142.9kb2024-11-06 21:42:572024-11-06 21:43:04

Judging History

This is the latest submission verdict.

  • [2024-11-06 21:43:04]
  • Judged
  • Verdict: 0
  • Time: 4453ms
  • Memory: 63392kb
  • [2024-11-06 21:42:57]
  • Submitted

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[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;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 27288kb

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: 4453ms
memory: 39804kb

input:

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

output:

9042998055336671259
5553010738509324117
8880026602228249211
9187084356907537870
14518950441018580424
9219186988735346339
8837946031213171276
12690879307813237987
5917253141596246012
17019504083170556475
13518353079614287125
1002496066594784074
2294414077412648844
16753778436605610653
313378447310403...

result:

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

Subtask #5:

score: 0
Wrong Answer

Test #27:

score: 0
Wrong Answer
time: 2097ms
memory: 57348kb

input:

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

output:

14176322116820800649
16035748856758534064
10074575887450631157
14626148319068161656
14790657027704433139
3815318942768257984
13074526658836050612
13682841757279070090
2136447312938119595
13353458651355306607
10788870154295770929
12697909779100767145
13092150329643330343
15471713778611592102
19414445...

result:

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

Subtask #6:

score: 0
Wrong Answer

Test #34:

score: 0
Wrong Answer
time: 1686ms
memory: 63392kb

input:

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

output:

11082601764155624927
9063925851944523874
14651422278619661612
200054101083571672
5689827896232631705
8124852469593492303
15524239212259456753
7026106695763026701
11727024048718782631
14893876321126357460
6539639157155050691
2129110296232957413
6967315953327954845
15689108474330915507
129987506288075...

result:

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

Subtask #7:

score: 0
Skipped

Dependency #1:

0%