传送门

文章目录

  • 题意:
  • 思路:

题意:

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

(1)(1)(1)将uuu的子树权值全部加111。

(2)(2)(2)将(u,v)(u,v)(u,v)路径上的点权值都加111。

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

思路:

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

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

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

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

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

假设当前权值总和为sumsumsum,考虑在线段树上二分一个前缀≤sum2+1\le\frac{sum}{2}+1≤2sum​+1的最大位置,将dfsdfsdfs映射到点,假设为pospospos,此时满足了什么呢?满足pospospos子树的权值总和≤sum2+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;
}
/**/

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

  1. [XXI Open Cup.Grand Prix of Korea]Advertisement Matching

    Advertisement Matching 题解 首先,题目的 a , b a,b a,b匹配显然是一个很常见的贪心. 有结论,设 a a a的集合为 A A A,如果 ∀ S ⊆ A , ∑ a ...

  2. 2020-2021 Winter Petrozavodsk Camp, Belarusian SU Contest (XXI Open Cup, Grand Prix of Belarus)

    题目链接 C. Brave Seekers of Unicorns 给出一个好数组的定义: 1.1.1. 长度不为空 2.2.2. a[i]⨁a[i−1]⨁a[i−2]≠0a[i] \bigoplus ...

  3. [XXII Open Cup, Grand Prix of Korea M]Yet Another Range Query Problem

    Yet Another Range Query Problem 题解 首先,看到这道题,我们应该是比较容易联想到扫描线加线段树的. 我们考虑维护每个点作为左端点到当前扫到的这个点作为右端点之间区间的信 ...

  4. G-Lexicographically Minimum Walk[CF-Gym-102391][2019-2020 XX Open Cup, Grand Prix of Korea]

    原题传送门 题面 Lexicographically Minimum Walk time limit per test2 secondsmemory limit per test1024 megaby ...

  5. 2020-2021 ACM-ICPC, Asia Nanjing Regional Contest (XXI Open Cup, Grand Prix of Nanjing)

    M. Monster Hunter 树形背包dp dp[i][j][k] 表示结点i的子树中有j个节点存活且当前节点i的状态为(0/1) 转移方程: dp[x][i+j][0]=min(dp[x][i ...

  6. [XXI Open Cup,Grand Prix of Tokyo]Ascending Matrix

    Ascending Matrix 题解 首先,这道题是要求所有的数列是从上往下,从左往右,都是递增的. 首先从左往右递增,我们可以去考虑它的差分序列,每个点的值就是它前面差分序列的点数嘛. 从上往下递 ...

  7. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Korea

    A. Donut 扫描线+线段树. #include<cstdio> #include<algorithm> using namespace std; typedef long ...

  8. 2015-2016 XVI Open Cup, Grand Prix of Bashkortostan, SKB Kontur Cup Stage 2

    地址 Rank Solved A B C D E F G H I J K L M 72/213 8/13 O . O O . O . O O O . O Ø O: 当场通过 Ø: 赛后通过 .: 尚未 ...

  9. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of SPb

    A. Base $i - 1$ Notation 两个性质: $2=1100$ $122=0$ 利用这两条性质实现高精度加法即可. 时间复杂度$O(n)$. #include<stdio.h&g ...

最新文章

  1. 网站推广流量获取仍需寻找全新获取网站推广流量渠道
  2. 安全声明标记语言SAML2.0初探
  3. linux cp源码_为Linux的cp和mv命令添加进度条
  4. python卸载opencv_20.Windows python,opencv的安装与卸载
  5. mysql服务无法启动进程意外终止_mysql服务无法启动 1067 错误,进程意外终止
  6. 实验二、XSS和SQL注入
  7. python安装及配置
  8. Android四大组件-Broadcast Receiver
  9. 欧姆龙PLC数据读写工具。 支持FinsTCP实测好用打开欧姆龙PLC读写软件,输入IP地址和端口号
  10. 安全合规/ISO--1--ISO 27000系列标准介绍
  11. 基于Excel的股票回测
  12. MySQL子查询的优缺点_为什么MySQL不推荐使用子查询和join
  13. kotlin java 知乎_GitHub - luciferldy/ZhihuDailyKotlin: 这是是一个使用 Kotlin 开发的知乎日报客户端...
  14. 《左手数据,右手图表》
  15. 《算法竞赛》被评为清华大学出版社2022年度“十佳图书”
  16. c语言字符串怎么退位,C语言第五六次作业.ppt
  17. 如何使用plsql连接远程数据库
  18. 技术一般的程序员找工作,如今真的一年比一年难...
  19. 支持居者有其屋,支持房产税出台与落地。
  20. 【密码学-2】什么是椭圆曲线密码

热门文章

  1. python使用spark sql查询impala_如何使用JDBC将Impala表直接加载到Spark?
  2. 为什么手机最后 10% 的电量很不耐,最后1%的电量最耐用?
  3. What?你还搞不懂什么是物体检测?
  4. 120天的烧脑只为孩子设计一套教具~
  5. 从Google Maglev说起,如何造一个牛逼的负载均衡?
  6. 干货整理:处理不平衡数据的技巧总结!收好不谢
  7. $query php,phpQuery让php处理html代码像jQuery一样方便
  8. java号段_JAVA手机号正则(多号段)
  9. 通达信版弘历软件指标_通达信软件指标编写基础教程,10个指标源码祝你股市一帆风顺...
  10. php正则过滤html标签_空格_换行符的代码,PHP 正则过滤 html 标签、空格、换行符的代码 (文章格式化)...