HDU5296 Annoying problem(LCA)】的更多相关文章

//#pragma comment(linker, "/STACK:1677721600") #include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <ctime> #include <vector> #include <cstdio> #include <cct…
Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 483    Accepted Submission(s): 148 Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,-,n, which…
dfs一遍得到每一个节点的dfs序,对于要插入的节点x分两种情况考虑: 1,假设x能够在集合中的某些点之间,找到左边和右边距离x近期的两个点,即DFS序小于x的DFS序最大点,和大于x的DFS序最小的点...... 2.假设x在集合中的点某一側,则找距离x的dfs序最小和最大的点 将x插入这个集合最少要走的距离为 dist[x]-dist[LCA(left,x)]-dist[LCA(right,x)]+dist[LCA(left,right)] 删除同理 Annoying problem Tim…
题解链接 Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 480    Accepted Submission(s): 146 Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,-,n, w…
题意: 给一棵n个节点的树,再给q个操作,初始集合S为空,每个操作要在一个集合S中删除或增加某些点,输出每次操作后:要使得集合中任意两点互可达所耗最小需要多少权值.(记住只能利用原来给的树边.给的树边已经有向.10万个点,10万个操作) 思路:只能用 O(nlogn)的复杂度.官方题解: 重点也就是要找到集合S中的以x和y为端点一条链,使得操作点u到达这条链是最近的.删除也是这样,找到这条链,删除u到这链的路长. 步骤: (1)记录从根遍历的DFS序. (2)计算每个点到根的路径所经过边的权之和…
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, whose vertices are conveniently labeled by 1,2,-,n. There are m chain on the tree, Each chain has a certain weight. Coco would like to pick out some ch…
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5296 Description Coco has a tree, whose nodes are conveniently labeled by 1,2,-,n, which has n-1 edge,each edge has a weight. An existing set S is initially empty. Now there are two kin…
Annoying problem Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,…,n, which has n-1 edge,each edge has a weight. An existing set S is initially empty.Now there are two kinds of operation: 1 x: If the node x is not in…
Annoying problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5296 Mean: 给你一个有根树和一个节点集合S,初始S为空,有q个操作,每个操作要么从树中选一个结点加入到S中(不删除树中节点),要么从S集合中删除一个结点.你需要从树中选一些边组成集合E,E中的边能够是S中的节点连通.对于每一个操作,输出操作后E中所有边的边权之和. analyse: 首先是构图,把树看作一个无向图,使用邻接表存图. 处理出…
Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 203    Accepted Submission(s): 60 Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,-,n, which h…
Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1006    Accepted Submission(s): 330 Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,…,n, which…
LCA+RMQ.挺不错的一道题目. 思路是如何通过LCA维护费用.当加入新的点u是,费用增量为dis[u]-dis[lca(u, lower_u)] - dis[lca(u, greater_u)] + dis[lca(lower_u, greater_u)].若beg[u]大于当前最大值或小于最小值,lower_u=min of current, greater_u = max of current. /* 5296 */ #include <iostream> #include <s…
最常见的LCA(树上公共祖先)都是在线算法,往往带了一个log.有一种办法是转化为"+-1最值问题"得到O(n)+O(1)的复杂度,但是原理复杂,常数大.今天介绍一种允许离线时接近线性求LCA的方法. 一个点和其他点的LCA必定是它到root路径上的所有节点之一,而另一个节点刚好在哪个节点下,LCA就是谁: 如图,标粗的箭头为当前搜索的路径,左边为已经搜索完毕的路径,右边的黑色节点尚未搜索.现在要求节点cur和节点a的LCA,显然a是什么颜色,LCA就也是这个颜色,如果a还没有被搜索到…
2015 Multi-University Training Contest 1 A.OO's Sequence 计算每个数的贡献 找出第\(i\)个数左边最靠右的因子位置\(lp\)和右边最靠左的因子位置\(rp\) 对答案的贡献就是\((rp-i)*(i-lp)\) 最后答案就是\(\sum_{i=1}^{n}(rp_i-i)*(i-lp_i)\) 预处理出来所有的因子,复杂度\(O(n\sqrt{10000})\) //#pragma GCC optimize("O3") //#…
from: Working with Scala's XML Support 虽然这个guy炒鸡罗嗦,但是还是讲到我要的那句话:  Because Scala doesn't support XML patterns with attributes. scala的模式匹配模式根本就不支持 属性 还是老老实实用XPath吧 XML is probably one of Scala's most controversial language features (right behind unrest…
原文地址:http://www.xuebuyuan.com/2039420.html CFURLCreateStringByAddingPercentEscapes 在作项目的的时候,一般都要用到网络搜索,所以链接(也就是NSURL)也会总是存在一些中文或者特殊字符,但是对于网址是不允许存在一些特殊字符的,所以在这里我列出一个对一个字符串进行NSUTF-8转码的宏,希望可以给大家提供方便. If you have tried to send any information using a GET…
URL 编码:CFURLCreateStringByAddingPercentEscapes If you have tried to send any information using a GET web request, you would have come cross an annoying problem, That annoying problem is making sure that the URL is corrently encoded. The issue is that…
---------- HDU 5288 OO’s Sequence 题意 给定一个数列(长度<$10^5$),求有多少区间[l,r],且区间内有多少数,满足区间内其它数不是他的约数. 数的范围$[1,10000]$.结果对$10^9+7$取模. Solution 首先可以预处理每个数的约数有哪些.对每个数存下它的位置有哪些. 然后可以枚举每一个位置,利用二分找到左边第一个是它约数的数,和右边第一个是他约数的数. 假设向左有L个数,向右有R个数.那么ans应该加上(L*R). 时间复杂度$O(n*…
摘自:有用到 http://blog.csdn.net/besily/article/details/5396268 PHP错误:Warning: Cannot modify header information - headers already sent by ... 2010-03-19 14:08 9779人阅读 评论(3) 收藏 举报 如果在执行php程序时看到这条警告:"Warning: Cannot modify header information - headers alrea…
如果在执行php程序时看到这条警告:"Warning: Cannot modify header information - headers already sent by ...." Few notes based on the following user posts:有以下几种解决方法: 1. Blank lines (空白行):Make sure no blank line after <?php ... ?> of the calling php scrīpt.检…
What is PHP-FPM? PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. These features include: Adaptive process spawning (NEW!) Basic statis…
https://blog.richardszalay.com/2007/03/08/dealing-with-exceptions-thrown-in-application_start/ One annoying problem I've noticed in the past is that if an exception is thrown in Application_Start() the application does not restart, resulting in subse…
算法编程Algos Programming 不同算法的集合,用于编程比赛,如ACM ICPC. 算法按主题划分.大多数算法都可以从文件中按原样运行.每种算法都有一个参考问题,并对其时间和空间复杂度作了一些说明. 参考:https://github.com/ADJA/algos 算法列表List of algorithms 动态(优化,规划)编程Dynamic Programming Convex Hull trick – Convex Hull trick is a geometry based…
[BZOJ4999]This Problem Is Too Simple! Description 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将i节点的值改为x. 2. Q i j x(0<=x<2^31) 表示询问i节点到j节点的路径上有多少个值为x的节点. Input 第一行有两个整数N,Q(1 ≤N≤ 100,000:1 ≤Q≤ 200,000),分别表示节点个数和操作个数. 下面一行N个整数,表示初始时每个节点的初…
Codeforce 472D Design Tutorial: Inverse the Problem 解析含快速解法(MST.LCA.思維) 今天我們來看看CF472D 題目連結 題目 給你一個\(n\times n\)的矩陣代表點\(i\)到點\(j\)的最短距離.問是否可以造出一棵邊權為正的樹. 前言 這題的輸入,輸入3e6個Integer經過實測就大概需要700ms以上了(如果沒開輸入優化好像還會直接TLE的樣子).並且我一開始是用Prim's Algo去寫MST的,priority_q…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形dp, 对于每条链u,v,w,我们只在lca(u,v)的顶点上处理它 让dp[i]表示以i为根的指数的最大值,sum[i]表示dp[vi]的和(vi为i的儿子们) 则i点有两种决策,一种是不选以i为lca的链,则dp[i]=sum[i]. 另一种是选一条以i为lca的链,那么有转移方程:dp[i]=…
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5293 被这题打蹦了,看着题解写的,很是爆炸,确实想不到,我用的DFS序+LCA+树形DP,当然也可以写树剖,不过这里DFS序更简单,因为都是对点到根的操作 #include<cstdio> #include<vector> #include<algorithm> #pragma comment(linker, "/STACK:102400000,102400000…
题意:一棵树,每个点都有自己val(1 <= val <= 1e5),而任意两个点u,v可以对lca(u,v) 产生gcd(valu,valv)的贡献,求每个点能接受到来自子树贡献的最大值. 分析:一个数w和其整数数倍的数gcd值还是w.记录每个值对应树的下标,枚举1- max{val}中的数,遍历其整数倍中两两结点的lca,检查其是否需要被更新. 其中遍历LCA的操作并不用二重循环枚举顶点,可以将顶点按dfs序排序后遍历其中相邻两点的LCA. 这里使用了RMQ的方式O(1) 查询LCA. #…
题目链接:点击打开链接 题意:给定n个点 m个询问 以下n-1行给定一棵树 m个询问 x y 问把树转成以x为根 y的父节点是谁 第一种情况lca==y那就是x的第 dep[x] - dep[y] -1 父亲,依次向上爬山坡,利用倍增的二进制加速. 另外一种就是Father[y]; #include"cstdio" #include"iostream" #include"queue" #include"algorithm" #…
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) , freopen(s".out","w",stdout) #define maxn 300000 using namespace std; struct OPT { int type; int a,b,c,tag; OPT(int type=0,int a=0,int…