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 ...
随机推荐
- java_web用户的自动登录模块的实现
javaBean的代码 package bean; import java.io.Serializable; public class Admin implements Serializable{ / ...
- Fragment初步了解
fragment 1.fragment解释: 从英文上来说fragment是碎片和片段的意思,这解释的是相当到位的,因为android中的fragment就像是碎片嵌在了Activity当中的,为构造 ...
- 图片 文字 input等垂直居中对齐
span::before { width:100%; height:1px; font-size:1em; content:''; background:#fff; position:absolute ...
- Windows常用性能计数器总结
基础监控: Processor:% Processor Time CPU当前利用率,百分比 Memory:Available MBytes 当前可用内存,兆字节(虚拟内存不需要监控,只有当物理内存不够 ...
- 数据结构复习:交换排序原理及C++实现
1. 交换排序的基本思想 两两比较key值,如果发生逆序(排列的顺序与期望的顺序相反)就交换,知道所有对象都排序完毕!常见的3种交换排序算法:冒泡排序,shaker排序和快速排序. 2. 冒泡排序 设 ...
- 一位Erlang程序猿的自白
12.00 Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style De ...
- 【WebForm】Js调用后台C#方法
因业务的需要,有这么个需求,需要前台的JS传参调用C#后台的方法.现在有这么个方法可以解决,整理如下. 首先,先说一下基本实现,前台用Jquery的ajax将其中的URL后加方法,然后在Data中传递 ...
- Task could not find "AxImp.exe" using the SdkToolsPath "C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\"
本机v7.0A目录里没有AxImp.exe,无奈只能去官网下了个V7.1的. 安装完V7.1后,去“开始-所有程序-Microsoft Windows SDK v7.1”里找到Windows SDK ...
- UVA 12898 And Or 数学暴力
And Or Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.actio ...
- codeblocks中添加-std=c99
早上用codeblocks编译一个c文件,出现这样一个编译错误: +'for'+loop+initial+declarations+are+only+allowed+in+C99+mode 原来cod ...