Link: http://acm.hdu.edu.cn/showproblem.php?pid=3966

这题注意要手动扩栈。

这题我交g++无限RE,即使手动扩栈了,但交C++就过了。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;
const int maxn = 5e4+;
#ifdef WIN32
#define LL __int64
#else
#define LL long long
#endif int A[maxn],totw;
struct node
{
int fa,son,top,w,size,deep;
}tree[maxn];
vector<int> vt[maxn];
int tr[maxn]; void dfs_1(int p,int last,int de)
{
tree[p].son = -;
tree[p].fa = last;
tree[p].deep = de;
tree[p].size = ;
int num = vt[p].size();
for(int i = ;i < num;++i)
if(vt[p][i] != last)
{
dfs_1(vt[p][i],p,de+);
tree[p].size += tree[vt[p][i]].size;
if(- == tree[p].son || tree[vt[p][i]].size > tree[tree[p].son].size)
tree[p].son = vt[p][i];
}
}
void dfs_2(int p,int top)
{
tree[p].w = ++totw;
tree[p].top = top;
if(tree[p].son != -)
dfs_2(tree[p].son,top);
else return ;
int num = vt[p].size();
for(int i = ;i < num;++i)
{
if(vt[p][i] != tree[p].fa && vt[p][i] != tree[p].son)
dfs_2(vt[p][i],vt[p][i]);
}
} void sol()
{
totw = ;
memset(tr,,sizeof(tr));
memset(tree,,sizeof(tree));
dfs_1(,,);
dfs_2(,);
}
int lowbit(int x)
{
return x & (-x);
}
int query(int p)
{
p = tree[p].w;
int tot = ;
while(p >= )
{
tot += tr[p];
p -= lowbit(p);
}
return tot;
}
void add(int n,int p,int d)
{
while(p <= n)
{
tr[p] += d;
p += lowbit(p);
}
}
void update_sec(int n,int u,int v,int d)
{
if(tree[u].w > tree[v].w) swap(u,v);
add(n,tree[u].w,d);
add(n,tree[v].w+,-d);
}
void update(int n,int u,int v,int d)
{
/*
u,v不断靠近根结点
*/
while(tree[u].top != tree[v].top)
{
if(tree[tree[u].top].deep < tree[tree[v].top].deep) swap(u,v);
update_sec(n,tree[u].top,u,d);
u = tree[tree[u].top].fa;
}
update_sec(n,u,v,d);
} int main()
{
// freopen("in.txt","r",stdin);
int n,m,p;
while(scanf("%d%d%d",&n,&m,&p)!=EOF)
{
for(int i = ;i <= n;++i)
scanf("%d",&A[i]);
for(int i = ;i <= n;++i)
vt[i].clear();
int x,y;
for(int i = ;i < m;++i)
{
scanf("%d%d",&x,&y);
vt[x].push_back(y);
vt[y].push_back(x);
}
sol();
char oper[];
while(p--)
{
scanf("%s",oper);
int x,y,z;
if('Q' == oper[])
{
scanf("%d",&x);
printf("%d\n",A[x] + query(x));
}
else if('I' == oper[])
{
scanf("%d%d%d",&x,&y,&z);
update(n,x,y,z);
}
else if('D' == oper[])
{
scanf("%d%d%d",&x,&y,&z);
update(n,x,y,-z);
}
}
}
return ;
}

HDU 3966 Aragorn's Story 树链剖分的更多相关文章

  1. HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树

    HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...

  2. Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)

    题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...

  3. HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 3966 Aragorn's Story 树链剖分+BIT区间修改/单点询问

    Aragorn's Story Description Our protagonist is the handsome human prince Aragorn comes from The Lord ...

  5. hdu 3966 Aragorn's Story : 树链剖分 O(nlogn)建树 O((logn)²)修改与查询

    /** problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966 裸板 **/ #include<stdio.h> #include& ...

  6. hdu 3966 Aragorn's Story 树链剖分 按点

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU 3966 Aragorn's Story (树链剖分入门题)

    树上路径区间更新,单点查询. 线段树和树状数组都可以用于本题的维护. 线段树: #include<cstdio> #include<iostream> #include< ...

  8. HDU 3966 Aragorn's Story (树链点权剖分,成段修改单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. //树链剖分 边权修 ...

  9. HDU 3966 Aragorn's Story 树链拋分

    一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...

随机推荐

  1. GNU make使用变量⑤变量的引用、定义等

    在 Makefile 中,变量是一个名字(像是 C 语言中的宏),代表一个文本字符串(变量的值).在 Makefile 的目标.依赖.命令中引用变量的地方,变量会被它的值所取代(与 C 语言中宏引用的 ...

  2. dpctl 工具使用

    一.在建立Mininet的时候,需要设置listenPort,这样可以在其它命令行里设置flow net = Mininet( topo=topo, listenPort=6634 ) 二.常见用法m ...

  3. MySQL数据类型-decimal详解

    from:http://www.linuxidc.com/Linux/2013-07/88032.htm 1.首先,对于精度比较高的东西,比如money,我会用decimal类型,不会考虑float, ...

  4. C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 检查版本升级、检查登录超时

    当用户都在一个公司内,甚至一个办公室时,喉一下或者跑过去亲自更新一下程序,就可以了,问题就很简单也没多少复杂性,也不怎么考验技术能力.当面对全国10万以上客户端时,问题就来了. 1:有的用户打开系统一 ...

  5. jQuery基础课程

    环境搭建 搭建一个jQuery的开发环境非常方便,可以通过下列几个步骤进行. 下载jQuery文件库 在jQuery的官方网站(http://jquery.com)中,下载最新版本的jQuery文件库 ...

  6. scala速成记录1

    选择  Learning Scala这本书,两百多页,足够薄. 安装 http://www.scala-lang.org/  下载Binary的版本.bin里边有所有操作系统下运行的可以运行的交互式s ...

  7. JS组件系列——Bootstrap寒冬暖身篇:弹出框和提示框效果以及代码展示

    前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编辑功能,一般常见的主要有两种处理方式:行内编辑和弹出框编辑.在增加用户体验方面,弹出框和提示框起着重要的作用,如果你的 ...

  8. 【jQuery】 jQuery上下飘动效果

    jQuery实现图片上下飘动效果 function moveRocket() { $(".smallShip") //2000毫秒内top = top + 60: .animate ...

  9. 软件工程(FZU2015)赛季得分榜,第11回合(beta冲刺+SE总结)

    目录 第一回合 第二回合 第三回合 第四回合 第五回合 第6回合 第7回合 第8回合 第9回合 第10回合 第11回合 增补作业 积分规则 积分制: 作业为10分制,练习为3分制:alpha30分:b ...

  10. css 补漏

    1.box-sizing: width(宽) + padding(内边距) + border(边框) = 元素实际宽度    height(高) + padding(内边距) + border(边框) ...