HDOJ 5296 Annoying problem LCA+数据结构】的更多相关文章

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=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 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): 483    Accepted Submission(s): 148 Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,-,n, which…
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…
//#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 题目连接: 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 Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5296 Mean: 给你一个有根树和一个节点集合S,初始S为空,有q个操作,每个操作要么从树中选一个结点加入到S中(不删除树中节点),要么从S集合中删除一个结点.你需要从树中选一些边组成集合E,E中的边能够是S中的节点连通.对于每一个操作,输出操作后E中所有边的边权之和. analyse: 首先是构图,把树看作一个无向图,使用邻接表存图. 处理出…
Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 11405    Accepted Submission(s): 5418 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, y…
Problem Description 度熊面前有一个全是由1构成的字符串,被称为全1序列.你可以合并任意相邻的两个1,从而形成一个新的序列.对于给定的一个全1序列,请计算根据以上方法,可以构成多少种不同的序列. Input 这里包括多组测试数据,每组测试数据包含一个正整数N,代表全1序列的长度. 1≤N≤200 Output 对于每组测试数据,输出一个整数,代表由题目中所给定的全1序列所能形成的新序列的数量. Sample Input 1 3 5 Sample Output 1 3 8 Hin…
差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1503    Accepted Submission(s): 647 Special Judge Problem Descr…
#include<stdio.h> int main() { int n,i,j,k; ],]; ]; while(scanf("%d %s %s",&n,in,out)!=EOF) { ];//stack维持着正常的进出栈 ;//栈顶 i=j=k=; &&i<n) { if(top!=-1&&stack[top]==out[i]) { top--; flag[k]=; k++; i++; } else { top++; fla…
这一套题做错了几次,按理说直接用等差数列求和公式就行了,主要是要考虑一些运算符的结核性问题: 四则运算符(+.-.*./)和求余运算符(%)结合性都是从左到右. 于是,我自己写了一个版本,主要是考虑(n+1)*n始终为偶数,这样就不用担心除以2时的取整问题: #include <stdio.h> int main(void) { int n; __int64 sum = ; while (scanf("%d", &n) != EOF) { sum = ((n + )…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 思路分析:该问题为裸的最大网络流问题,数据量不大,使用EdmondsKarp算法求解即可:需要注意的是该问题的点最多有15个,边的数目最多有1000个,所以该图中存在重边,需要将多个重边合为一条边: 代码如下: #include <queue> #include <vector> #include <cstdio> #include <climits> #…
考虑第1个火车出站的时刻,从1到n都有可能,如果它是第i个出栈,那么前面有规模为i-1的子问题,后面有规模为n-i的子问题.累加.…
两个数组存进出顺序,如果不同进栈,相同出栈.…
火车进站出站的问题满足卡特兰数...卡特兰数的相关知识如下: 卡特兰数又称卡塔兰数,是组合数学中一个常出现在各种计数问题中出现的数列.由以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)命名. 前几项为 (OEIS中的数列A000108): 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 176…
---------- HDU 5288 OO’s Sequence 题意 给定一个数列(长度<$10^5$),求有多少区间[l,r],且区间内有多少数,满足区间内其它数不是他的约数. 数的范围$[1,10000]$.结果对$10^9+7$取模. Solution 首先可以预处理每个数的约数有哪些.对每个数存下它的位置有哪些. 然后可以枚举每一个位置,利用二分找到左边第一个是它约数的数,和右边第一个是他约数的数. 假设向左有L个数,向右有R个数.那么ans应该加上(L*R). 时间复杂度$O(n*…
最常见的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") //#…
算法编程Algos Programming 不同算法的集合,用于编程比赛,如ACM ICPC. 算法按主题划分.大多数算法都可以从文件中按原样运行.每种算法都有一个参考问题,并对其时间和空间复杂度作了一些说明. 参考:https://github.com/ADJA/algos 算法列表List of algorithms 动态(优化,规划)编程Dynamic Programming Convex Hull trick – Convex Hull trick is a geometry based…
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…
摘自:有用到 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.检…