Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3155    Accepted Submission(s): 1055

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 case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.

The second line contains n integers, describe the sequence.

Each of following m lines contains three integers s, t, k.

[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
 
Output
For each test case, output m lines. Each line contains the kth big number.
 
Sample Input
1
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2
 
Sample Output
2
 
Source
 
Recommend
zty
 

这题……不想说什么.

题目还行主要是内存!!

我应该2分内存的……555……

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<cctype>
#include<map>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define RepD(i,n) for(int i=n;i>=0;i--)
#define MAXN (200000+10)
#define MAXM (200000+10)
int n,m,a[MAXN],a2[MAXN];
struct node
{
node *ch[2];
int a,siz;
node(){ch[0]=ch[1]=NULL;siz=a=0;}
void update()
{
siz=a;
if (ch[0]) siz+=ch[0]->siz;
if (ch[1]) siz+=ch[1]->siz;
}
}*null=new node(),*root[MAXN]={NULL},q[MAXN*9];
int q_s;
void make_node(node *&y,node *&x,int l,int r,int t)
{
if (x==NULL) x=null;
y=&q[++q_s];
*y=node();
int m=(l+r)>>1;
if (l==r)
{
*y=*x;
y->siz++;y->a++;
return;
}
if (t<=a2[m])
{
make_node(y->ch[0],x->ch[0],l,m,t);
y->ch[1]=x->ch[1];
y->update();
}
else
{
make_node(y->ch[1],x->ch[1],m+1,r,t);
y->ch[0]=x->ch[0];
y->update();
}
}
void find(node *&x1,node *&x2,int l,int r,int k)
{
if (x1==NULL) x1=null;
if (x2==NULL) x2=null;
if (l==r) {printf("%d\n",a2[l]);return;}
int m=(l+r)>>1;
int ls=0;
if (x2->ch[0]) ls+=x2->ch[0]->siz;
if (x1->ch[0]) ls-=x1->ch[0]->siz;
if (ls>=k) find(x1->ch[0],x2->ch[0],l,m,k);
else find(x1->ch[1],x2->ch[1],m+1,r,k-ls);
}
void print(node *x,int _x)
{
cout<<_x<<':'<<x->siz<<' ';
//cout<<'<';
if (x->ch[0]!=null) print(x->ch[0],_x*2);
//cout<<'>';
if (x->ch[1]!=null) print(x->ch[1],_x*2+1);
//cout<<'>';
}
int main()
{
//freopen("hdu2665.in","r",stdin);
null->ch[0]=null; null->ch[1]=null;
int t;
scanf("%d",&t);
while (t--)
{
q_s=0;
scanf("%d%d",&n,&m);
For(i,n) scanf("%d",&a[i]),a2[i]=a[i];
sort(a2+1,a2+1+n);
int size=unique(a2+1,a2+1+n)-(a2+1);
For(i,n)
{
make_node(root[i],root[i-1],1,size,a[i]);
}
//For(i,n) print(root[i],1),cout<<endl;
For(i,m)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
find(root[l-1],root[r],1,size,k);
}
For(i,n) root[i]=NULL;
}
return 0;
}

HDU 2665(Kth number-区间第k大[内存限制+重数])的更多相关文章

  1. HDU 2665.Kth number 区间第K小

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. hdu 2665 Kth number

    划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...

  3. 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )

    在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...

  4. HDU 2665 Kth number(主席树静态区间第K大)题解

    题意:问你区间第k大是谁 思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的. 具体可以看q学姐的B站视频 代码: #include<cmath> #i ...

  5. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

  6. hdu 2665 Kth number(划分树模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ]  改变一下输入就可以过 http://poj.org/problem? ...

  7. HDU 2665 Kth number(可持续化线段树)

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. HDU - 2665 Kth number 主席树/可持久化权值线段树

    题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...

  9. HDU 2665 Kth number(划分树)

    Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

随机推荐

  1. System类基础

    取时间差: public class SystemDemo01 {     public static void main(String[] args) {         long startTim ...

  2. yii2.0 从控制器到视图的输出

    在controllers/SiteController.php文件中,添加 public function actionSay($message = 'Hello') { return $this-& ...

  3. 有关extern的用法

    1.引言 C++语言的创建初衷是“a better C”,但是这并不意味着C++中类似C语言的全局变量和函数所采用的编译和连接方式与C语言完全相同.作为一种欲与C兼容的语言, C++保留了一部分过程式 ...

  4. 安装CAD出现Error 1904.Module的解决方法

    在安装AutoCAD2008时,安装过程中出现了一个小错误,虽然说不影响使用,也不影响功能,但还是需要把这个问题解决,今天就和大家分享解决这个问题的方法. 错误描述 会在安装过程中出现错误提示:Err ...

  5. QT5.4 计算器程序 打包&发布,解决dll的最新解决方案(图文并茂,很清楚)

    QT写界面还是很不错,就是打包会比较麻烦,折腾了一天总算是打包完成了. QT软件的打包发布一个难点是必备dll文件的识别,现在高版本QT自带了一个windeployqt工具,直接会把需要的dll生成一 ...

  6. javax.ejb.SessionBean

    import javax.ejb.SessionBean;import javax.ejb.SessionContext;import javax.ejb.CreateException; //有状态 ...

  7. CSSBox - Java HTML rendering engine

    CSSBox - Java HTML rendering engine CSSBox is an (X)HTML/CSS rendering engine written in pure Java. ...

  8. 第七届河南省赛10403: D.山区修路(dp)

    10403: D.山区修路 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 69  Solved: 23 [Submit][Status][Web Bo ...

  9. Android 中万能的 BaseAdapter(Spinner,ListView,GridView) 的使用!

    大家好!今天给大家讲解一下BaseAdapter(基础适配器)的用法,适配器的作用主要是用来给诸如(Spinner,ListView,GridView)来填充数据的.而(Spinner,ListVie ...

  10. 在 win 10 中使用sql 2012 附加低版本数据失败的解决办法。

    随着win 10 的发布,我也尝试把自己的笔记本升级下,体验win10,由于自己电脑好长时间没有管理过,东西比较乱,一激动就格式了硬盘.但是所有的资料都丢失了,不过我都提前备份到网盘上.好了,废话不多 ...