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这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...
随机推荐
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- PHP代码重用与函数编写
代码重用与函数编写 1.使用require()和include()函数 这两个函数的作用是将一个文件爱你载入到PHP脚本中,这样就可以直接调用这个文件中的方法.require()和include()几 ...
- 2016中国·西安“华山杯”WriteUp- SeeSea
题目打包下载:https://yunpan.cn/ckyrKxHJDPAIN (提取码:bbaf) Web 1.签到(10) 扫码回复 hs_ctf拿flag, 套路题. flag_Xd{hSh_ct ...
- AFNetWorking3.0源码分析
分析: AFNetWorking(3.0)源码分析(一)——基本框架 AFNetworking源码解析 AFNetworking2.0源码解析<一> end
- 基于xml的Spring多数据源配置和使用
上一篇讲了<基于注解的Spring多数据源配置和使用>,通过在类或者方法上添加@DataSource注解就可以指定某个数据源.这种方式的优点是控制粒度细,也更灵活. 但是当有些时候项目分模 ...
- 一个五年 Android 开发者百度、阿里、聚美、映客的面试心经
花絮 也许会有人感叹某些人的运气比较好,但是他们不曾知道对方吃过多少苦,受过多少委屈.某些时候就是需要我们用心去发现突破点,然后顺势而上,抓住机遇,那么你将会走向另外一条大道,成就另外一个全新的自我. ...
- ios9适配系列教程——ios9新变化
Demo1_iOS9网络适配_改用更安全的HTTPS iOS9把所有的http请求都改为https了:iOS9系统发送的网络请求将统一使用TLS 1.2 SSL.采用TLS 1.2 协议,目的是 强制 ...
- C#软件设计——小话设计模式原则之:单一职责原则SRP
前言:上篇C#软件设计——小话设计模式原则之:依赖倒置原则DIP简单介绍了下依赖倒置的由来以及使用,中间插了两篇WebApi的文章,这篇还是回归正题,继续来写写设计模式另一个重要的原则:单一职责原则. ...
- 解决win8 下 eclipse 中文字体太小的问题
一.把字体设置为Courier New 操作步骤:打开Elcipse,点击菜单栏上的“Windows”——点击“Preferences”——点击“Genneral”——点击“Appearance”— ...
- Nhibernate Query By Criteria 条件查询
HQL运算符 QBC运算符 含义 = Restrictions.eq() 等于equal <> Restrictions.ne() 不等于not equal > Restrictio ...