QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#662682#9492. 树上简单求和ANIG0 3ms27436kbC++143.7kb2024-10-21 09:11:082024-10-21 09:11:10

Judging History

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

  • [2024-10-21 09:11:10]
  • 评测
  • 测评结果:0
  • 用时:3ms
  • 内存:27436kb
  • [2024-10-21 09:11:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int unsigned long long
const int N=2e5+5,T=150,B=500,M=3e7+5;
struct msg{
	signed bh,x,y,c;
	int z;
}g[M];
vector<msg>q1,q2;
signed lst[M],idx;
int n,m,f[N],w[N],rs[N],hd[N],laz[N],sm[N],st[N],ed[N],bh[N],lx[N],dfn[N],eds[N],dy1[N],dy2[N],idx1,idx2,mk[N],w1[N],w2[N],dep1[N],dep2[N],f1[N][20],f2[N][20];
void add(int x,msg y){
	g[++idx]=y;
	lst[idx]=hd[x];
	hd[x]=idx;
}
vector<int>p1[N],p2[N];
void dfs1(int x){
	for(int i=1;i<=18;i++)f1[x][i]=f1[f1[x][i-1]][i-1];
	mk[x]=1;
	dy1[++idx1]=x;
	for(auto c:p1[x]){
		if(mk[c])continue;
		f1[c][0]=x;
		dep1[c]=dep1[x]+1;
		dfs1(c);
	}
	mk[x]=0;
}
void dfs2(int x){
	for(int i=1;i<=18;i++)f2[x][i]=f2[f2[x][i-1]][i-1];
	mk[x]=1;
	dy2[++idx2]=x;
	dfn[x]=idx2;
	for(auto c:p2[x]){
		if(mk[c])continue;
		f2[c][0]=x;
		dep2[c]=dep2[x]+1;
		dfs2(c);
	}
	eds[x]=idx2;
	mk[x]=0;
}
int up1(int x,int k){
	while(k){
		x=f1[x][__lg(k&-k)];
		k^=k&-k;
	}
	return x;
}
int up2(int x,int k){
	while(k){
		x=f2[x][__lg(k&-k)];
		k^=k&-k;
	}
	return x;
}
int lca1(int a,int b){
	if(dep1[a]>dep1[b])swap(a,b);
	b=up1(b,dep1[b]-dep1[a]);
	if(a==b)return a;
	for(signed i=18;i>=0;i--){
		if(f1[a][i]!=f1[b][i])a=f1[a][i],b=f1[b][i];
	}
	return f1[a][0];
}
int lca2(int a,int b){
	if(dep2[a]>dep2[b])swap(a,b);
	b=up2(b,dep2[b]-dep2[a]);
	if(a==b)return a;
	for(signed i=18;i>=0;i--){
		if(f2[a][i]!=f2[b][i])a=f2[a][i],b=f2[b][i];
	}
	return f2[a][0];
}
void add(int bh,int x,int a,int b,int c,int z){
	add(x,{bh,a,b,c,z});
}
void add(int bh,int x,int y,int c1,int a,int b,int c2,int z){
	add(bh,x,a,b,c2,z);
	add(bh,y,a,b,c2,z);
	add(bh,c1,a,b,c2,-z);
	if(c1!=1)add(bh,f1[c1][0],a,b,c2,-z);
}
void solve(){
	for(auto c:q2){
		for(auto d:q1){
			if(d.bh>c.bh)break;
			add(c.bh,d.x,d.y,d.c,c.x,c.y,c.c,d.z);
		}
	}
	memset(w1,0,sizeof(w1));
	for(auto c:q1){
		w1[c.x]+=c.z;
		w1[c.y]+=c.z;
		w1[c.c]-=c.z;
		if(c.c!=1)w1[f1[c.c][0]]-=c.z;
	}
	for(int i=n;i>=1;i--){
		int x=dy1[i];
		for(auto c:p1[x]){
			if(c==f1[x][0])continue;
			w1[x]+=w1[c];
		}
	}
	for(int i=1;i<=n;i++)w[i]+=w1[i],w2[i]=w[i];
    for(int i=1;i<=n;i++){
    	int x=dy2[i];
		for(auto c:p2[x]){
		    if(c==f2[x][0])continue;
		    w2[c]+=w2[x];
		}
    }
	q1.clear();
	q2.clear();
}
void ADD(int l,int r,int y){
	for(int i=l;i<=r;i++)sm[i]+=y;
}
void add(int x,int y){
	int l=dfn[x],r=eds[x];
	if(bh[l]==bh[r]){
		ADD(l,r,y);
		return;
	}
	int ll=bh[l]+1,rr=bh[r]-1;
	for(int i=ll;i<=rr;i++)laz[i]+=y;
    ADD(l,st[ll]-1,y);ADD(ed[rr]+1,r,y);
}
int gets(int x){
	if(!x)return 0;
	return sm[dfn[x]]+laz[bh[dfn[x]]];
}
int gets(int a,int b,int c){
	return gets(a)+gets(b)-gets(c)-gets(f2[c][0]);
}
void dfs(int x){
	add(x,1);
	mk[x]=1;
	for(int i=hd[x];i;i=lst[i]){
		msg c=g[i];
		rs[c.bh]+=gets(c.x,c.y,c.c)*c.z;
	}
	for(auto c:p1[x]){
		if(mk[c])continue;
		dfs(c);
	}
	mk[x]=0;
	add(x,-1);
}
signed main(){
	freopen("mist02.in","r",stdin);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>w[i],bh[i]=(i-1)/B+1;
	for(int i=1;i<=bh[n];i++)st[i]=ed[i-1]+1,ed[i]=min(i*B,n);
	for(int i=1;i<n;i++){
		int x,y;
		cin>>x>>y;
		p1[x].push_back(y);
		p1[y].push_back(x);
	}
	for(int i=1;i<n;i++){
		int x,y;
		cin>>x>>y;
		p2[x].push_back(y);
		p2[y].push_back(x);
	}
	dfs1(1);
	dfs2(1);
	solve();
	for(int i=1;i<=m;i++){
		int x,y,z;
		cin>>x>>y>>z;
		q1.push_back({i,x,y,lca1(x,y),z});
		int c=lca2(x,y);
		q2.push_back({i,x,y,c});
		lx[i]=1;
		rs[i]=w2[x]+w2[y]-w2[c];
		if(c!=1)rs[i]-=w2[f2[c][0]];
		if(i%T==0)solve();
		if(i%1000==0)cerr<<i<<endl;
	}
	solve();
	dfs(1);
	for(int i=1;i<=m;i++)if(lx[i])cout<<rs[i]<<"\n";
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 26264kb

input:

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

output:


result:

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

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: 0ms
memory: 27436kb

input:

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

output:


result:

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

Subtask #5:

score: 0
Wrong Answer

Test #27:

score: 0
Wrong Answer
time: 0ms
memory: 26152kb

input:

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

output:


result:

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

Subtask #6:

score: 0
Wrong Answer

Test #34:

score: 0
Wrong Answer
time: 3ms
memory: 25776kb

input:

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

output:


result:

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

Subtask #7:

score: 0
Skipped

Dependency #1:

0%