Aragorn's Story HDU - 3966 -树剖模板
思路 :树链剖分就是可以把一个路径上的点映射成几段连续的区间上。这样对于连续的区间可以用线段树维护,
对于每一段连续的区间都可以通过top [ ]数组很快的找到这段连续区间的头。跳的过程类似于 lca ,但这里 要注意的是
每一个点只属于一条链。 (重载运算符时要注意 node 一个 新的结点,不要 乱用*this 指针)
- #include<bits/stdc++.h>
- using namespace std;
- #define MID int m = (l+r)/2
- #define maxn 56789
- #define inf 0x3f3f3f3f
- struct node
- {
- int sum,lazy,cnt;
- node()
- {
- sum=lazy=cnt=0;
- }
- node operator+(const node &a)const
- {
- node ret;
- ret.sum=a.sum+sum;
- ret.cnt=a.cnt+cnt;
- return ret;
- }
- } tree[maxn*4];
- char str[12];
- vector<int>edge[maxn];
- int data[maxn],n,m,id[maxn],fa[maxn],u,ans;
- int son[maxn],top[maxn],tid[maxn],cnt,v,ad;
- int deep[maxn],siz[maxn],id_data[maxn],q;
- void pushdown(int root)
- {
- if(tree[root].lazy==0)return ;
- tree[root*2].lazy+=tree[root].lazy;
- tree[root*2+1].lazy+=tree[root].lazy;
- tree[root*2].sum+=tree[root*2].cnt*tree[root].lazy;
- tree[root*2+1].sum+=tree[root*2+1].cnt*tree[root].lazy;
- tree[root].lazy=0;
- }
- void bulid(int root,int l,int r)
- {
- tree[root].lazy=0;
- if(l==r)
- {
- tree[root].sum=id_data[l];
- tree[root].cnt=1;
- return ;
- }
- MID;
- bulid(root*2,l,m);
- bulid(root*2+1,m+1,r);
- tree[root]=tree[root*2]+tree[root*2+1];
- }
- void updata(int root,int l,int r,int L,int R,int ad)
- {
- if(r<L||l>R)return;
- if(L<=l&&r<=R)
- {
- tree[root].sum+=tree[root].cnt*ad;
- tree[root].lazy+=ad;
- return ;
- }
- pushdown(root);
- MID;
- updata(root*2,l,m,L,R,ad);
- updata(root*2+1,m+1,r,L,R,ad);
- tree[root]=tree[root*2]+tree[root*2+1];
- }
- void query(int root,int l,int r,int L,int R)
- {
- if(r<L||l>R)return ;
- if(L<=l&&r<=R)
- {
- ans+=tree[root].sum;
- return;
- }
- pushdown(root);
- MID;
- query(root*2,l,m,L,R);
- query(root*2+1,m+1,r,L,R);
- }
- void dfs1(int u,int pre,int ide)
- {
- son[u]=-1,siz[u]=1;
- deep[u]=ide,fa[u]=pre;
- for(int i=0; i<edge[u].size(); i++)
- {
- int v=edge[u][i];
- if(v==pre)continue;
- dfs1(v,u,ide+1);
- if(son[u]==-1||siz[son[u]]<siz[v])
- son[u]=v;
- }
- }
- void dfs2(int u,int tp)
- {
- top[u]=tp, tid[u]=++cnt;
- id_data[cnt]=data[u];
- if(son[u]!=-1)dfs2(son[u],tp);
- for(int i=0; i<edge[u].size(); i++)
- {
- int v=edge[u][i];
- if(v==fa[u]||v==son[u])continue;
- dfs2(v,v);
- }
- }
- void solve(int x,int y,int ad)
- {
- int tx=top[x],ty=top[y];
- while(tx!=ty)
- {
- if(deep[tx]<deep[ty])swap(x,y),swap(tx,ty);
- updata(1,1,n,tid[tx],tid[x],ad);
- x=fa[tx],tx=top[x];
- }
- if(deep[x]<deep[y])swap(x,y);
- updata(1,1,n,tid[y],tid[x],ad);
- }
- int main()
- {
- while(~scanf("%d%d%d",&n,&m,&q))
- {
- cnt=0;
- for(int i=1; i<=n; i++)
- {
- scanf("%d",&data[i]);
- edge[i].clear();
- }
- while(m--)
- {
- scanf("%d%d",&u,&v);
- edge[u].push_back(v);
- edge[v].push_back(u);
- }
- dfs1(1,0,1);
- dfs2(1,1);
- bulid(1,1,n);
- while(q--)
- {
- scanf("%s",str);
- if(str[0]=='I')
- {
- scanf("%d%d%d",&u,&v,&ad);
- solve(u,v,ad);
- }
- else if(str[0]=='D')
- {
- scanf("%d%d%d",&u,&v,&ad);
- solve(u,v,-ad);
- }
- else
- {
- scanf("%d",&u);
- ans=0;
- query(1,1,n,tid[u],tid[u]);
- printf("%d\n",ans);
- }
- }
- }
- return 0;
- }
Aragorn's Story HDU - 3966 -树剖模板的更多相关文章
- A - Aragorn's Story HDU - 3966 树剖裸题
这个题目是一个比较裸的树剖题,很好写. http://acm.hdu.edu.cn/showproblem.php?pid=3966 #include <cstdio> #include ...
- HDU 3966 (树链剖分+线段树)
Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板 ...
- AC日记——Aragorn's Story HDU 3966
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【树链剖分】洛谷P3384树剖模板
题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节点的值都加上z 操作2: 格式 ...
- 洛谷树剖模板题 P3384 | 树链剖分
原题链接 对于以u为根的子树,后代节点的dfn显然比他的dfn大,我们可以记录一下回溯到u的dfn,显然这两个dfn构成了一个连续区间,代表u及u的子树 剩下的就和树剖一样了 #include< ...
- HDU 3966 树链剖分+树状数组 模板
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3966(树链剖分+线段树区间更新)
传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...
- HDU 3966 /// 树链剖分+树状数组
题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...
- hdu 3966 树链剖分
思路:树链剖分入门题,我这门入得好苦啊,程序很快写出来了,可是在LCA过程中把update函数里的左右边界位置写反了,一直RE到死. #pragma comment(linker, "/ST ...
随机推荐
- 【洛谷P2822 组合数问题】
题目连接 #include<iostream> #include<cstring> #include<cstdio> #include<cctype> ...
- [APIO2007] 风铃
题目链接 可能是个树上 DP?指针真好玩 23333. 首先对于所有玩具如果有深度差超过 1 的就是无解(在这里贡献 WA * 3),所以 dfs 一遍记录深度是有必要的…… 然后如果有一个点的两颗子 ...
- Python统计词频的几种方式
语料 text = """My fellow citizens: I stand here today humbled by the task before us, gr ...
- Docker下安装Jenkins
Docker安装参见:https://www.cnblogs.com/hackyo/p/9280042.html 安装Jenkins: docker run \ -u root \ --rm \ -d ...
- JN_0004:轻松解码类似eval(function(p,a,c,k,e,d){}))的JavaScript代码
百度访问统计代码JavaScript源码:红色加粗部分将是要修改的地方.eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"&qu ...
- MySQL关于日志配置安全整改及处理方法
[环境介绍] 系统环境:Linux + mysql 5.7.18 + 主从复制架构 [背景描述] 需求:MySQL数据库都有每年的集团安全整改,常常要求弱口令扫描,基线扫描,漏洞扫描等等.对于MySQ ...
- 液晶流在齐次 Besov 空间中的正则性准则
在 [Zhang, Zujin. Regularity criteria for the three dimensional Ericksen–Leslie system in homogeneous ...
- 使用sessionStorage、localStorage存储数组与对象
先介绍一下localStorage localStorage对象是HTML5的客户端存储持久化数据的方案.为了能访问到同一个localStorage对象,页面必须来自同一个域名(子域名无效),使用同一 ...
- Django 反向解析
#1,定义: #随着功能的增加会出现更多的视图,可能之前配置的正则表达式不够准确,于是就要修改正则表达式,但是正则表达式一旦修改了,之前所有对应的超链接都要修改,真是一件麻烦的事情,而且可能还会漏掉一 ...
- spring事务源码分析结合mybatis源码(三)
下面将结合mybatis源码来分析下,这种持久化框架是如何对connection使用,来达到spring事务的控制. 想要在把mybatis跟spring整合都需要这样一个jar包:mybatis-s ...