QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#363131#6350. MITppipWA 3ms52496kbC++143.8kb2024-03-23 18:41:282024-03-23 18:41:29

Judging History

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

  • [2024-03-23 18:41:29]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:52496kb
  • [2024-03-23 18:41:28]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
constexpr int N(1e6);
using LL=long long;
vector<pair<int,LL>> e[N+5];
int fa[N+5],d[N+5],tp[N+5],son[N+5],sz[N+5];
int sum[N+5],st[N+5],TP;
int lb[N+5],tf[N+5],dn[N+5];
int tr[N+5],a[N+5],b[N+5];
int L[N+5],R[N+5],fz[N+5];
LL h[N+5];
void init(int u,int fa) {
	::fa[u]=fa;
	d[u]=d[fa]+1;
	sz[u]=1;
	static int lda{0};
	L[u]=++lda;
	fz[lda]=u;
	for (auto [v,w]:e[u]) {
		h[v]=h[u]+w;
		e[v].erase(find(e[v].begin(),e[v].end(),make_pair(u,w)));
		init(v,u);
		sz[u]+=sz[v];
		if (sz[v]>sz[son[u]]) son[u]=v;
	}
	R[u]=lda;
}
void upd(int x) {
	tr[x]=a[x];
	for (int y{fa[x]};tf[y]==x;y=lb[y]) tr[x]+=tr[y];
}
int build(int l,int r) {
	int x{st[r--]};
	lb[x]=st[l-1];
	if (l<=r) {
		int i{upper_bound(sum+l,sum+r+1,(sum[l-1]+sum[r]>>1))-sum};
		tf[build(l,i)]=x;
		if (i<r) tf[build(i+1,r)]=x;
	}
	return upd(x),x;
}
void hld(int u,int top) {
	tp[u]=top;
	dn[top]=u;
	st[++TP]=u;
	sum[TP]=sz[u]+sum[TP-1];
	if (son[u]) sum[TP]-=sz[son[u]],hld(son[u],top);
	else build(TP-(d[u]-d[top]),TP);
	for (auto [v,w]:e[u])
		if (v!=son[u]) hld(v,v);
	--TP;
}
namespace G {
	void modify(int u,LL k) {
		while (u) {
			a[u]+=k;b[tp[u]]+=k;
			for (int x{u};x;x=tf[x]) upd(x);
			u=fa[tp[u]];
		}
	}
}
int jump(int u,int sz) {
	while (b[tp[u]]<sz) u=fa[tp[u]];
	int x{dn[tp[u]]};
	while (sz>0)
		if (sz>=tr[x]) sz-=tr[exchange(x,lb[x])];
		else sz-=a[exchange(x,fa[x])];
	x=(tp[x]==tp[u]?son[x]:tp[u]);
	return d[u]<d[x]?u:x;
}
int LCA(int u,int v) {
	while (tp[u]^tp[v]) {
		if (d[tp[u]]<d[tp[v]]) swap(u,v);
		u=fa[tp[u]];
	}
	return d[u]<d[v]?u:v;
}
bool isson(int u,int v) { return L[u]<=L[v]&&L[v]<=R[u]; }
LL dis(int u,int v) {
	return h[u]+h[v]-2*h[LCA(u,v)];
}
int n;
int cen;
LL ans;
namespace S {
	LL t[N<<1],tg[N];
	void plant() {
		for (int i{1};i<=n;++i) t[i+n-1]=dis(fz[i],cen),ans+=t[i+n-1];
		for (int i{n-1};i>=1;--i) t[i]=min(t[i<<1],t[i<<1|1]);
	}
	void up(int p) {
		p+=n-1;
		while (p>>=1) t[p]=min(t[p<<1],t[p<<1|1])+tg[p];
	}
	int query() {
		int x{1};
		while (x<n) {
			if (t[x]==t[x<<1]+tg[x]) x<<=1;
			else x=x<<1|1;
		}
		return x-n+1;
	}
	void tag(int p,LL k) {
		t[p]+=k;
		if (p<n) tg[p]+=k;
	}
	void modify(int l,int r,LL v) {
		int l0{l},r0{r};
		for (l+=n-1,r+=n-1;l<=r;l>>=1,r>>=1) {
			if (l&1) tag(l++,v);
			if (~r&1) tag(r--,v);
		}
		up(l0);up(r0);
	}
}
namespace F {
	int t[N+5];
	void add(int x,LL k) {
		for (;x<=n;x+=x&-x) t[x]+=k;
	}
	int qry(int u) {
		int l{L[u]-1},r{R[u]},ans{0};
		while (r>l) ans+=t[r],r&=r-1;
		while (l>r) ans-=t[l],l&=l-1;
		return ans;
	}
	int kth(int k) {
		int x{0};
		for (int i{19};i>=0;--i)
			if ((x|1<<i)<=n&&t[x|1<<i]<k)
				k-=t[x|=1<<i];
		return fz[x+1];
	}
}
int centroid(int k) {
	int u{jump(F::kth(k/2+1),(k+1)/2)};
	if ((~k&1)&&cen&&(F::qry(u)==k/2||F::qry(jump(F::kth(1),k/2))==k/2)) return cen;
	else return u;
}
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	cin>>n;
	for (int i{1};i<n;++i) {
		int u,v,w;cin>>u>>v>>w;
		e[u].emplace_back(v,w);
		e[v].emplace_back(u,w);
	}
	init(1,0);hld(1,1);
	for (int i{1};i<=n;++i) G::modify(i,1),F::add(i,1);
	cen=centroid(n);
	S::plant();
	vector<LL> res;
	for (int i{n-1};i>=2;--i) {
		int u{fz[S::query()]};
		ans-=S::t[1];
		S::modify(L[u],L[u],1e18);
		G::modify(u,-1);
		F::add(L[u],-1);
		u=cen;
		int v{centroid(i)};
		if (u!=v) {
			LL d{dis(u,v)};
			ans-=d;
			if (isson(v,u)) {
				S::modify(L[u],R[u],2*d);
				S::tag(1,-d);
			} else if (isson(u,v)) {
				S::modify(L[v],R[v],-2*d);
				S::tag(1,d);
			} else {
				S::modify(L[u],R[u],d);
				S::modify(L[v],R[v],-d);
			}
			cen=v;
		}
		if (~i&1) res.push_back(ans);
	}
	copy(res.rbegin(),res.rend(),ostream_iterator<LL>(cout," "));
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

7
1 3 99
2 3 82
3 4 4
4 5 43
5 6 5
4 7 3

output:

181 280 287 

result:

ok 3 number(s): "181 280 287"

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 52104kb

input:

1000
328 12 58771429
881 504 17030494
847 622 20805817
12 922 58532669
879 929 52585738
495 726 27873950
855 17 37971674
797 941 76999719
702 610 719916
479 127 97829887
731 557 55300256
282 615 88802280
651 318 64099880
540 51 6547869
896 54 87823596
674 879 80674008
77 691 54802376
193 851 7869172...

output:

49376646832 98161052378 146867159995 195451242327 243883567800 292246985615 340514831975 387785045812 435877330541 483815483221 531688195262 579455370589 627124290289 674664925164 722040527899 769358351709 816553822418 863641913272 910573276428 957424515928 1004160803433 1050815067266 1097340223865 ...

result:

wrong answer 1st numbers differ - expected: '48829042195', found: '49376646832'