HDU2665 kth number 线段树做法】的更多相关文章

题意:求区间第k小 思路: 线段树 每个节点上保存 当前区间已经排序好的序列 (归并一下就好了嘛 复杂度 O(l)的) 这样建树的时空复杂度都是 O(nlogn)的 对于 每次询问 二分一个答案 在树上upper_bound一下 判断一下 这样 查询的复杂度 就成O(m*log(inf) * log(n) * log(n))的了 就酱~ 但是POJ死也卡不过去--桑心 //By SiriusRen #include <vector> #include <cstdio> #inclu…
K-th Number 题意:给定一个包含n个不同数的数列a1, a2, ..., an 和m个三元组表示的查询.对于每个查询(i, j, k), 输出ai, ai+1, ... ,aj的升序排列中第k个数 . 题解:用线段树,每个节点维护一个区间并且保证内部升序,对于每次查询x,返回该区间小于x的数的个数.就这样不断二分,直到找到x为止. 线段树(归并树)+二分查找 #include <iostream> #include <cstdio> #include <cstrin…
题目链接 K-th Number #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i) #define lson i << 1, L, mid #define rson i << 1 |…
http://poj.org/problem?id=2104 由于这题的时间限制不紧,所以用线段树水一水. 每个节点保存的是一个数组. 就是对应区间排好序的数组. 建树的时间复杂度需要nlogn 然后查询的时候,对于线段树覆盖了的区间,可以直接二分即可. 查询复杂度需要logn^2 所以复杂度需要mlognlogn 对于怎么确定是那个元素.可以二分一个值val 然后查询在个val在这个区间是否第k大即可. 所以复杂度需要mlognlognlog1e9 开始的时候,不知道怎么确定2.3.5.6的第…
解题关键:划分树模板题. #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…
poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树   刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后的线段树 然后有可加减性 也就是说你每添加的一个点的那棵树都给你保留下来了 呃 ... 这么说好像还是有点生涩 那么就拿poj2104来举例子吧 慢慢讲我觉得会很好的 题意就是给你一个100000长度的数字 然后100000次询问[L,R]之间第k大的数字是多少 这个很容易看出来 暴力根本不可以 黑…
poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是主席树,就先要知道线段树,主席树就是n棵线段树,因为线段树只能维护最大值或者最小值,要想求出第二大的数字怎么办呢?两颗线段树呗!好,那么第n大呢,就可以构造n棵线段树,这样的内存是显然会爆掉的,那么怎么办呢?因为每一次更新都是更新的是从叶子节点到根节点的一条路,路的长度大约是logn,如下图红色的为…
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对最少的个数. 前置技能 环序列 还 线段树的逆序对求法 逆序对:ai > aj 且 i < j ,换句话说数字大的反而排到前面(相对后面的小数字而言) 环序列:把第一个放到最后一个数后面,就是一次成环,一个含有n个元素序列有n个环序列. 线段树的逆序对求法:每个叶子节点保存的是当前值数字的个数.根…
[POJ2104][HDU2665]K-th Number 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 quickly k-th…
Problem Kth Number Solution 裸的主席树,模板题.但是求k大的时候需要非常注意,很多容易写错的地方.卡了好久.写到最后还给我来个卡空间. 具体做法参见主席树论文<可持久化数据结构研究>. AC Code #include "cstdio" #include "iostream" #include "cstring" #include "algorithm" using namespace…
题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久化线段树/函数式线段树的这种实现方式 被用来求区间第$K$大的时候,是可持久化的权值线段树 主席树的特点就是 $[l,r]$的区间信息可以通过创造的第$r$颗树的减去第$l-1$颗树得到 具体思路就是: 将数列的值进行排序,去重 对于原序列的每一个值,依次插入可持久化的权值线段树,将它所在排序位置权…
Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5425    Accepted Submission(s): 1760 Problem Description Give you a sequence and ask you the kth big number of a inteval.   Input The…
题目链接: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…
K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 51440   Accepted: 17594 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse…
K-th Number Input The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000). The second line contains n different integer numbers not exceeding 109 b…
http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ]  改变一下输入就可以过 http://poj.org/problem?id=2104 http://poj.org/problem?id=2761 Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submissio…
题目描述 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 quickly k-th order statistics in the array segmen…
K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 59327   Accepted: 20660 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse…
仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15527 Accepted Submission(s): 9471 Problem Description The inversion number of a…
主席树 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 quickly k-th order statistics in the a…
K-th Number 题意: 给你一些数,让你求一个区间内,第k大的数是多少. 题解: 主席树第一题,看的qsc视频写的,戳戳戳 学到了unique函数,他的作用是:把相邻的重复的放到后面,返回值是放后面的第一个的迭代器. 故使用之前要排序,之后在用erase删除后面重复的,便可达到去重的目的,之后就可以离散化了. 代码: #include<iostream> #include<cstdio> #include<vector> #include<algorith…
K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 44952   Accepted: 14951 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse…
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 37 Accepted Submission(s): 25   Problem Description Give you a sequence and ask you the kth big number of a inteval.   Input The first l…
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 quickly k-th order statistics in the array segment. Th…
题目链接: 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…
Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Give you a sequence and ask you the kth big number of a inteval.   Input The first line is the number of the test cases. For each test…
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25092    Accepted Submission(s): 14816 Problem Description The inversion number of a given number sequence a1, a2, ..., a…
明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8324    Accepted Submission(s): 5115 Problem Descri…
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…
题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; ; ][mx]; int sortt[mx]; ][mx]; void build(int l,int r,int c) { if (l==r) return ; ; ; ; for (int i…