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. CHM打不开的解决方法

    CHM打不开的解决方法 听语音 | 浏览:62240 | 更新:2013-02-04 14:58 | 标签:软件 1 2 3 4 5 6 分步阅读 一键约师傅 百度师傅高质屏和好师傅,拯救你的碎屏机 ...

  2. 掌握SortSet接口和Set接口的关系,以及常用方法。

    TreeSet类是可以排序的类.TreeSet实际上也是SortSet接口的子类. 此接口的所有类都是可以排序的. 所有的方法: 实例: package 类集; import java.util.So ...

  3. UCenter创始人密码正确但是登录不了

    UCenter创始人密码正确但是登录不了,没有什么提示,就反复输入密码登录 也进不了......... ================================================ ...

  4. JS:offsetWidth\offsetleft 等图文解释

        网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.of ...

  5. sql 关于查询时 出现的 从数据类型 varchar 转换为 numeric 时出错 的解决方法。

    出现这种问题 一般是查询时出现了 varchar 转 numeric 时出了错  或varchar字段运算造成的 解决方法: 让不能转的数不转换就可以了 sql的函数有个isNumeric(参数) 用 ...

  6. 【codevs2495】 水叮当的舞步

    http://codevs.cn/problem/2495/ (题目链接) 题意 给出一个N*N的矩阵,其中元素有5种颜色,每次可以将左上角元素所在的连通块更换一种颜色,连通块指相邻并且颜色相同的元素 ...

  7. DialogFragment is gone after returning back from another activity

    基本情景如下: 在DialogFragment中单击一个按钮跳转到another Activity做一些逻辑处理,然后将返回的结果回显到该DialogFragment上. 处理逻辑是: 在Dialog ...

  8. linux下压缩,解压缩的方法

    linux zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip - ...

  9. 如丝般顺滑地从Windows迁移SQLServer数据库到Linux

    老鸟看过菜鸟的上一篇<MSSQL On Linux备份与还原>文章后,很满意,但是还是忍不住发问:"这篇文章讲的是MSSQL在Linux系统上的备份与还原,如果我之前是Windo ...

  10. web前端各大技术都能实现什么功能

    web前端各大技术都能实现什么功能 以下是孜然为你总结的web前端开发你必须要一项一项掌握的技术:Html.css.ajax.jquery.extjs.JavaScript,今天为你详细解读他们各自都 ...