XXI Open Cup. Grand Prix of Korea I. Query On A Tree 17 树剖 + 二分 + 树带权重心

传送门

文章目录

题意:

给你一棵树,每棵树初始权值都为 0 0 0,现在给你两个操作:

( 1 ) (1) (1) u u u的子树权值全部加 1 1 1

( 2 ) (2) (2) ( u , v ) (u,v) (u,v)路径上的点权值都加 1 1 1

每次输出一个点 x x x,满足 ∑ y = 1 N a [ y ] ∗ d i s ( x , y ) \sum_{y=1}^Na[y]*dis(x,y) y=1Na[y]dis(x,y)最小,如果多个点相同,输出深度最小的点。

思路:

观察这个式子,发现就是一个带权重心的问题,如果不在重心显然向重心走更优,需要支持修改。

这个题有一个结论:深度最小的带权重心的子树带权和一定 > > >总和的一半。

用反证法来证明,假设这个不满足,即非这个子树的权值和,一定 ≥ \ge 权值和的一半,显然向其父亲节点走之后,会使得减少至少一半的权值,加上不到一半的权值,会更优。

考虑如何找到这样的一个点,显然我们可与从头 d f s dfs dfs每个点,判断是否合法,这个复杂度 O ( n ) O(n) O(n)

但是根据题面,我们显然要打一个树剖,这就跟 d f s dfs dfs序扯上关系了,所以考虑能否将上面的问题转换到 d f s dfs dfs上,用线段树来维护呢?

假设当前权值总和为 s u m sum sum,考虑在线段树上二分一个前缀 ≤ s u m 2 + 1 \le\frac{sum}{2}+1 2sum+1的最大位置,将 d f s dfs dfs映射到点,假设为 p o s pos pos,此时满足了什么呢?满足 p o s pos pos子树的权值总和 ≤ s u m 2 + 1 \le \frac{sum}{2}+1 2sum+1,让后我们再向上倍增找到答案即可。

写完就过有被爽到。。

// Problem: I. Query On A Tree 17
// Contest: Codeforces - XXI Open Cup. Grand Prix of Korea
// URL: https://codeforces.com/gym/102759/problem/I
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n;
int dfn[N],se[N],fa[N][20],top[N],son[N],tot,depth[N];
int inv[N];
LL sum;
vector<int>v[N];
struct Node {
	int l,r;
	LL sum,lazy;
}tr[N<<2];

void dfs1(int u,int f) {
	fa[u][0]=f; depth[u]=depth[f]+1; se[u]=1;
	for(int i=1;i<=18;i++) fa[u][i]=fa[fa[u][i-1]][i-1];
	for(auto x:v[u]) {
		if(x==f) continue;
		dfs1(x,u);
		se[u]+=se[x];
		if(se[x]>se[son[u]]) son[u]=x;
	}
}

void dfs2(int u,int t) {
	top[u]=t; dfn[u]=++tot;
	inv[tot]=u;
	if(son[u]) dfs2(son[u],t);
	for(auto x:v[u]) {
		if(x==fa[u][0]||x==son[u]) continue;
		dfs2(x,x);
	}
}

void pushup(int u) {
	tr[u].sum=tr[L].sum+tr[R].sum;
}

void pushdown(int u) {
	LL lazy=tr[u].lazy; tr[u].lazy=0;
	tr[L].sum+=Len(L)*lazy; tr[L].lazy+=lazy;
	tr[R].sum+=Len(R)*lazy; tr[R].lazy+=lazy;
}

void build(int u,int l,int r) {
	tr[u]={l,r};
	if(l==r) return;
	build(L,l,Mid); build(R,Mid+1,r);
}

void change(int u,int l,int r) {
	if(tr[u].l>=l&&tr[u].r<=r) {
		tr[u].sum+=Len(u);
		tr[u].lazy++;
		return;
	}
	pushdown(u);
	if(l<=Mid) change(L,l,r);
	if(r>Mid) change(R,l,r);
	pushup(u);
}

LL query_sum(int u,int l,int r) {
	if(tr[u].l>=l&&tr[u].r<=r) return tr[u].sum;
	pushdown(u);
	LL ans=0;
	if(l<=Mid) ans+=query_sum(L,l,r);
	if(r>Mid) ans+=query_sum(R,l,r);
	return ans;
}

int query_k(int u,LL sum) {
	if(tr[u].l==tr[u].r) return inv[tr[u].l];
	pushdown(u);
	if(sum<=tr[L].sum) return query_k(L,sum);
	else return query_k(R,sum-tr[L].sum);
}

void update(int x,int y) {
	while(top[x]!=top[y]) {
		if(depth[top[x]]<depth[top[y]]) swap(x,y);
		change(1,dfn[top[x]],dfn[x]);
		x=fa[top[x]][0]; 
	}	
	if(depth[x]>depth[y]) swap(x,y);
	change(1,dfn[x],dfn[y]); 
}

int solve() {
	LL now=tr[1].sum/2+1;
	int pos=query_k(1,now);
	if(query_sum(1,dfn[pos],dfn[pos]+se[pos]-1)>=now) return pos;
	for(int i=18;i>=0;i--) 
		if(fa[pos][i]&&query_sum(1,dfn[fa[pos][i]],dfn[fa[pos][i]]+se[fa[pos][i]]-1)<now) 
			pos=fa[pos][i];
	return fa[pos][0];
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);	

	scanf("%d",&n);
	for(int i=1;i<=n-1;i++) {
		int a,b; scanf("%d%d",&a,&b);
		v[a].pb(b); v[b].pb(a);
	}
	dfs1(1,0); dfs2(1,1);
	build(1,1,n);
	int q; scanf("%d",&q);
	while(q--) {
		int op,l,r;
		scanf("%d%d",&op,&l);
		if(op==1) change(1,dfn[l],dfn[l]+se[l]-1);
		else scanf("%d",&r),update(l,r);
		printf("%d\n",solve());
	} 




	return 0;
}
/*

*/









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值