/** problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966 裸板 **/ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<vector> using namespace std; ; template <typename T> class SegmentTree { private: struct Node…
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #inc…
题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2:( D, i, j, x ) 从i到j的路径上经过的节点全部都减去x: 3:(Q, x) 查询节点x的权值为多少? 解题思路: 可以用树链剖分对节点进行hash,然后用线段树维护(修改,查询),数据范围比较大,要对线段树进行区间更新 #include <cstdio> #include <…
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10483    Accepted Submission(s): 2757 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor…
Aragorn's Story Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M…
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 <algorit…
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3494    Accepted Submission(s): 973 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lord…
树上路径区间更新,单点查询. 线段树和树状数组都可以用于本题的维护. 线段树: #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #define lson rt<<1 #define rson rt<<1|1 #define Lson l,m,lson #define Rson m+1,r,rson #define max(a,b) a&…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. //树链剖分 边权修改 单点查询 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; ; struct data { int to ,…
一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都会强行写3遍左右...) 二.题意 阿拉贡同学发现一棵树装的若干营地有了若干敌人(可以是负数),这些敌人的变化情况会使用类似于“A-B之间的路上的所有营地都增加某个数量的人数”来进行.最后任意时间有询问:某个点当前有多少人? 三.题解 树链拋分的思路是这样的:首先把一棵树,对于每个节点都将会有:将子…
/** problem: http://poj.org/problem?id=2763 **/ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<vector> using namespace std; ; template <typename T> class SegmentTree { private: struct Node { int left, right;…
HDU3669 Aragorn's Story 树链剖分 点权 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: n个点的,m条边,每个点都 有点权 修改 从u->v上所有点的点权 查询单点点权 题解: 树链剖分裸题 树链剖分就是将树分割为多条边,然后利用数据结构来维护这些链的一个技巧 重儿子:父亲节点的所有儿子中子树结点数目最多( size*siz**e* 最大)的结点: 轻儿子:父亲节点中除了重儿子以外的儿子: 重边:父亲结点和重儿…
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <vector> #define ll long…
Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.php?pid=3966 这题就是一个模板题,模板调过了就可以过 #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string.h> #include&l…
题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意: 给你一棵树,一开始每条边都是白色,有三种操作: 1.将 u - v路径上的边转换颜色 2.将 u - v路径上相邻的边转换颜色 3.求 u - v 路径上黑色边的数量 思路: 好变态的一道树链剖分啊....写自闭了 首先树链剖分,用点表示边.然后就是非常绕的轻重链的维护了,我们用两棵线段树来进行维护,一颗维护重链上的,一棵维护轻链上的标记值. 第一种操作,重链的话直接线段树区间操作…
题目传送门 题目大意: 有n个兵营形成一棵树,给出q次操作,每一次操作可以使两个兵营之间的所有兵营的人数增加或者减少同一个数目,每次查询输出某一个兵营的人数. 思路: 树链剖分模板题,讲一下树链剖分过程的理解. 第一步,dfs,记录每个节点的父节点,子节点数目,重子节点,树的深度. 第二步,dfs,处理出dfs序和轻重链的起点,重链形成一条,轻链的起点就是本身,处理dfs序的时候先处理重链的dfs序. 修改,利用树状数组(或者线段树),个人认为这是最难的地方. inline void chang…
Minimum Cut Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total Submission(s): 895    Accepted Submission(s): 387 Problem Description Given a simple unweighted graph G (an undirected graph containing no loops nor…
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp[u][1]表示考虑经过u节点该子树的最优值(可能过,可能不过),很明显:\[dp[u][0] = \sum\{max(dp[v][0], dp[v][1])\} v是u的儿子\], 下面来算dp[u][1]: 考虑一条经过u(以u为lca)的链,他经过子树中的节点v(可能有多个),那么\[dp[u…
Little Devil I Problem Description There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love…
Dylans loves tree view code#pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <…
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1915    Accepted Submission(s): 492 Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes…
Dylans loves tree Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes on tree is numbered by 1∼N. Then he is given Q questions like that: ①0 x y:change node x′s value to y ②1 x y:For all the value in the path fr…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:一颗树上,每个点有权值,定义三种操作: 1)I操作表示从a到b节点之间的节点都加上一个值 2)D操作表示从a到b节点之间的节点的都减去一个权值 3)Q操作询问a节点当前的值. 分析:树链剖分裸题,在树上修改点权及询问点权,将树上信息映射到线段上后,就是一道水线段树题了. #include <cstdio> #include <cstring> #include <st…
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2082 树链剖分模版题,求和,修改单边权. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; typedef long long LL; ; struct EDGE { int to , next; LL cost; }edg…
提交 题意:给了两棵树,他们的跟都是1,然后询问,u,v 表 示在第一棵树上在u点往根节点走 , 第二棵树在v点往根节点走,然后求他们能到达的最早的那个共同的点 解: 我们将第一棵树进行书链剖,然后第二棵树采用主席树,他的信息来自他的父亲节点,每个点存他在第一棵树 树链剖分后的位置,这样我们每次查询uv的时候我们只要 我们选取u和top[u]这段区间在主席树v这颗树上找,在这个区间能取到的最大值,一旦存在,这个最大值就我们要的,这个点保存着他到根节点这条路上所有点在第一棵树剖分后的位置 #inc…
2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 7971  Solved: 2990 [Submit][Status][Discuss] Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“112221”由3段组成:“11”.“222”和“1”. 请你写一个程序依…
思路: 感觉这题也可神了.. (还是我太弱) 首先发现每一位不会互相影响,可以把每一位分开考虑,然后用树链剖分或者LCT维护这个树 修改直接修改,询问的时候算出来每一位填0,1经过这条链的变换之后得到的值 考虑贪心,从高往低,如果这一位填0可以得到1,那么填0一定是最优的 否则如果可以填1,就把这一位填为1 复杂度是nklog^2n或者nklogn,只能通过50%的数据 发现可以并行计算这k位,复杂度降为nlog^2n的树链剖分或者nlogn的LCT,可以通过100%的数据 这个题没有卡常,合并…
题意:给出一棵树,这棵树每个点有权值,然后有3种操作.操作一:修改树根为rt,操作二:修改u到v路径上点权值为w,操作三:询问以rt为根x子树的最小权值. 解法:如果没有修改树根操作那么这题就是树链剖分的裸题.但是修改树根操作会使得题目变得复杂一些,这里直接说结论:我们先直接以1为根建树进行树链剖分,这样的话根固定了那么路径修改操作就照常,然后我们要考虑换根操作对查询的影响(这是重点). 画图分析后可以发现,可以分为3种情况,①x==rt,询问的就是整棵树  ②x不在1到rt的路径上,对查询没有…
题目大意: https://www.luogu.org/problemnew/show/P3384 树链剖分的讲解 两个dfs() 修改 查询 很详细很好理解 https://www.cnblogs.com/George1994/p/7821357.html 不过上面的讲解没有完整的代码 没有说到 操作3和操作4 应该处理的区间 某棵子树在线段树中的对应区间 可以从 根节点的dfsID p[i] 及 根节点的儿子数量 num[i] 得到 即若子树的根节点为 i 那么对应的区间应该是 ( p[i]…
pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定一个棵树,然后三种操作 Q x:查询节点x的值 I x y w:节点x到y这条路径上全部节点的值添加w D x y w:节点x到y这条路径上全部节点的值降低w 解题思路:树链剖分,用树状数组维护每一个节点的值. #pragma comment(linker, "/STACK:1024000000,1…