【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的所有方方面面,比 ...
随机推荐
- 解决java web项目导入后出现的问题 ---cannot be read or is not a valid ZIP file
导入以前的web项目后会出现以下三个错误: 1. Archive for required library: ‘WebContent/WEB-INF/lib/readme.txt’ in projec ...
- 「 Luogu P3137 」X 「 USACO16FEB 」 圆形谷仓
# 题目大意 管理大大给修下 $\text{Markdown}$ 吧,严重影响做题体验啊. 这道题的意思很简单就是给你一个长度是 $n$ 的环,这个环上不均匀的分布着 $n$ 头奶牛.一头奶牛移动要花 ...
- vuex相关(actions和mutation的异曲同工)
vuex说明: Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 包含的内容: state: ...
- [Python3网络爬虫开发实战] 1.2.3-ChromeDriver的安装
前面我们成功安装好了Selenium库,但是它是一个自动化测试工具,需要浏览器来配合使用,本节中我们就介绍一下Chrome浏览器及ChromeDriver驱动的配置. 首先,下载Chrome浏览器,方 ...
- js 技巧 (九)按键JS
1. 禁止复制(copy),禁用鼠标右键! <SCRIPT> //加入页面保护 function rf() {return false; } document.oncontextmenu ...
- buf.compare()
buf.compare(otherBuffer) otherBuffer {Buffer} 返回:{Number} 比较两个 Buffer 实例,无论 buf 在排序上靠前.靠后甚至与 otherBu ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- Django——分页功能Paginator
Django分页功能----Paginator Paginator所需参数: Paginator(object_list,per_page) Paginator常用属性: per_page: 每页显示 ...
- BNUOJ 26229 Red/Blue Spanning Tree
Red/Blue Spanning Tree Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on HDU. ...
- Codeforces Round #355 (Div. 2)-B. Vanya and Food Processor,纯考思路~~
B. Vanya and Food Processor time limit per test 1 second memory limit per test 256 megabytes input s ...