HDU 2665(Kth number-区间第k大[内存限制+重数])

 

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
 


 

这题……不想说什么.

题目还行主要是内存!!

HDU 2665(Kth number-区间第k大[内存限制+重数])

我应该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;

}


 

 

你可能感兴趣的:(number)