HDU 3966 Aragorn's Story 树链剖分
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 树链剖分的更多相关文章
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)
题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...
- HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3966 Aragorn's Story 树链剖分+BIT区间修改/单点询问
Aragorn's Story Description Our protagonist is the handsome human prince Aragorn comes from The Lord ...
- hdu 3966 Aragorn's Story : 树链剖分 O(nlogn)建树 O((logn)²)修改与查询
/** problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966 裸板 **/ #include<stdio.h> #include& ...
- hdu 3966 Aragorn's Story 树链剖分 按点
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3966 Aragorn's Story (树链剖分入门题)
树上路径区间更新,单点查询. 线段树和树状数组都可以用于本题的维护. 线段树: #include<cstdio> #include<iostream> #include< ...
- HDU 3966 Aragorn's Story (树链点权剖分,成段修改单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. //树链剖分 边权修 ...
- HDU 3966 Aragorn's Story 树链拋分
一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...
随机推荐
- UVA数学入门训练Round1[6]
UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...
- 初识javascript变量和基本数据类型
1.1首先,学习使用firebug控制台.设置一下firefox 中的配置选项,以便使控制台中的javascript警告更为严格...以方便我们更好的找出程序中不必要的bug. 1. 在火狐浏览器fi ...
- ACE admin 后台管理框架
一.样式 1.常见按钮 .btn-app .btn-group .btn-corner .btn-xs .btn-sm .btn-mini .btn-minier .btn-defalut .btn ...
- git使用札记
#1,本地推送到远程 查看本地关联的远程分支:git remote -v 添加远程分支:git remote add 推送本地到远程分支:git push URLOFYOURGIT #2,远程获取一个 ...
- LeetCode:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
- NOSQL数据模型和CAP原理
NOSQL数据模型和CAP原理 http://blog.sina.com.cn/s/blog_7800d9210100t33v.html 我本来一直觉得NoSQL其实很容易理解的,我本身也已经对NoS ...
- Winform开发:在ProgressBar显示百分比数字
如果不使用Label而是直接在进度条上显示文字,可以扩展一个派生类自己画,代码如下: public partial class Form1 : Form { public Form1() { Init ...
- 发布ASP.NET Core网站到IIS
打开VS2015,新建项目: 选择模板,取消身份验证: 项目加载完成后就可以运行了: 下面要发布到IIS: 需要安装从IIS到Kestrel server的反向代理,下载地址:.NET Core Wi ...
- 解决 Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
转载 php 5个版本,5.2.5.3.5.4.5.5,怕跟不上时代,新的服务器直接上5.5,但是程序出现如下错误:Deprecated: mysql_connect(): The mysql ext ...
- 【USACO 2.3】Money Systems(dp)
v种货币,求有多少种组成和为n. dp[i][j]表示前i种货币价格为j有多少种方案,dp[i][j]+=dp[i-1][j-c]. http://train.usaco.org/usacoprob2 ...