两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R],对这个区间排序后,我们构造出了线段树, 每个值在这个子区间出现了几次都可以从线段树中查询,我们就可以在对数时间解决询问. 现在的问题就是怎样在时空复杂度O(nlogn)的时间内构造出这么多的线段树! 1.首先对原序列a排序并删去重复得到新序列b 2.对a的每个前缀构造线段树,即a的前i个数出现在…
K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 44940   Accepted: 14946 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse…
题意:问你区间第k大是谁 思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的. 具体可以看q学姐的B站视频 代码: #include<cmath> #include<set> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include <iostream> #include<algorit…
K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 43315   Accepted: 14296 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse…
[转载]一篇还算可以的文章,关于可持久化线段树http://finaltheory.info/?p=249 无修改的区间第K大 我们先考虑简化的问题:我们要询问整个区间内的第K大.这样我们对值域建线段树,每个节点记录这个区间所包含的元素个数,建树和查询时的区间范围用递归参数传递,然后用二叉查找树的询问方式即可:即如果左边元素个数sum>=K,递归查找左子树第K大,否则递归查找右子树第K – sum大,直到返回叶子的值. 现在我们要回答对于区间[l, r]的第K大询问.如果我们能够得到一个插入原序…
题目链接:http://poj.org/problem?id=2104 主席树入门题目,主席树其实就是可持久化权值线段树,rt[i]维护了前i个数中第i大(小)的数出现次数的信息,通过查询两棵树的差即可得到第k大(小)元素. #include<cstdio> #include<vector> #include<algorithm> using namespace std; #define lson(i) node[i].lson #define rson(i) node…
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss’s castle as a line (the length is n), on…
主席树入门 时隔5个月,我又来填主席树的坑了,现在才发现学算法真的要懂了之后,再自己调试,慢慢写出来,如果不懂,就只会按照代码敲,是不会有任何提升的,都不如不照着敲. 所以搞算法一定要弄清原理,和代码为什么要这么写. 下面是我找的一篇博客,写的真的很好,我就是用这个看懂的,但我还觉得有几点注意要说: 一: 你要想学主席树,就一定要会灵活运用线段树,这里也有一篇线段树的博客:点这里 我认为当你把这上面给出代码的题都自己写了一遍之后 你就算是可以灵活运用线段树了. 二: 下面这篇博客给出的那个例子非…
Jewel Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 985    Accepted Submission(s): 247 Problem Description Jimmy wants to make a special necklace for his girlfriend. He bought many beads with…
K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse…
poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是主席树,就先要知道线段树,主席树就是n棵线段树,因为线段树只能维护最大值或者最小值,要想求出第二大的数字怎么办呢?两颗线段树呗!好,那么第n大呢,就可以构造n棵线段树,这样的内存是显然会爆掉的,那么怎么办呢?因为每一次更新都是更新的是从叶子节点到根节点的一条路,路的长度大约是logn,如下图红色的为…
HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2那个. 思路: 主席树操作,这里的思路是从n到1开始建树.其他就是主席树查询区间第K小,计算区间不同值个数. #include <algorithm> #include <iterator> #include <iostream> #include <cstring&…
主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\(O(nlogn)\). 动态整体kth: 权值线段树维护一下,时间复杂度\(O(nlogn)\). 静态区间kth: 主席树维护,时间复杂度\(O(nlogn)\). 动态区间kth: 就是本次的标题. 回忆一下主席树是如何维护静态区间kth的. 建立可持久化线段树后,利用前缀和的思想查询区间的k…
题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<iostream> #include<stdio.h> #include<algorithm> #include<vector> using namespace std; # define ll long long ; struct node { int sum; in…
例题:http://poj.org/problem?id=2104 最近可能是念念不忘,必有回响吧,总是看到区间第k大的问题,第一次看到是在知乎上有人面试被弄懵了后来又多次在比赛中看到.以前大概是知道怎么解决但是没有实际操作过.直到昨天看到了POJ上的2104题,一个标准的区间第K大询问,然后好好总结了一下区间第K大的问题. 普通人要是没想过这个问题,突然被问到第一个反应肯定和知乎上面试的哥们儿一样,把区间里面的所有数拎出来,排序,找第K个,但是这样时间复杂度是很大的,如果m次询问,时间复杂度是…
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data…
题目大意: 求序列的区间第k大 基本思路: 因为我根本就没有思路,知道这是主席树,我就去学了下,在b站上看了uestc的教学视频,然后看了一篇博客,博客http://www.cnblogs.com/Empress/p/4652449.html,我觉得理解的无修改主席树差不多了,也没那么难.下面代码是按照b站上来的,我很喜欢他的离散化方式,不过我不喜欢他的代码风格. 下面阐述我所理解的主席树的基本思路和细节: 转自:http://www.cnblogs.com/Empress/p/4652449.…
解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> using namespace std; typedef long long ll; ; ][MAXN],sorted[MAXN],toleft[][MAXN]; void build(int l…
题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickl…
Kth number                                                 Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)                                                                        Total Submission(s): 10682    Accep…
K-th Number 题意: 给你一些数,让你求一个区间内,第k大的数是多少. 题解: 主席树第一题,看的qsc视频写的,戳戳戳 学到了unique函数,他的作用是:把相邻的重复的放到后面,返回值是放后面的第一个的迭代器. 故使用之前要排序,之后在用erase删除后面重复的,便可达到去重的目的,之后就可以离散化了. 代码: #include<iostream> #include<cstdio> #include<vector> #include<algorith…
poj-2104(区间第K大问题) #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; struct T { int ls; int rs; int sum; }; ; T tri[*N]; int a[N],sort_a[N]; int rt[N]; int cnt; int n,m; void update (…
K-th Number Time Limit: 20000MS Memory Limit: 65536K Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data stru…
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一个带边权的图.对于每一个询问(S , T , K , A , B),有两种操作,加一条单位边花费为A,将某条边流量扩展一个单位花费为B,在预算为K的情况下求S到T最大流的最大值. http://acm.hdu.edu.cn/showproblem.php?pid=4729 做法:如果A <= B,很明显只做添加边操作,而且只加在S -…
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5412 Problem Description There are N boys in CodeLand. Boy i has his coding skill Ai. CRB wants to know who has the suitable coding skill. So you should treat the following two types of queries. Query 1…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417   Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his love…
和向左密集比起来向右密集只需要进行小小的额修改,就是更新的时候从右往左更新.. 自己写的被卡死时间.不知道怎么回事,和网上博客的没啥区别.. /* 给定一个n个数的序列a 每次询问区间[l,r],求出去重后区间中每个数的第一次出现的位置pi pi构成一个新的升序,求这个新数组的中位数 要求强制在线,询问[l,r]根据上一个询问的答案进行加密,形成新的询问 t1=(l+ansi-1) % mod n + 1 t2=(r+ansi-1) % mod n + 1 l=min(t1,t2),r=max(…
题意:在区间中找一个数,求出该区间每个数与这个数距离的总和,使其最小 找的数字是中位数(若是偶数个,则中间随便哪个都可)接着找到该区间比此数大的数的总和 区间中位数可以使用划分树,然后在其中记录:每层的 1-i 中划分到左区间的总和 划分树: 划分树是一种基于线段树的数据结构.主要用于快速求出(在log(n)的时间复杂度内)序列区间的第k大值 . 划分树和归并树都是用线段树作为辅助的,原理是基于快排 和归并排序 的. 划分树的建树过程基本就是模拟快排过程,取一个已经排过序的区间中值,然后把小于中…
题目链接: https://www.luogu.org/problem/P3834 首先要离散化,然后主席树模板. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define mid (l+r)/2 5 using namespace std; 6 7 const int N = 200010; 8 int n, q, m, cnt = 0; 9 int a[N], b[N], T[N];…
Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They…