【hdu3966】Aragorn's Story
题意:给一棵树,并给定各个点权的值,然后有3种操作:
I C1 C2 K: 把C1与C2的路径上的所有点权值加上K
D C1 C2 K:把C1与C2的路径上的所有点权值减去K
Q C:查询节点编号为C的权值
裸裸的树剖
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std; typedef long long LL; #define N 50010 struct Node
{
int to,next;
}e[N<<]; int id,cnt;
int head[N];
int num[N],siz[N],top[N],son[N];
int dep[N],pos[N],rank1[N],fa[N]; LL sum[N<<],add[N<<]; char s[]; int n,m,q;
int u,v;
int al,ar,ask; inline void init()
{
memset(son,-,sizeof(son));
memset(head,-,sizeof(head));
id=;
cnt=;
} void link(int x,int y)
{
e[++cnt]=(Node){y,head[x]};
head[x]=cnt;
} void dfs(int x,int father,int d)
{
siz[x]=;
dep[x]=d;
fa[x]=father;
for (int i=head[x];~i;i=e[i].next)
{
int t=e[i].to;
if (t!=fa[x])
{
dfs(t,x,d+);
siz[x]+=siz[t];
if (son[x]==- || siz[x]>siz[son[x]])
son[x]=t;
}
}
} void dfs2(int x,int cha)
{
top[x]=cha;
pos[x]=++id;
rank1[pos[x]]=x;
if (son[x]==-)
return ;
dfs2(son[x],cha);
for(int i=head[x];~i;i=e[i].next)
{
int t=e[i].to;
if(t!=son[x] && t!=fa[x])
dfs2(t,t);
}
} void pushup(int now)
{
sum[now]=max(sum[now<<],sum[now<<|]);
} void pushdown(int now,int m)
{
if (add[now])
{
add[now<<]+=add[now];
add[now<<|]+=add[now];
sum[now<<]+=add[now]*(m-(m>>));
sum[now<<|]+=add[now]*(m>>);
add[now]=;
}
} void build(int nowl,int nowr,int now)
{
add[now]=;
if (nowl==nowr)
{
sum[now]=num[rank1[nowl]];
return ;
}
int mid=(nowl+nowr)>>;
build(nowl,mid,now<<);
build(mid+,nowr,now<<|);
pushup(now);
} void update(int nowl,int nowr,int now,int L,int R,int d)
{
if (nowl>=L && nowr<=R)
{
add[now]+=d;
sum[now]+=d*(nowr-nowl+);
return ;
}
pushdown(now,nowr-nowl+);
int mid=nowl+nowr>>;
if (L<=mid) update(nowl,mid,now<<,L,R,d);
if (mid<R) update(mid+,nowr,now<<|,L,R,d);
pushup(now);
} int query(int nowl,int nowr,int now,int d)
{
int res();
if (nowl==nowr)
return sum[now];
pushdown(now,nowr-nowl+);
int mid=nowl+nowr>>;
if (d<=mid) res=query(nowl,mid,now<<,d);
else res=query(mid+,nowr,now<<|,d);
pushup(now);
return res;
} void work(int x,int y,int val)
{
while (top[x]!=top[y])
{
if (dep[top[x]]<dep[top[y]])
swap(x,y);
update(,n,,pos[top[x]],pos[x],val);
x=fa[top[x]];
}
if (dep[x]>dep[y])
swap(x,y);
update(,n,,pos[x],pos[y],val);
}
int main()
{
while (scanf("%d%d%d",&n,&m,&q)!=EOF && n+m+q)
{
init();
for (int i=;i<=n;i++)
scanf("%d",&num[i]);
for (int i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
link(u,v);
link(v,u);
}
dfs(,,);
dfs2(,);
build(,n,);
while (q--)
{
scanf("%s",s);
if (s[]=='I')
{
scanf("%d%d%d",&al,&ar,&ask);
work(al,ar,ask);
}
else if (s[]=='D')
{
scanf("%d%d%d",&al,&ar,&ask);
work(al,ar,-ask);
}
else
{
scanf("%d",&ask);
printf("%d\n",query(,n,,pos[ask]));
}
}
}
return ;
}
【hdu3966】Aragorn's Story的更多相关文章
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- 【原】谈谈对Objective-C中代理模式的误解
[原]谈谈对Objective-C中代理模式的误解 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 这篇文章主要是对代理模式和委托模式进行了对比,个人认为Objective ...
- 【原】FMDB源码阅读(三)
[原]FMDB源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 FMDB比较优秀的地方就在于对多线程的处理.所以这一篇主要是研究FMDB的多线程处理的实现.而 ...
- 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新
[原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...
- 【调侃】IOC前世今生
前些天,参与了公司内部小组的一次技术交流,主要是针对<IOC与AOP>,本着学而时习之的态度及积极分享的精神,我就结合一个小故事来初浅地剖析一下我眼中的“IOC前世今生”,以方便初学者能更 ...
- Python高手之路【三】python基础之函数
基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ...
- Python高手之路【一】初识python
Python简介 1:Python的创始人 Python (英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种解释型.面向对象.动态数据类型的高级程序设计语言,由荷兰人Guido ...
- 【开源】简单4步搞定QQ登录,无需什么代码功底【无语言界限】
说17号发超简单的教程就17号,qq核审通过后就封装了这个,现在放出来~~ 这个是我封装的一个开源项目:https://github.com/dunitian/LoTQQLogin ————————— ...
- 【原】FMDB源码阅读(二)
[原]FMDB源码阅读(二) 本文转载请注明出处 -- polobymulberry-博客园 1. 前言 上一篇只是简单地过了一下FMDB一个简单例子的基本流程,并没有涉及到FMDB的所有方方面面,比 ...
随机推荐
- 笔试算法题(42):线段树(区间树,Interval Tree)
议题:线段树(Interval Tree) 分析: 线段树是一种二叉搜索树,将一个大区间划分成单元区间,每个单元区间对应一个叶子节点:内部节点对应部分区间,如对于一个内部节点[a, b]而言,其左子节 ...
- 笔试算法题(14):整数二进制表示中的1 & 判定栈的push和pop序列是否对应
出题:输入一个整数,要求计算此整数的二进制表示中1的个数 分析: 如果整数表示为k,当其是负数的时候,使用1<<i分别检测k的每一位:当其位整数的时候,则k/2表示将其二进制表示右移一位, ...
- PHP明细之间的关联和having进行分组,不推荐这样做,只是做为偷懒的办法
-- 只求和wrt的数据,其它数据保持不变!SELECT A.return_id,A.relevant_id,A.order_id,A.deliver_order_id,A.product_id,A. ...
- Spring中注解注入bean和配置文件注入bean
注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: & ...
- memcached协议解析 及使用
本文转载自:http://www.ccvita.com/306.html 协议memcached 的客户端使用TCP链接与服务器通讯.(UDP接口也同样有效,参考后文的 “UDP协议” )一个运行中的 ...
- thinkphp5 框架修改的地方
框架修改的地方 vendor/topthink/think-captcha/src/Captcha.php api验证码入库 196行 $img_code = strtoupper(implode(' ...
- AutoEncoders原理
目录 Auto-Encoders How to Train? Auto-Encoders How to Train?
- 集训第六周 数学概念与方法 UVA 11722 几何概型
---恢复内容开始--- http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=31471 题意,两辆火车,分别会在[t1,t2],[ ...
- 第二周习题F
Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications: x2 ...
- 如何判断CPU、内存、磁盘的性能瓶颈?
1.如何判断CPU.内存.磁盘的瓶颈? CPU瓶颈1) 查看CPU利用率.建议CPU指标如下 a) User Time:65%-70% b) System Time:30%-35% c) Idle:0 ...