QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#717748#9492. 树上简单求和liujiameng0 3ms22172kbC++143.7kb2024-11-06 18:51:542024-11-06 18:51:55

Judging History

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

  • [2024-11-06 18:51:55]
  • 评测
  • 测评结果:0
  • 用时:3ms
  • 内存:22172kb
  • [2024-11-06 18:51:54]
  • 提交

answer

#include<bits/stdc++.h>
#define ull unsigned long long
using namespace std;
const int N = 2e5 + 10, D = 18;
vector<int>g1[N],g2[N];
int n;

int sqn, dl[N], dr[N], dfn, l[N], r[N], bel[N], fa[N][D+1], dep1[N];
ull a1[N], a2[N];
void dfs(int x, int f)
{
	dl[x] = ++dfn, fa[x][0] = f, dep1[x] = dep1[f] + 1;
	for(int i=1;i<=D;i++) fa[x][i] = fa[fa[x][i-1]][i-1];
	for(auto j:g1[x]) if(j!=f) dfs(j,x);
	dr[x] = dfn;
}
int LCA(int x, int y)
{
	if(dep1[x]>dep1[y]) swap(x,y);
	int i;
	for(i=D;i>=0;i--) if(dep1[fa[y][i]]>=dep1[x]) y = fa[y][i];
	if(x==y) return x;
	for(i=D;i>=0;i--) if(fa[x][i]!=fa[y][i]) x = fa[x][i], y = fa[y][i];
	return fa[x][0];
}
void init()
{
	dfs(1,0);
	sqn = (int)sqrt(n);
	int i, j;
	for(i=1;r[i-1]!=n;i++) l[i] = r[i-1] + 1, r[i] = min(l[i]+sqn-1,n);
	sqn = i - 1;
	for(i=1;i<=sqn;i++) for(j=l[i];j<=r[i];j++) bel[j] = i;
}
void add(int a, ull x)
{
	a = dl[a];
	int p = bel[a], i;
	for(i=a;i<=r[p];i++) a1[i] += x;
	for(i=p+1;i<=sqn;i++) a2[i] += x;
}
ull ask(int x)
{
	return a1[dr[x]] + a2[bel[dr[x]]] - a1[dl[x]-1] - a2[bel[dl[x]-1]];
}
ull ans[N], val[N];
struct qq{int id,x,k;}qu[N*4];int cq;
struct op{int x,id;ull y;}ad[N*4];int co;
void ins(int x, int id, ull y){add(x,y),ad[++co]={x,id,y};}

int cnt[N];
int dep[N], p[N], is[N], fa2[N], up[N];
void dfs2(int x, int f)
{
	dep[x] = dep[f] + 1, fa2[x] = f;
	for(auto j:g2[x]) if(j!=f) dfs2(j,x);
}
void init2()
{
	dfs2(1,0);
	int i;
	for(i=1;i<=n;i++) p[i] = i;
	sort(&p[1],&p[n]+1,[](int a,int b){return dep[a]>dep[b];});
	for(i=1;i<n;i++) if(!is[p[i]])
	{
		int x = p[i], y = fa2[x], z = sqn;
		while(y>1&&z)
		{
			if(is[y]) break;
			y = fa2[y], z--;
		}
		if(z) is[y] = 1;
	}
	is[1] = 1;
	for(i=2;i<=n;i++) if(is[i])
	{
		int x = fa2[x];
		while(!is[x]) x = fa2[x];
		up[i] = x;
	}
}

void dfsp(int x, int f)
{
	for(auto j:g2[x]) if(j!=f) cnt[j] += cnt[x], dfsp(j,x);
}

int main()
{
	int m, i, a, b;
	scanf("%d%d", &n, &m);
	for(i=1;i<=n;i++) scanf("%llu", &val[i]);
	for(i=1;i<n;i++) scanf("%d%d", &a, &b), g1[a].emplace_back(b), g1[b].emplace_back(a);
	for(i=1;i<n;i++) scanf("%d%d", &a, &b), g2[a].emplace_back(b), g2[b].emplace_back(a);
	init(), init2();
	for(i=1;i<=n;i++)
	{
		ins(i,0,val[i]);
		if(i>1) ins(fa[i][0],0,-val[i]);
	}
	for(i=1;i<=m;i++)
	{
		int x, y, xx, yy, l;
		ull k;
		scanf("%d%d%llu", &x, &y, &k), xx = x, yy = y;
		l = LCA(x,y), ins(x,i,k), ins(y,i,k), ins(l,i,-k), ins(fa[l][0],i,-k);
		while(!is[x]) ans[i] += ask(x), x = fa2[x];
		while(!is[y]) ans[i] += ask(y), y = fa2[y];
		if(x==y)
		{
			ans[i] = 0;
			x = xx, y = yy;
			if(dep[x]>dep[y]) swap(x,y);
			while(dep[x]<dep[y]) ans[i] += ask(y), y = fa2[y];
			while(x!=y) ans[i] = ask(x) + ask(y), x = fa2[x], y = fa2[y];
			ans[i] += ask(x);
			continue;
		}
		qu[++cq] = {i,1,x}, qu[++cq] = {i,1,y};
		while(up[x]!=up[y])
		{
			if(dep[up[x]]>dep[up[y]]) x = up[x];
			else y = up[y];
		}
		qu[++cq] = {i,-1,x}, qu[++cq] = {i,-1,y};
		if(dep[x]>dep[y]) swap(x,y);
		while(dep[x]<dep[y]) ans[i] += ask(y), y = fa2[y];
		while(x!=y) ans[i] += ask(x) + ask(y), x = fa2[x], y = fa2[y];
		ans[i] += ask(x);
	}
	sort(&qu[1],&qu[cq]+1,[](qq a,qq b){return a.x<b.x;});
	for(i=1;i<=cq;)
	{
		int l = i, r = i, x, j;
		ull res = 0;
		while(r<=cq&&qu[r].x==qu[l].x) r++;
		r--, x = qu[l].x;
		for(i=1;i<=n;i++) cnt[i] = 0;
		for(i=x;i;i=fa2[i]) cnt[i] = 1;
		dfsp(1,0);
		sort(&qu[l],&qu[r]+1,[](qq a,qq b){return a.id<b.id;});
		for(i=l,j=1;i<=r;i++)
		{
			while(ad[j].id<=qu[i].id) res += ad[j].y * cnt[ad[j].x];
			ans[qu[i].id] += res * qu[j].k;
		}
		i = r + 1;
	}
	for(i=1;i<=m;i++) printf("%llu\n", ans[i]);
	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: 22172kb

input:

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

output:

2119529465275825075
13981100112264111231
13981100112264111231
13981100112264111231
10375275900430727164
13981100112264111231
17958970787349923428
15979021401466958803
6749374011864068531
13981100112264111231
10375275900430727164
8120980158966640742
13981100112264111231
17857642005059840516
139811001...

result:

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

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Runtime Error

Test #21:

score: 0
Runtime Error

input:

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

output:


result:


Subtask #5:

score: 0
Time Limit Exceeded

Test #27:

score: 0
Time Limit Exceeded

input:

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

output:


result:


Subtask #6:

score: 0
Time Limit Exceeded

Test #34:

score: 0
Time Limit Exceeded

input:

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

output:


result:


Subtask #7:

score: 0
Skipped

Dependency #1:

0%