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 ...
随机推荐
- ASP.NET EF 使用LinqPad 快速学习Linq
使用LinqPad这个工具可以很快学习并掌握linq[Language Integrated Query] linqPad官方下载地址:http://www.linqpad.net/ linqPad4 ...
- yii2-widget-fileinput英文文档翻译
源地址:http://plugins.krajee.com/file-input 该插件是为bootstrap开发的增强版h5文件上传插件,具有多文件预览,多文件选择等功能.该插件提供了基于boots ...
- ConcurrentHashMap分析
1.ConcurrentHashMap锁分段技术 ConcurrentHashMap使用锁分段技术,首先将数据分成一段一段地存储,然后给每一段数据配一把锁,当一 ...
- sql server 2008 r2 产品密钥
数据中心版:PTTFM-X467G-P7RH2-3Q6CG-4DMYBDDT3B-8W62X-P9JD6-8MX7M-HWK38==================================== ...
- localhost或127.0.0.1或192.168.1.*被转到129129.com上的问题
系统启动里会有个httpd的apache程序在运行,自启禁用掉后.windows下有个apache文件夹,干掉就可以. 个别GHOST XP程序里面会装这种流氓程序.
- count(*)与count(1)、count('xxx')等在使用语法方面的区别
语法方面: 区别就是:没有区别!!! “*”号是通配符: “*”号是通配符 “*”号是通配符 使用"*"号和使用其他数字和任意非字段字符在使用方面没有任何语法错误; 至于效率方面是 ...
- LINUX内核中的机制OOM
[概念] LINUX内核中有一个机制叫做OOM killer(Out Of Memery killer) 该机制监控内存占用过大,尤其是瞬间消耗大量内存的进程, 为了防止内存被耗尽,所以OOM kil ...
- Homestead在windows7 下的搭建
遇到的问题有 1.Homestead 的版本问题,教程git版本是 v5,最新是v7的,如果用最新,就要求vagrant的版本是 2.0的: 2.启动homestead后,出现 No input fi ...
- CGIC简明教程(转摘)
CGIC简明教程 本系列的目的是演示如何使用C语言的CGI库“CGIC”完成Web开发的各种要求. ********************************* 基础知识 1 ...
- python基础(7)--深浅拷贝、函数
1.深浅拷贝 在Python中将一个变量的值传递给另外一个变量通常有三种:赋值.浅拷贝.深拷贝 Python数据类型可氛围基本数据类型包括整型.字符串.布尔及None等,还有一种由基本数据类型作为最基 ...