QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#593572#3226. Distance Sumdongyc666WA 4ms18024kbC++142.4kb2024-09-27 14:43:232024-09-27 14:43:23

Judging History

This is the latest submission verdict.

  • [2024-09-27 14:43:23]
  • Judged
  • Verdict: WA
  • Time: 4ms
  • Memory: 18024kb
  • [2024-09-27 14:43:23]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int NR=2e5+10;
int n,dis[NR],fa[NR][20],dep[NR];
int L[NR],R[NR],dfn[NR],siz[NR],idx[NR],S;
#define pii pair<int,int>
#define fi first
#define se second
#define mkp make_pair
#define pb emplace_back
vector<pii>son[NR];
set<int>s;

void dfs(int id,int fath){
	dep[id]=dep[fath]+1;
	fa[id][0]=fath;siz[id]=1;
	dfn[id]=++S;idx[S]=id;
	L[id]=dfn[id];
	for(int i=1;i<=18;i++)fa[id][i]=fa[fa[id][i-1]][i-1];
	for(auto lcy:son[id]){
		dis[lcy.fi]=dis[id]+lcy.se;
		dfs(lcy.fi,id);
		siz[id]+=siz[lcy.fi];
	}
	R[id]=dfn[id]+siz[id]-1;
}
int LCA(int x,int y){
	if(dep[x]<dep[y])swap(x,y);
	for(int i=18;i>=0;i--)
		if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
	if(x==y)return x;
	for(int i=18;i>=0;i--)
		if(fa[x][i]!=fa[y][i])
			x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}
int getd(int x,int y){
	return dis[x]+dis[y]-2*dis[LCA(x,y)];
}
int jump(int x,int y){
	for(int i=18;i>=0;i--)
		if((y>>i)&1)x=fa[x][i];
	return x;
}

struct BIT{
	int c[NR];
	int lowbit(int x){
		return x&(-x);
	}
	void add(int x,int y){
		while(x<=n){
			c[x]+=y;
			x+=lowbit(x); 
		}
	}
	int ask(int x){
		int res=0;
		while(x){
			res+=c[x];
			x-=lowbit(x);
		}
		return res;
	}
	int calc(int l,int r){return ask(r)-ask(l-1);}
}T;

void Insert(int x){
	if(s.count(dfn[x]))return;
	auto it=s.lower_bound(dfn[x]);
	if(it==s.end()){
		s.insert(dfn[x]);	
		return;
	}
	int tmp=LCA(idx[*it],x);
	s.insert(dfn[tmp]);
	s.insert(dfn[x]);
}
int getfa(int x){
	auto it=s.lower_bound(dfn[x]);
	if(it==s.begin())return 0;
	it--;
	return LCA(idx[*it],x);
}
signed main(){
	cin>>n;
	for(int i=2,x,y;i<=n;i++)
		cin>>x>>y,son[x].pb(mkp(i,y));
	dfs(1,0);
	int res=0,rt=0;
	for(int i=1;i<=n;i++){
		if(!rt)rt=i;
		res+=getd(rt,i);
		Insert(i);T.add(dfn[i],1);
		if(L[rt]<=L[i]&&R[i]<=R[rt]&&i!=rt){
			int tmp=jump(i,dep[i]-dep[rt]-1);
			auto it1=s.lower_bound(L[tmp]),it2=s.lower_bound(R[tmp]);
			it2--;
			int tar=LCA(idx[*it1],idx[*it2]),val=T.calc(L[tar],R[tar]);
//			printf("i:%lld tmp:%lld tar:%lld val:%lld\n",i,tmp,tar,val);
			if(val*2>i){
				res-=(val*2-i)*(dis[tar]-dis[rt]);
				rt=tar;
			}
		}
		else{
			int val=T.calc(L[rt],R[rt]);
			if(val*2<i){
				int nxt=getfa(rt);
				res-=(i-val*2)*(dis[rt]-dis[nxt]);
				rt=nxt;
			}
		}
//		printf("i:%lld rt:%lld\n",i,rt);
//		for(int x:s)cout<<idx[x]<<' ';cout<<endl;
		cout<<res<<'\n';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 16008kb

input:

10
5 1
2 1
1 1
4 1
2 1
5 1
2 1
2 1
5 1

output:

0
3
4
6
6
8
9
11
12
14

result:

ok 10 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 17948kb

input:

10
5 1
10 1
5 1
3 1
5 1
5 1
1 1
3 1
1 1

output:

0
4
4
6
6
7
8
12
14
16

result:

ok 10 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 16148kb

input:

10
4 1
7 1
9 1
3 1
7 1
1 1
7 1
6 1
3 1

output:

0
5
6
9
11
12
12
13
15
17

result:

ok 10 lines

Test #4:

score: 0
Accepted
time: 4ms
memory: 17956kb

input:

10
1 1
9 1
9 1
6 1
9 1
3 1
10 1
1 1
3 1

output:

0
1
3
5
7
8
10
13
13
15

result:

ok 10 lines

Test #5:

score: 0
Accepted
time: 0ms
memory: 17904kb

input:

10
6 1
6 1
3 1
3 1
1 1
1 1
9 1
3 1
1 1

output:

0
2
3
5
6
7
9
12
13
16

result:

ok 10 lines

Test #6:

score: 0
Accepted
time: 3ms
memory: 18024kb

input:

10
8 1
10 1
6 1
3 1
1 1
1 1
4 1
5 1
4 1

output:

0
4
6
6
9
10
13
14
18
19

result:

ok 10 lines

Test #7:

score: -100
Wrong Answer
time: 0ms
memory: 17948kb

input:

10
5 1
8 1
9 1
1 1
1 1
5 1
5 1
1 1
1 1

output:

0
2
5
7
7
9
10
11
13
15

result:

wrong answer 3rd lines differ - expected: '4', found: '5'