hdu 4918
第一道树的点分治。
感谢:
http://blog.csdn.net/u013368721/article/details/40887575
首先,找出原图的重心(最大子树大小最小的点(如果作为根)),去掉它后原图就分成了若干的森林,再在对应的森林中找出重心,递归这个过程(只有一个点时返回)。
这样,我们就可以根据重心的所属关系,建立出一棵树,显然原图的重心就是这颗树的重心,每个重心对应一些点(就是去掉该重心前,该重心联通块中的所有点)。
容易看出,因为每次选的是重心,所以树每次至少会分成两块(最坏情况),考虑我们根据重心所属关系建立出的树,最多有O(logn)层,每层找重心总的复杂度是O(n)(因为每层重心对应的点是O(n)级别的),所以建立这个关系是O(nlogn)的复杂度。
应前辈的称呼,我们就叫建立出的这颗很多重心的树为重心树。
对于这道题,我们可以给重心树的每个节点建立一个树状数组,树状数组下标为到重心的距离,值为点的权值,将重心对应的点都放到这个树状数组中(因为最大距离就是该重心对应的点数-1,所以树状数组的大小就设成该重心对应的点数),因为一层对应的所有重心对应的点数最多为O(n),所以总共占用的空间为O(nlogn)。然后还要根据这个重心的不同子重心分开记录一下这个东西,用于去重。
#include <cstdio>
#include <vector>
#include <map>
#define N 100010
using namespace std; struct Stat {
int r, d, s;
Stat( int r, int d, int s ):r(r),d(d),s(s){}
};
struct Bit {
int n;
vector<int> vv;
void init( int n ) {
this->n = n;
vv.resize( n+ );
for( int t=; t<=n; t++ ) vv[t] = ;
}
void modify( int pos, int delta ) {
pos++;
for( int i=pos; i<=n; i+=i&-i )
vv[i]+=delta;
}
int query( int pos ) {
int rt = ;
pos++;
if( pos>n ) pos=n;
for( int i=pos; i; i-=i&-i )
rt += vv[i];
return rt;
}
}; int n, m;
vector<int> g[N];
vector<Stat> st[N];
map<int,Bit > bit[N];
int ww[N];
int vis[N], siz[N], anc[N], bac[N], dis[N];
int qu[N], beg, tail; void init( int n ) {
for( int i=; i<=n; i++ ) {
g[i].clear();
st[i].clear();
bit[i].clear();
vis[i] = false;
}
}
void build( int root ) {
qu[beg=tail=] = root;
anc[root] = root;
siz[root] = bac[root] = ;
while( tail>=beg ) {
int u=qu[beg++];
for( int t=; t<g[u].size(); t++ ) {
int v=g[u][t];
if( v==anc[u] || vis[v] ) continue;
qu[++tail] = v;
anc[v] = u;
siz[v] = bac[v] = ;
}
}
for( int i=tail; i; i-- ) {
int v=qu[i];
int u=anc[v];
siz[v]++;
siz[u]+=siz[v];
if( siz[v]>bac[u] ) bac[u]=siz[v];
}
for( int i=; i<=tail; i++ ) {
int u = qu[i];
if( siz[root]-siz[u]>bac[u] ) bac[u]=siz[root]-siz[u];
}
for( int i=; i<=tail; i++ ) {
int u = qu[i];
if( bac[u]<bac[root] ) root=u;
}
//---------------- found the core -------------------//
bit[root][].init( tail+ );
bit[root][].modify( , ww[root] );
st[root].push_back( Stat(root,,) );
vis[root] = true; anc[root] = root;
for( int t=; t<g[root].size(); t++ ) {
int sr = g[root][t];
if( vis[sr] ) continue;
qu[beg=tail=] = sr;
dis[sr] = ;
anc[sr] = root;
while( tail>=beg ) {
int u=qu[beg++];
for( int t=; t<g[u].size(); t++ ) {
int v=g[u][t];
if( vis[v] || v==anc[u] ) continue;
qu[++tail] = v;
dis[v] = dis[u]+;
anc[v] = u;
}
}
bit[root][sr].init( tail+ );
for( int i=; i<=tail; i++ ) {
int u=qu[i];
bit[root][].modify( dis[u], ww[u] );
bit[root][sr].modify( dis[u], ww[u] );
st[u].push_back( Stat(root,dis[u],sr) );
}
}
for( int t=; t<g[root].size(); t++ ) {
int v=g[root][t];
if( vis[v] ) continue;
build(g[root][t]);
}
}
void modify( int u, int d ) {
for( int t=; t<st[u].size(); t++ ) {
Stat &s = st[u][t];
bit[s.r][].modify( s.d, d );
if( s.s ) bit[s.r][s.s].modify( s.d, d );
}
}
int query( int u, int d ) {
int rt = ;
for( int t=; t<st[u].size(); t++ ) {
Stat &s = st[u][t];
if( d<s.d ) continue;
int v = bit[s.r][].query( d-s.d );
rt += v;
if( s.s ) {
v = bit[s.r][s.s].query( d-s.d );
rt -= v;
}
}
return rt;
} int main() {
while() {
if( scanf( "%d%d", &n, &m )!= ) return ;
init(n);
for( int i=; i<=n; i++ )
scanf( "%d", ww+i );
for( int i=,u,v; i<n; i++ ) {
scanf( "%d%d", &u, &v );
g[u].push_back( v );
g[v].push_back( u );
}
build();
for( int i=; i<=m; i++ ) {
char ch[];
int u, d;
scanf( "%s%d%d", ch, &u, &d );
if( ch[]=='!' ) {
modify( u, d-ww[u] );
ww[u] = d;
} else {
printf( "%d\n", query(u,d) );
}
}
}
}
hdu 4918的更多相关文章
- HDU 4918 Query on the subtree(动态点分治+树状数组)
题意 给定一棵 \(n\) 个节点的树,每个节点有点权.完成 \(q\) 个操作--操作分两种:修改点 \(x\) 的点权.查询与 \(x\) 距离小于等于 \(d\) 的权值总和. \(1 \leq ...
- 胡小兔的OI日志3 完结版
胡小兔的 OI 日志 3 (2017.9.1 ~ 2017.10.11) 标签: 日记 查看最新 2017-09-02 51nod 1378 夹克老爷的愤怒 | 树形DP 夹克老爷逢三抽一之后,由于采 ...
- hdu 4915 Parenthese sequence--2014 Multi-University Training Contest 5
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4915 Parenthese sequence Time Limit: 2000/1000 MS (Ja ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- 基于bootstrap物资管理系统后台模板——后台
链接:http://pan.baidu.com/s/1geKwVMN 密码:0utl
- MongoDB之安装和基本使用(一)
环境 ubuntu16.04 mongodb基本特点 MongoDB 是一个基于分布式 文件存储的NoSQL数据库;可以把MongoDB想象成一个大py字典. 模式自由 :可以把不同结构的文档存储在同 ...
- 71.Adam Taylor玩转MicroZed系列第82部分:简单通信接口第2部分
By Adam Taylor 从上周的博客开始,我们已经进入到Zedboard(而不是MicroZed)板上的OLED显示模块的编程了.然而在正式进入具体的OLED编程之前,我认为有必要验证我们是否已 ...
- SQLite3数据库的操作
数据库的操作 我们在这个项目中使用的是SQLITE3数据库软件. 通过使用SQLITE3进行创建数据库,创建表,插入记录,查询记录,更新记录,关闭数据库等操作来实现将相应的数据存入数据库中. 打开数据 ...
- juery给所有ID属性相同的div绑定一个事件
案例: <div id="div1">内容</div> <div id="div1">内容</div> < ...
- Python基础之杂货铺
字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-310 ...
- C++ 模版的优点和缺点
优点: 1. 灵活性, 可重用性和可扩展性; 2. 可以大大减少开发时间,模板可以把用同一个算法去适用于不同类型数据,在编译时确定具体的数据类型; 3. 模版模拟多态要比C++类继承实现多态效率要高, ...
- PyQt:eg4
import sys from PyQt4 import QtCore from PyQt4 import QtGui class Form(QtGui.QDialog): def __init__( ...
- ASP.NET Core 2.0 MVC 发布部署--------- Ubuntun 16.04 X64 具体操作
.Net Core 部署到Ubuntu 16.04 中的步骤 1.安装工具 1.apache 2..Net Core(dotnet-sdk-2.0) 3.Supervisor(进程管理工具,目的是服务 ...
- 转 proc文件
/proc 是一个伪文件系统, 被用作内核数据结构的接口, 而不仅仅是解释说明/dev/kmem. /proc 里的大多数文件都是只读的, 但也可以通过写一些文件来改变内核变量. 下面对整个 /pro ...