QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#695340#9492. 树上简单求和lmeowdn0 1312ms97256kbC++143.5kb2024-10-31 19:48:452024-10-31 19:48:46

Judging History

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

  • [2024-10-31 19:48:46]
  • 评测
  • 测评结果:0
  • 用时:1312ms
  • 内存:97256kb
  • [2024-10-31 19:48:45]
  • 提交

answer

//Shirasu Azusa 2024.09
#include<bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<typename T,typename U>
T ceil(T x, U y) {return (x>0?(x+y-1)/y:x/y);}
template<typename T,typename U>
T floor(T x, U y) {return (x>0?x/y:(x-y+1)/y);}
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}

#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
int read() {
	int x=0,w=1; char c=getchar(); 
	while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
	while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();} 
	return x*w;    
}

const int N=4e5+5;
ull n,Q,a[N],deg[N],g[1000][1000],s[N],t[N],w[N],pf[N];
int m,fa1[N],d1[N],p1[N],p2[N],d2[N],son1[N],son2[N],top1[N],top2[N],fa2[N],b1[N],b2[N],sz1[N],sz2[N],t1,t2,B;
vi e1[N],e2[N];

void dfs1(int u,int *fa,int *sz,int *son,vi *e) {
	for(int v:e[u]) if(v!=fa[u]) {
		fa[v]=u; pf[v]=pf[u]+a[v];
		dfs1(v,fa,sz,son,e);
		sz[u]+=sz[v];
		if(sz[son[u]]<sz[v]) son[u]=v;
	} sz[u]++;
}
void dfs2(int u,int tp,int *d,int *p,int *fa,int *son,int *top,vi *e,int &t) {
	//cout<<"DDD "<<u<<endl;
	p[++t]=u, d[u]=t, top[u]=tp;
	if(son[u]) dfs2(son[u],tp,d,p,fa,son,top,e,t);
	for(int v:e[u]) if(v!=fa[u]&&v!=son[u]) {
		dfs2(v,v,d,p,fa,son,top,e,t);
	}
}

void upd(int u,int v,int k) {
	int l=d1[u], r=d1[v]; if(l>r) swap(l,r);
	int pl=max(l/B,1), pr=min(m,r/B+1);
		rep(i,pl,pr) {
			int x=(i-1)*B+1, y=i*B;
			if(l<=x&&y<=r) s[i]+=k;
			else {
				x=max(x,l), y=min(y,r);
				rep(j,x,y) {
					int u=p1[j];
					t[b2[u]]+=k;
					w[u]+=k;
				}
			}
		}
}

int qry(int u,int v) {
	int l=d2[u], r=d2[v]; if(l>r) swap(l,r); int ans=0;
		int xl=(l-1)/B+2, xr=(r-1)/B;
		if(xl<=xr) {
			rep(i,1,m) {
				int ww=(g[i][xr]-g[i][xl-1]);
				ww=ww*s[i]; ans+=ww;
			}
			rep(i,xl,xr) ans+=t[i];
			int p=(xl-1)*B, q=xr*B+1;
			rep(i,l,p) ans+=w[p2[i]]+s[b1[p2[i]]];
			rep(i,q,r) ans+=w[p2[i]]+s[b1[p2[i]]];
		} else {
			rep(i,l,r) ans+=w[p2[i]]+s[b1[p2[i]]];
		}
	return ans;
}

signed main() {
	n=read(), Q=read(), B=sqrt(n);
	rep(i,1,n) a[i]=read();
	rep(i,2,n) {
		int u=read(), v=read();
		e1[u].eb(v), e1[v].eb(u);
	}
	dfs1(1,fa1,sz1,son1,e1);
	dfs2(1,1,d1,p1,fa1,son1,top1,e1,t1);
	rep(i,2,n) {
		int u=read(), v=read();
		e2[u].eb(v), e2[v].eb(u);
	}
	pf[1]=a[1]; dfs1(1,fa2,sz2,son2,e2);
	dfs2(1,1,d2,p2,fa2,son2,top2,e2,t2);
	//rep(i,1,n) cout<<p1[i]<<" "; puts("");
	//rep(i,1,n) cout<<p2[i]<<" "; puts("");
	rep(i,1,n) b1[i]=(d1[i]-1)/B+1;
	rep(i,1,n) b2[i]=(d2[i]-1)/B+1;
	rep(i,1,n) g[b1[i]][b2[i]]++;
	//rep(i,1,n) cout<<d2[i]<<" "; puts("");
	m=n/B+1;
	rep(i,1,m) rep(j,1,m) g[i][j]+=g[i][j-1];
	rep(i,1,Q) {
		int u=read(), v=read(), k=read();
		int x=u,y=v;
		while(top1[u]!=top1[v]) {
			if(d1[top1[u]]<d1[top1[v]]) swap(u,v);
			upd(top1[u],u,k); u=fa1[top1[u]];
		} upd(u,v,k);
		u=x, v=y; int ans=0;
		while(top2[u]!=top2[v]) {
			if(d2[top2[u]]<d2[top2[v]]) swap(u,v);
			//cout<<u<<" "<<v<<" "<<top2[u]<<" "<<top2[v]<<endl;
			ans+=qry(top2[u],u); u=fa2[top2[u]];
		} ans+=qry(u,v);
		int l=(d2[u]<d2[v]?u:v);
		ans+=pf[x]+pf[y]-pf[l]-pf[fa2[l]];
		//cout<<l<<" "<<pf[x]+pf[y]-pf[l]-pf[fa2[l]]<<endl;
		printf("%llu\n",ans);
	}
	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: 43676kb

input:

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

output:

1893166980
3777759685
1865147222
1201676360
778010838
369874791
2139078965
900803640
683225114
1717207258
71294631
2163220889
3657902292
4152625252
3256343724
3236576587
3739365960
349965035
3710494941
2468225074
2967714668
3575642600
3483371228
2200051571
3396417127
1321692522
3808822272
2178712755...

result:

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

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: 614ms
memory: 68820kb

input:

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

output:

3370383387
3261788086
1068694826
520459726
2161597345
1613772405
179806368
3769119739
2914358603
2176593969
490985073
2178819964
1839295904
2372569769
2770004262
1481772010
533115754
768507151
2855574685
2628580755
2676234863
290944082
3535324787
2209813624
127202824
684229668
1757443811
2065551530
...

result:

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

Subtask #5:

score: 0
Wrong Answer

Test #27:

score: 0
Wrong Answer
time: 1312ms
memory: 89748kb

input:

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

output:

4102984509
3708771970
2517882827
3860819314
2155526693
4082956168
1093961571
282004792
1856662179
39389740
1522180517
268617217
3604475734
3676848994
841643108
3557666365
3673961894
2791810734
3279277125
743332662
1222098519
2951070266
837740942
272441601
655679266
8608156
1902664398
1329888899
1692...

result:

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

Subtask #6:

score: 0
Wrong Answer

Test #34:

score: 0
Wrong Answer
time: 706ms
memory: 97256kb

input:

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

output:

1702905249
2883934499
1651659045
3502263693
1246800515
3546770883
1871135994
2727261263
4266472144
3927847583
4202637589
706118127
238511120
625221329
11894940
3963298623
1662003018
3953998125
4143691714
317797138
1062708047
1192085023
2672432124
3329650551
496146923
657072695
1118850647
994910276
3...

result:

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

Subtask #7:

score: 0
Skipped

Dependency #1:

0%