hdu 3966 树链剖分
思路:树链剖分入门题,我这门入得好苦啊,程序很快写出来了,可是在LCA过程中把update函数里的左右边界位置写反了,一直RE到死。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pb push_back
#define mp make_pair
#define Maxn 50010
#define Maxm 200010
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 100000
#define lowbit(x) (x&(-x))
#define clr(x,y) memset(x,y,sizeof(x))
#define Mod 1000000007
using namespace std;
int head[Maxn],vi[Maxn],dep[Maxn],w[Maxn],top[Maxn],son[Maxn],sz[Maxn],fa[Maxn],e,id;
int num[Maxn];
struct Edge{
int u,v,next;
}edge[Maxn*];
struct Tree{
int l,r,c;
int mid(){
return (l+r)>>;
}
}tree[Maxn*];
void init()
{
clr(head,-);clr(vi,);
e=;id=;
}
void add(int u,int v)
{
edge[e].u=u,edge[e].v=v,edge[e].next=head[u],head[u]=e++;
}
void BuildTree(int l,int r,int po)
{
tree[po].l=l,tree[po].r=r,tree[po].c=;
if(l==r)
return ;
int mid=tree[po].mid();
BuildTree(l,mid,lson(po));
BuildTree(mid+,r,rson(po));
}
void down(int po)
{
tree[lson(po)].c+=tree[po].c;
tree[rson(po)].c+=tree[po].c;
tree[po].c=;
}
void update(int l,int r,int c,int po)
{
if(l<=tree[po].l&&tree[po].r<=r){
tree[po].c+=c;
return ;
}
down(po);
int mid=tree[po].mid();
if(r<=mid)
update(l,r,c,lson(po));
else if(l>=mid+)
update(l,r,c,rson(po));
else {
update(l,mid,c,lson(po));
update(mid+,r,c,rson(po));
}
}
int getans(int i,int po)
{
if(tree[po].l==tree[po].r){
return tree[po].c;
}
down(po);
int mid=tree[po].mid();
if(i<=mid)
return getans(i,lson(po));
else
return getans(i,rson(po));
}
void dfs(int u)
{
vi[u]=;
int i,v;
son[u]=,sz[u]=;
for(i=head[u];i!=-;i=edge[i].next){
v=edge[i].v;
if(vi[v]) continue;
dep[v]=dep[u]+;
fa[v]=u;
dfs(v);
if(sz[v]>sz[son[u]])son[u]=v;
sz[u]+=sz[v];
}
}
void build(int u,int ti)
{
int i,v;
w[u]=++id;top[u]=ti;vi[u]=;
if(son[u]) build(son[u],ti);
for(i=head[u];i!=-;i=edge[i].next){
v=edge[i].v;
if(vi[v]||v==son[u]) continue;
build(v,v);
}
}
void calc(int u,int v,int c)
{
int f1=top[u],f2=top[v];
while(f1!=f2){
if(dep[f1]<dep[f2]){
swap(f1,f2),swap(u,v);
}
update(w[f1],w[u],c,);
u=fa[f1];f1=top[u];
}
if(dep[u]>dep[v])
swap(u,v);
update(w[u],w[v],c,);
return ;
}
int main()
{
int n,m,p,i,j,u,v,val;
char str[];
while(scanf("%d%d%d",&n,&m,&p)!=EOF){
init();
for(i=;i<=n;i++)
scanf("%d",num+i);
for(i=;i<=m;i++){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs();
clr(vi,);
build(,);
BuildTree(,id,);
int ans;
for(i=;i<=p;i++){
scanf("%s",str);
if(str[]=='I'){
scanf("%d%d%d",&u,&v,&val);
calc(u,v,val);
}
else if(str[]=='D'){
scanf("%d%d%d",&u,&v,&val);
calc(u,v,-val);
}else {
scanf("%d",&u);
ans=getans(w[u],);
printf("%d\n",num[u]+ans);
}
}
}
return ;
}
hdu 3966 树链剖分的更多相关文章
- HDU 3966 (树链剖分+线段树)
Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板 ...
- 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 树链剖分后线段树维护
题意: 一棵树, 操作1.$path(a,b)$之间的点权$+k$ 操作2.单点查询 题解: 树链剖分即可,注意代码细节,双向映射 主要是记录一下板子 #include <string.h> ...
- HDU 3966 树链剖分+树状数组 模板
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 5893 (树链剖分+合并)
List wants to travel Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
- hdu 5052 树链剖分
Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- hdu 4897 树链剖分(重轻链)
Little Devil I Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 5274 树链剖分
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
随机推荐
- C#获取程序集的版本号和最后编译时间
C#获取程序集的版本号:string ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToStrin ...
- WEB标准系列-HTML元素嵌套
转:http://www.smallni.com/element-nesting/ 先来看以下这样一段代码: <ul> <li><h4><a href=&qu ...
- iOS 3DES DES AES加密注意事项!!很重要,否则会加密失败
今天做项目,需要进行3des加密. 加密的gkey:abcdefgh giv:(偏移量)abcdefgh 加密后结果:p+X985x5bFS6dWjAnm6sdQ== 下面是代码: +(NSStr ...
- case when遇到空串转成0
须要注意:假设字段为varchar类型,when后的条件要加上引號 SELECT (CASE marital_status WHEN 0 THEN '已婚' WHEN 1 THEN '未婚' ELSE ...
- 博客标题栏增加一个"闪存“按钮
最近来博客园喜欢去闪存上看看,也就是一个类似微博的东西,但是貌似没看到哪里有这个按钮. 所以只要自己动手搞一个. 暴力猴js: // ==UserScript== // @name fwindpeak ...
- Jquery 右键菜单(ContextMenu)插件使用记录
目前做的项目需要在页面里面用右键菜单,在网上找到两种jquery的右键菜单插件,但是都有各种问题.所以就自己动手把两种插件结合了下. 修改后的右键菜单插架可以根据绑定的触发页面元素不同,复用同一个菜单 ...
- TP的一条sql语句(子查询)
$model=M(''); $model->table(C('DB_PREFIX').'goods as g') ->join(C('DB_PREFIX').'orders as o on ...
- 强连通分量(LRJ训练指南)
#include <iostream> #include <queue> #include <string> #include <cstdio> #in ...
- 【React Native 实战】微信登录
1.前言 在今天无论是游戏开发还是app开发,微信作为第三方登录必不可少,今天我们就用react-native-wechat实现微信登录,分享和支付同样的道理就不过多的介绍了. 2.属性 1)regi ...
- iOS-swift环形进度指示器+图片加载动画
demo.gif 如图,这个动画的是如何做的呢? 分析: 1.环形进度指示器,根据下载进度来更新它 2.扩展环,向内向外扩展这个环,中间扩展的时候,去掉这个遮盖 一.环形进度指示器 1.自定义View ...