数链剖分(Aragorn's Story )
题目链接:https://vjudge.net/contest/279350#problem/A
题目大意:n个点,m条边,然后q次询问,因为在树上,两个点能确定一条直线,我们可以对这条直线上的所有值进行加减操作,也可以单点询问。
各个数组的作用:sto是刚开始的输入数据,head是前向星,dfsnum指的是dfs序,depth指的是每个点的深度son指的是每个节点的重儿子,father指的是每个点的父节点,Size指的是以当前点为根节点的树,ord指的是遍历顺序,cost指的是编号之后的每个点,top指的是当前的这条重链的最顶端的那个点,剩下的就是线段树的数组了。
注意点:我们通过两个dfs来给这些数组赋值,通过第一个dfs,我们可以把depth和father,size,son求出来,剩下的ord和top通过第二个dfs求出来,为什么使用两个dfs?我的理解就是,第一个dfs和第二个dfs的遍历条件并不相同,第一个dfs就是能走就走,第二个dfs是在按照已经分好链的前提下进行走的,也就是说这里的ord数组并不能在第一个dfs中实现,只能在第二个数组中实现。(后续有新的理解会继续补充)。
AC代码:
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
#include<stdio.h>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
# define inf 0x3f3f3f3f
# define ll long long
# define lson l,m,rt<<
# define rson m+,r,rt<<|
const int maxn = 5e4+;
int sto[maxn],head[maxn],edgnum,dfsnum,depth[maxn];
int son[maxn],father[maxn],Size[maxn],ord[maxn],cost[maxn],top[maxn];
int tree[maxn<<],lazy[maxn<<];
struct node
{
int to;
int nex;
} edge[maxn<<];
void addedge(int fr,int to)
{
edge[edgnum].nex=head[fr];
edge[edgnum].to=to;
head[fr]=edgnum++;
}
void dfs1(int fr,int rt,int dep)
{
father[fr]=rt;
Size[fr]=;
son[fr]=-;
depth[fr]=dep;
for(int i=head[fr]; i!=-; i=edge[i].nex)
{
int to=edge[i].to;
if(to==rt)
continue;
dfs1(to,fr,dep+);
Size[fr]+=Size[to];
if(son[to]==-||(Size[son[fr]]<Size[to]))
{
son[fr]=to;
}
}
}
void dfs2(int fr,int rt)
{
ord[fr]=++dfsnum;
cost[ord[fr]]=sto[fr];
top[fr]=rt;
if(son[fr]!=-)
dfs2(son[fr],rt);
for(int i=head[fr]; i!=-; i=edge[i].nex)
{
int u=edge[i].to;
if(son[fr]!=u&&father[fr]!=u)
{
dfs2(u,u);
}
}
}
void init()
{
dfsnum=;
dfs1(,-,);
dfs2(,);
}
void up(int rt)
{
tree[rt]=tree[rt<<]+tree[rt<<|];
}
void down(int len,int rt)
{
if(lazy[rt])
{
lazy[rt<<]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
tree[rt<<]+=(len-len/)*lazy[rt];
tree[rt<<|]+=(len/)*lazy[rt];
lazy[rt]=;
}
}
void buildtree(int l,int r,int rt)
{
lazy[rt]=;
tree[rt]=;
if(l==r)
{
tree[rt]=cost[l];
return ;
}
int m=(l+r)>>;
buildtree(lson);
buildtree(rson);
up(rt);
}
void update(int l,int r,int rt,int L,int R,int p)
{
if(L<=l&&R>=r)
{
tree[rt]+=p*(r-l+);
lazy[rt]+=p;
return ;
}
down(r-l+,rt);
int m=(l+r)>>;
if(L<=m)
update(lson,L,R,p);
if(R>m)
update(rson,L,R,p);
up(rt);
}
void Update(int n,int x,int y,int p)
{
int tx=top[x],ty=top[y];
while(tx!=ty)
{
if(depth[tx]<depth[ty])
{
swap(tx,ty);
swap(x,y);
}
update(,n,,ord[tx],ord[x],p);
x=father[tx],tx=top[x];
}
if(depth[x]<depth[y])
{
swap(x,y);
}
update(,n,,ord[y],ord[x],p);
}
int query(int l,int r,int rt,int pos)
{
if(l==r)
{
return tree[rt];
}
down(r-l+,rt);
int ans=;
int m=(l+r)>>;
if(pos<=m)
ans+=query(lson,pos);
if(pos>m)
ans+=query(rson,pos);
return ans;
up(rt);
}
int main()
{
int n,m,q;
while(~scanf("%d %d %d",&n,&m,&q))
{
edgnum=;
for(int i=; i<=n; i++)
{
scanf("%d",&sto[i]);
head[i]=-;
}
int t1,t2;
for(int i=; i<=m; i++)
{
scanf("%d %d",&t1,&t2);
addedge(t1,t2);
addedge(t2,t1);
}
init();
char str[];
buildtree(,n,);
int t3;
while(q--)
{
scanf("%s",str);
if(str[]=='I')
{
scanf("%d %d %d",&t1,&t2,&t3);
Update(n,t1,t2,t3);
}
else if(str[]=='Q')
{
scanf("%d",&t1);
int ans=query(,n,,ord[t1]);
printf("%d\n",ans);
}
else if(str[]=='D')
{
scanf("%d %d %d",&t1,&t2,&t3);
Update(n,t1,t2,-t3);
}
}
}
return ;
}
数链剖分(Aragorn's Story )的更多相关文章
- G - Game HDU - 5242 (数链剖分)
题目链接: G - Game HDU - 5242 题目大意:首先是T组测试样例,给出一颗以1节点为根的树,每个节点有各自的价值,有m次从根节点出发向下走到叶子节点的机会,每次会得到所有经过节点的权值 ...
- 线段树&数链剖分
傻逼线段树,傻逼数剖 线段树 定义: 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点. 使用线段树可以快速的查找某一个节点在若干条线段中出现 ...
- 数链剖分(Tree)
题目链接:https://cn.vjudge.net/contest/279350#problem/D 题目大意:操作,单点查询,区间取反,询问区间最大值. AC代码: #include<ios ...
- 数链剖分(树的统计Count )
题目链接:https://cn.vjudge.net/contest/279350#problem/C 具体思路:单点更新,区间查询,查询的时候有两种操作,查询区间最大值和区间和. 注意点:在查询的时 ...
- 数链剖分(Housewife Wind )
题目链接:https://vjudge.net/contest/279350#problem/B 题目大意:给你n,q,s.n指的是有n个点,q代表有q次询问,s代表的是起点.然后接下来会有n-1条 ...
- HDU 3966 Aragorn's Story (树链剖分+树状数组)
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 浅谈树链剖分(C++、算法、树结构)
关于数链剖分我在网上看到的有几个比较好的讲解,本篇主要是对AC代码的注释(感谢各位witer的提供) 这是讲解 http://www.cnblogs.com/kuangbin/archive/2013 ...
- POJ 3237 Tree (树链剖分)
Tree Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 2825 Accepted: 769 Description ...
- 1036: [ZJOI2008]树的统计Count (树链剖分)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3401 Solved: 1418[Submit] ...
随机推荐
- 1094. The Largest Generation (25)-(dfs,树的遍历,统计每层的节点数)
题目很简单,就是统计一下每层的节点数,输出节点数最多的个数和对应的层数即可. #include <iostream> #include <cstdio> #include &l ...
- Python中的字典详解
https://www.cnblogs.com/yjd_hycf_space/p/6880026.html
- php插入中文数据到MySQL乱码
事情是这样的:我在本地的测试成功了,放到服务器测试,发现服务器的数据库里的中文竟然乱码了. 我进行了以下几步基本的做法: PHP文件改为utf-8的格式. 加入header("Content ...
- 浅谈个人对RAID技术的理解
RAID,字面意思为一种廉价的冗余磁盘阵列,它是通过将大量的磁盘分组,实现了数据冗余,目的是为了保护数据.RAID现已经应用于计算机各个领域.它的优点是降低了工作成本并提高了效率,并且使系统有稳定的运 ...
- ElasticSearch 2 (14) - 深入搜索系列之全文搜索
ElasticSearch 2 (14) - 深入搜索系列之全文搜索 摘要 在看过结构化搜索之后,我们看看怎样在全文字段中查找相关度最高的文档. 全文搜索两个最重要的方面是: 相关(relevance ...
- 关于使用实验室服务器的GPU以及跑上TensorFlow代码
连接服务器 Windows - XShell XFtp SSH 通过SSH来连接实验室的服务器 使用SSH连接已经不陌生了 github和OS课设都经常使用 目前使用 192.168.7.169 使用 ...
- 打包spring项目遇到的坑 Unable to locate Spring NamespaceHandler for XML schema ……shcema/context 产生的原因及解决方法
图1 图2 问题原因:导致该问题的原因就是打包的时候,同时将 spring-context 和 spring-aop包提取到了我们的程序应用的包中,在package过程中,这2个依赖包的 XML sc ...
- 使用ajax异步上传文件或图片(配合php)
//html代码 <form enctype="multipart/form-data" id="upForm"> <input type=& ...
- mininet invalid literal for int() with base 10: 'cpu.cfs_period_us:'
问题产生原因:内核编译时没有加入 CONFIG_CFS_BANDWIDTH 选项 http://www.haifux.org/lectures/299/netLec7.pdf https://mail ...
- Strongly connected HDU - 4635(判断强连通图 缩点)
找出强联通块,计算每个连通块内的点数.将点数最少的那个连通块单独拿出来,其余的连通块合并成一个连通分量. 那么假设第一个连通块的 点数是 x 第二个连通块的点数是 y 一个强连通图(每两个点之间,至 ...