zoukankan      html  css  js  c++  java
  • POJ 题目2761 Feed the dogs(主席树||划分树)

    Feed the dogs
    Time Limit: 6000MS   Memory Limit: 65536K
    Total Submissions: 16860   Accepted: 5273

    Description

    Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on one line, numbered from 1 to n, the leftmost one is 1, the second one is 2, and so on. In each feeding, Jiajia choose an inteval[i,j], select the k-th pretty dog to feed. Of course Jiajia has his own way of deciding the pretty value of each dog. It should be noted that Jiajia do not want to feed any position too much, because it may cause some death of dogs. If so, Wind will be angry and the aftereffect will be serious. Hence any feeding inteval will not contain another completely, though the intervals may intersect with each other.

    Your task is to help Jiajia calculate which dog ate the food after each feeding.

    Input

    The first line contains n and m, indicates the number of dogs and the number of feedings.

    The second line contains n integers, describe the pretty value of each dog from left to right. You should notice that the dog with lower pretty value is prettier.

    Each of following m lines contain three integer i,j,k, it means that Jiajia feed the k-th pretty dog in this feeding.

    You can assume that n<100001 and m<50001.

    Output

    Output file has m lines. The i-th line should contain the pretty value of the dog who got the food in the i-th feeding.

    Sample Input

    7 2
    1 5 2 6 3 7 4
    1 5 3
    2 7 1
    

    Sample Output

    3
    2
    

    Source

    POJ Monthly--2006.02.26,zgl & twb

    求区间第k大值

    ac代码

    主席树版本号

    Problem: 2761		User: kxh1995
    Memory: 24508K		Time: 2813MS
    Language: C++		Result: Accepted

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    int a[100010],t[100010];
    int T[100010*30],lson[100010*30],rson[100010*30],c[100010*30];
    int n,m,cnt,tot;
    void init_hash()
    {
    	int i;
    	for(i=1;i<=n;i++)
    		t[i]=a[i];
    	sort(t+1,t+1+n);
    	cnt=unique(t+1,t+1+n)-t-1;
    }
    int build(int l,int r)
    {
    	int root=tot++;
    	c[root]=0;
    	if(l!=r)
    	{
    		int mid=(l+r)>>1;
    		lson[root]=build(l,mid);
    		rson[root]=build(mid+1,r);
    	}
    	return root;
    }
    int hash(int x)
    {
    	return lower_bound(t+1,t+1+cnt,x)-t;
    }
    int update(int root,int pos,int val)
    {
    	int newroot=tot++;
    	int temp=newroot;
    	c[newroot]=c[root]+val;
    	int l=1,r=cnt;
    	while(l<r)
    	{
    		int mid=(l+r)>>1;
    		if(pos<=mid)
    		{
    			lson[newroot]=tot++;
    			rson[newroot]=rson[root];
    			newroot=lson[newroot];
    			root=lson[root];
    			r=mid;
    		}
    		else
    		{
    			rson[newroot]=tot++;
    			lson[newroot]=lson[root];
    			newroot=rson[newroot];
    			root=rson[root];
    			l=mid+1;
    		}
    		c[newroot]=c[root]+val;
    	}
    	return temp;
    }
    int query(int l_root,int r_root,int k)
    {
    	int l=1,r=cnt;
    	while(l<r)
    	{
    		int mid=(l+r)>>1;
    		if(c[lson[l_root]]-c[lson[r_root]]>=k)
    		{
    			r=mid;
    			l_root=lson[l_root];
    			r_root=lson[r_root];
    		}
    		else
    		{
    			l=mid+1;
    			k-=c[lson[l_root]]-c[lson[r_root]];
    			l_root=rson[l_root];
    			r_root=rson[r_root];
    		}
    	}
    	return l;
    }
    int main()
    {
    	//int n,m;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		int i;
    		tot=0;
    		for(i=1;i<=n;i++)
    			scanf("%d",&a[i]);
    		init_hash();
    		T[n+1]=build(1,cnt);
    		for(i=n;i>0;i--)
    		{
    			int pos=hash(a[i]);
    			T[i]=update(T[i+1],pos,1);
    		}
    		while(m--)
    		{
    			int l,r,k;
    			scanf("%d%d%d",&l,&r,&k);
    			printf("%d
    ",t[query(T[l],T[r+1],k)]);
    		}
    	}
    }

    划分树版本号

    Problem: 2761		User: kxh1995
    Memory: 18988K		Time: 2000MS
    Language: C++		Result: Accepted

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int tree[30][100100],sorted[100010],toleft[30][100010];
    int cmp(const void *a,const void *b)
    {
    	return *(int *)a-*(int *)b;
    }
    void build(int l,int r,int dep)
    {
    	if(l==r)
    	{
    		return;
    	}
    	int mid=(l+r)>>1;
    	int same=mid-l+1;
    	int i;
    	for(i=l;i<=r;i++)
    		if(tree[dep][i]<sorted[mid])
    			same--;
    		int lpos=l;
    		int rpos=mid+1;
    		for(i=l;i<=r;i++)
    		{
    			if(tree[dep][i]<sorted[mid])
    			{
    				tree[dep+1][lpos++]=tree[dep][i];
    			}
    			else
    				if(tree[dep][i]==sorted[mid]&&same>0)
    				{
    					tree[dep+1][lpos++]=tree[dep][i];
    					same--;
    				}
    				else
    					tree[dep+1][rpos++]=tree[dep][i];
    				toleft[dep][i]=toleft[dep][l-1]+lpos-l;
    		}
    		build(l,mid,dep+1);
    		build(mid+1,r,dep+1);
    }
    int query(int L,int R,int l,int r,int dep,int k)
    {
    	if(l==r)
    	{
    		return tree[dep][l];
    	}
    	int mid=(L+R)>>1;
    	int cnt=toleft[dep][r]-toleft[dep][l-1];
    	if(cnt>=k)
    	{
    		int newl=L+toleft[dep][l-1]-toleft[dep][L-1];
    		int newr=newl+cnt-1;
    		return query(L,mid,newl,newr,dep+1,k);
    	}
    	else
    	{
    		int newr=r+toleft[dep][R]-toleft[dep][r];
    		int newl=newr-(r-l-cnt);
    		return query(mid+1,R,newl,newr,dep+1,k-cnt);
    	}
    }
    int main()
    {
    	int n,m;
    	int i;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		memset(tree,0,sizeof(tree));
    		for(i=1;i<=n;i++)
    		{
    			scanf("%d",&tree[0][i]);
    			sorted[i]=tree[0][i];
    		}
    		qsort(sorted+1,n,sizeof(sorted[1]),cmp);
    		build(1,n,0);
    		while(m--)
    		{
    			int a,b,k;
    			scanf("%d%d%d",&a,&b,&k);
    			int ans=query(1,n,a,b,0,k);
    			printf("%d
    ",ans);
    		}
    	}
    }


  • 相关阅读:
    [图形学] 结束 [Unity Shader] 开始
    [图形学] Chp18 OpenGL表面纹理函数
    [图形学] Chp17 OpenGL光照和表面绘制函数
    [图形学] Chp14 GLU曲面裁剪函数程序示例及样条表示遗留问题
    [图形学] Chp10 OpenGL三维观察程序示例
    [图形学] 问题: 裁剪窗口坐标对应显示窗口中像素是一对一还是一对多?
    [图形学] Chp9 三维几何变换--栈处理函数与矩阵管理函数的区别
    [图形学] 习题8.12 NLN二维线段裁剪算法实现
    [图形学] 习题8.6 线段旋转后使用Cohen-Sutherland算法裁剪
    [图形学] Chp8 使用双缓存创建帧动画
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/6933139.html
Copyright © 2011-2022 走看看