SPOJ913 Query on a tree II】的更多相关文章

Time Limit: 433MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, represe…
SP913 QTREE2 - Query on a tree II 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点b有向路径上的第k个点的编号 有多组测试数据,每组数据以DONE结尾. 裸的LCA. 在处理第二个操作时,我直接向上数跳了多少个. 顾z大佬说不能这么做,要求出跳到那个点的深度再去跳. 真的是这样,不过懒得想了,应该是+1-1的误差. balabala... code: #include <iost…
Query on a tree II You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length. We will ask you to perfrom some instructions of th…
题目描述 [传送门] 题目大意 给一棵树,有两种操作: 求(u,v)路径的距离. 求以u为起点,v为终点的第k的节点. 分析 比较简单的倍增LCA模板题. 首先对于第一问,我们只需要预处理出根节点到各个节点之间的距离,然后倍增LCA求解就可以了. 那么第二问我WA了6发,原来是眼瞎和手残打错了两个字符错掉了. 我们将问题分成3个部分: LCA是第k个 第k个在u到LCA的路径上 第k个在LCA到v的路径上. 首先如果LCA是第k个,那么直接输出. 如果是第二种情况,那么从u开始做倍增,每一次k-…
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length. We will ask you to perfrom some instructions of the following form: D…
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length. We will ask you to perfrom some instructions of the following form: D…
Description 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点b有向路径上的第k个点的编号 有多组测试数据,每组数据以DONE结尾. Input 第一组数据包含一个整数\(T\),代表有\(T\)组测试数据.\(1\leq T \leq 25\) 对每一组测试数据: 第一行一个整数\(N(n \leq 10000)\) 接下来有\(N-1\)行,每一行描述树上的一条边\(a,b,c( c\leq 100…
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length. We will ask you to perfrom some instructions of the following form: D…
spoj题面 Time limit 433 ms //spoj的时限都那么奇怪 Memory limit 1572864 kB //1.5个G,疯了 Code length Limit 15000 B OS Linux Language limit All except: ERL JS-RHINO NODEJS PERL6 VB.NET Source Special thanks to Ivan Krasilnikov for his alternative solution Author Th…
QTREE2 经典的倍增思想 题目: 给出一棵树,求: 1.两点之间距离. 2.从节点x到节点y最短路径上第k个节点的编号. 分析: 第一问的话,随便以一个节点为根,求得其他节点到根的距离,然后对于每个询问(x,y),想求得lca(x,y),直接用dis[x]+dis[y]-2*dis[ lca(x,y) ]即可. 第二问的话,可以用倍增的方式求.我们通过求得节点x,y,lca(x,y)的深度,判断第k个节点落在哪个链上,该链是指是从x到根或者从y到根.最后倍增可以轻松求出一个链上第k个父亲是谁…
思路 第一个可以倍增,第二个讨论在a到lca的路径上还是lca到b的路径上, 倍增即可 代码 #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int jump[10010][16],sum[10010][16],fir[10010],nxt[10010*2],v[10010*2],w[10010*2],cnt,dep[10010],n; void addedge…
传送门 倍增水题…… 本来还想用LCT做的……然后发现根本不需要 //minamoto #include<bits/stdc++.h> using namespace std; #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) <<],*p1=buf,*p2=buf; inline int read(){ #define num ch-'0'…
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<set&…
P1501 [国家集训队]Tree II 题目描述 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\(v\)的路径上的点的权值都加上自然数\(c\): - u1 v1 u2 v2:将树中原有的边\((u_1,v_1)\)删除,加入一条新边\((u_2,v_2)\),保证操作完之后仍然是一棵树: * u v c:将\(u\)到\(v\)的路径上的点的权值都乘上自然数\(c\): / u v:询问\(u…
COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight. We will ask you to perform the following operation: u v : ask for how many different integers that represent…
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中size[v]是以v为根的子树的节点个数,全部由重边组成的路径是重路径,根据论文上的证明,任意一点到根的路径上存在不超过logn条轻边和logn条重路径. 这样我们考虑用数据结构来维护重路径上的查询,轻边直接查询. 通常用来维护的数据结构是线段树,splay较少见. 具体步骤 预处理 第一遍dfs 求…
Query on a tree Time Limit: 5000ms Memory Limit: 262144KB   This problem will be judged on SPOJ. Original ID: QTREE64-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Font Size: + - Type:   None Graph…
Query on a tree again! 给出一棵树,树节点的颜色初始时为白色,有两种操作: 0.把节点x的颜色置反(黑变白,白变黑). 1.询问节点1到节点x的路径上第一个黑色节点的编号. 分析: 先树链剖分,线段树节点维护深度最浅的节点编号. 注意到,如果以节点1为树根时,显然每条重链在一个区间,并且区间的左端会出现在深度浅的地方.所以每次查找时发现左区间有的话,直接更新答案. 9929151 2013-08-28 10:45:55 Query on a tree again! 100…
  Query on a tree Time Limit: 851MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Submit Status Description You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to per…
3637: Query on a tree VI Time Limit: 8 Sec  Memory Limit: 1024 MBSubmit: 206  Solved: 38[Submit][Status][Discuss] Description You are given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes are numbered from 1 to n. Each nod…
QTREE - Query on a tree #number-theory You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of t…
The Query on the Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 57    Accepted Submission(s): 20 Problem Description 度度熊近期沉迷在和树有关的游戏了.他一直觉得树是最奇妙的数据结构. 一天他遇到这样一个问题: 有一棵树,树的每一个点有点权,每次有三种操作:…
QTREE - Query on a tree 题目链接:http://www.spoj.com/problems/QTREE/ 参考博客:http://blog.sina.com.cn/s/blog_7a1746820100wp67.html 树链剖分入门题 代码如下(附注解): #include <cstdio> #include <cstring> #include <iostream> #define lson (x<<1) #define rson…
BZOJ_1803_Spoj1487 Query on a tree III_主席树 Description You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose label is k-th largest in the subtree of the node x. Assume no two nodes have the same labels. I…
QTREE5 - Query on a tree V 动态点分治和动态边分治用Qtree4的做法即可. LCT: 换根后,求子树最浅的白点深度. 但是也可以不换根.类似平常换根的往上g,往下f的拼凑 考虑深度的pushup必须考虑原树结构的联系,而ch[0],ch[1]又不是直接的前驱后继,每次pushup还要找前驱后继答案,还不如直接记下来. 故,节点里维护: 1.sz,大小 2.color节点颜色 3.set每个虚儿子贡献的最浅深度 4.lmn,rmn,当前x的splay子树最浅点和最深点的…
P1501 [国家集训队]Tree II 看着维护吧2333333 操作和维护区间加.乘线段树挺像的 进行修改操作时不要忘记吧每个点的点权$v[i]$也处理掉 还有就是$51061^2=2607225721>2147483647$ 所以要开unsigned int #include<iostream> #include<cstdio> #include<cstring> #define rint register int #define di unsigned i…
题目链接:http://www.spoj.com/problems/QTREE/en/ QTREE - Query on a tree #tree You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form:…
Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 733    Accepted Submission(s): 275 Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey…
SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<algorithm> #include<queue> #include<set> #include<map>…
BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑分块(莫队与分块真是基友) 我们按照深度为\(\sqrt{n}\)的子树分块,那么这一棵树最多不超过\(\sqrt{n}\)个块. 维护每一个块的根节点到树上每一个节点的答案,暴力即可.然后用可持久化块状数组维护一下遍历时出现的最深的颜色的深度. 查询答案的做法: 在一个块内,直接暴力查. 不在一个…