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这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...
随机推荐
- POJ3928Ping pong[树状数组 仿逆序对]
Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3109 Accepted: 1148 Descrip ...
- ArrayList 保证多线程安全
一:使用synchronized关键字 二:使用Collections.synchronizedList();使用方法如下: 假如你创建的代码如下:List<Map<String,Obje ...
- aop
做aop做一些事情::: package cn.happy.spring04aop; public interface ISomeService { public void doSomeThing() ...
- 通过trie树单词自动补全(二)
经常使用iciba进行单词查询, 关于他的搜索建议是通过单词前缀做的索引, 所以自己想动手实现下, 当然如果借助mysql的话,一条sql语句就能实现, 网上查询了下trie正适合做这个,所以通过C语 ...
- 【笔记5】用pandas实现矩阵数据格式的推荐算法 (基于物品的协同)
''' 基于物品的协同推荐 矩阵数据 说明: 1.修正的余弦相似度是一种基于模型的协同过滤算法.我们前面提过,这种算法的优势之 一是扩展性好,对于大数据量而言,运算速度快.占用内存少. 2.用户的评价 ...
- 屏蔽防止被别的网站嵌入框架代码(防止被人frame)
<SCRIPT LANGUAGE=javascript> if (top.location != self.location)top.location=self.location; < ...
- iOS 关于字体根据不同屏幕尺寸等比适配的问题(zz)
http://www.jianshu.com/p/5815e81abb52 背景 去年的六月份开始了一个新的项目,此项目支持的设备从4S开始一直到6+,也就是说屏幕的尺寸最小的320x480 最大的1 ...
- 使用MEF实现通用参数设置
通用后台管理系统必备功能模块包含日志管理,权限管理,数据字典,参数配置等功能.参数设置主要用于设置系统运行所需的一些基础性配置项,比如redis缓存,mq消息队列,系统版本等信息.好的参数设置需要达到 ...
- php根据地址的经纬度查询周围的城市例子
目前的工作是需要对用户的一些数据进行分析,每个用户都有若干条记录,每条记录中有用户的一个位置,是用经度和纬度表示的.还有一个给定的数据库,存储的是一些已知地点以及他们的经纬度,内有43W多条的数据.现 ...
- ssh项目将搜索条件进行联动
<s:form namespace="/tb" action="tenderList" name="searchForm" id=&q ...