HDU 5044 (树链剖分+树状数组+点/边改查)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5044
题目大意:修改链上点,修改链上的边。查询所有点,查询所有边。
解题思路:
2014上海网赛的变态树链剖分模板题。将以往树链剖分的点&边修改和查询合在一起之后,难度上去不少。
第一个卡人点是读入优化。
第二个卡人点是树状数组。由于要查询所有点,如果使用线段树,每次都要扫到底层才能取出点值,必T无疑。
然后使用树状数组之后,树链剖分的点/边修改写法有些变动。
点查询变化不大。
边查询只要查询一下dep(u,v)大的那一端。
具体变化请仔细看代码。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include "cstdio"
#include "cstring"
#include "iostream"
using namespace std;
#define maxn 100005
template <class T>
inline bool read(T &ret)
{
char c;
int sgn;
if(c=getchar(),c==EOF) return ; //EOF
while(c!='-'&&(c<''||c>'')) c=getchar();
sgn=(c=='-')?-:;
ret=(c=='-')?:(c-'');
while(c=getchar(),c>=''&&c<='') ret=ret*+(c-'');
ret*=sgn;
return ;
}
int s[maxn],dep[maxn],w[maxn],fa[maxn],top[maxn],son[maxn],tol,cnt,n,head[maxn];
struct Edge
{
int u,v,c;
}edge[maxn];
struct EDGE
{
int next,to;
}e[*maxn];
void addedge(int u,int v)
{
e[tol].to=v;
e[tol].next=head[u];
head[u]=tol++;
}
void dfs1(int u,int pre,int d)
{
s[u]=;fa[u]=pre;dep[u]=d;son[u]=-;
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].to;
if(v==pre) continue;
dfs1(v,u,d+);
s[u]+=s[v];
if(son[u]!=-||s[v]>s[son[u]]) son[u]=v;
}
}
void dfs2(int u,int tp)
{
w[u]=++cnt;top[u]=tp;
if(son[u]!=-) dfs2(son[u],tp);
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].to;
if(v!=son[u]&&v!=fa[u]) dfs2(v,v);
}
}
//Binary-Indexed-Tree type:0-point,1-edge
int psum[maxn],esum[maxn];
inline int lowbit(int x) {return x&(-x);}
int sum(int x,int type)
{
int ret=;
while(x>)
{
if(!type) ret+=psum[x];
else ret+=esum[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int d,int type)
{
while(x<=n)
{
if(!type) psum[x]+=d;
else esum[x]+=d;
x+=lowbit(x);
}
}
void p_Change(int x,int y,int v)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
add(w[top[x]],v,);
add(w[x]+,-v,);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
add(w[x],v,);
add(w[y]+,-v,);
}
void e_Change(int x,int y,int v)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
add(w[top[x]],v,);
add(w[x]+,-v,);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
if(x!=y) //这个判断非常重要,没有的话T死你
{
add(w[son[x]],v,);
add(w[y]+,-v,);
}
}
int main()
{
//freopen("in.txt","r",stdin);
int T,u,v,c,m,no=;
char cmd[];
read(T);
while(T--)
{
memset(head,-,sizeof(head));
memset(esum,,sizeof(esum));
memset(psum,,sizeof(psum));
tol=cnt=;
read(n);read(m);
for(int i=;i<n;i++)
{
read(edge[i].u);read(edge[i].v);
addedge(edge[i].u,edge[i].v);
addedge(edge[i].v,edge[i].u);
}
dfs1(,,);
dfs2(,);
for(int i=;i<n;i++)
if(dep[edge[i].u]<dep[edge[i].v]) swap(edge[i].u,edge[i].v);
while(m--)
{
scanf("%s",cmd);
read(u);read(v);read(c);
if(cmd[]=='') p_Change(u,v,c);
if(cmd[]=='') e_Change(u,v,c);
}
printf("Case #%d:\n",++no);
for(int i=;i<=n;i++) {if(i>=) printf(" ");printf("%d",sum(w[i],));}
printf("\n");
for(int i=;i<n;i++) {if(i>=) printf(" ");printf("%d",sum(w[edge[i].u],));}
printf("\n");
}
}
11824525 | 2014-10-08 17:34:00 | Accepted | 5044 | 3718MS | 11576K | 3384 B | C++ | Physcal |
HDU 5044 (树链剖分+树状数组+点/边改查)的更多相关文章
- hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...
- Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组
Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...
- 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释
P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- hdu 3966 Aragorn's Story(树链剖分+树状数组)
pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...
- HDU 3966 /// 树链剖分+树状数组
题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...
- HDU 3966 Aragorn's Story (树链剖分+树状数组)
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...
- bzoj1146整体二分+树链剖分+树状数组
其实也没啥好说的 用树状数组可以O(logn)的查询 套一层整体二分就可以做到O(nlngn) 最后用树链剖分让序列上树 #include<cstdio> #include<cstr ...
随机推荐
- mongo数据库的导入导出
http://www.iwangzheng.com/ [root@a02]$show dbs; changhong_tv_cms 0.078GB [root@a02]$ mongodump -d ch ...
- error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项:值“0”不匹配值“2”
error: vtkCommon.lib(vtkSmartPointerBase.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项:值“0”不 ...
- Rotate bitmap by real angle
tl;dr; Use GDI+ SetWorldTransform With WinAPI's SetWorldTransform you can transform the space of dev ...
- python 最佳入门实践
勿在浮沙筑高台,无论什么技术,掌握核心精神和api,是很重要的. 但是入门过程也可能不是一帆风顺的,这里有八个入门任务,看看你完成了没有: http://code.tutsplus.com/artic ...
- 海量数据导入MySQL的注意事项
对于千万行级别的数据,处理起来非常麻烦,例如有一个文件a.txt,大小超过2GB,共2000多万行,每行是一个新闻的相关信息,其中有一列为新闻标题,字符串型,新闻标题较长,现需要对新闻标题进行聚类,将 ...
- Find Leaves of Binary Tree
Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves ...
- django LDAP
> http://goodosoft.github.io/2015/02/25/Using-AD-as-authentication-for-Django/ > http://my.osc ...
- Django之表单字段的选填与后台界面的管理
参考: http://www.crazyant.net/1005.html http://gmingzhe.blog.51cto.com/810664/163051 所有的字段,默认blank=Fal ...
- MyBatis3: Could not find SQL statement to include with refid ‘
错误: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.Incompl ...
- iOS 中NSOperationQueue,Grand Central Dispatch , Thread的上下关系和区别
In OS X v10.6 and later, operation queues use the libdispatch library (also known as Grand Central D ...