poj2761——Feed the dogs(划分树或SBT)

本文探讨了如何使用划分树(SBT)解决区间内第K大的问题,详细解释了排序区间、插入与删除操作的过程,并通过具体代码实现来解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Feed the dogs
Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 13221Accepted: 3959
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大,是一道很裸的划分树的题。。。

         因为想练哈刚学的SBT,再加上划分树不熟。。。

         所以想到了此方法。。。将区间排好序后依次插入SBT,查询后再删除。。。

         将区间排序是因为区间有重叠部分,可以降低时间复杂度。。。不排序会TLE!!!

代码:

#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
int val[100010];
struct SBT
{
    int key,size,left,right;
    void init()
    {
        left=right=0;
        size=1;
    }
}tree[100010];
int tot=0,root;
struct node
{
    int start,end,id,th,ans;
}que[100010];

void left(int &p)
{
	int k=tree[p].right;
	tree[p].right=tree[k].left;
	tree[k].left=p;
    tree[k].size=tree[p].size;
    tree[p].size=tree[tree[p].left].size+tree[tree[p].right].size+1;
    p=k;
}

void right(int &p)
{
	int k=tree[p].left;
	tree[p].left=tree[k].right;
	tree[k].right=p;
	tree[k].size=tree[p].size;
	tree[p].size=tree[tree[p].left].size+tree[tree[p].right].size+1;
	p=k;
}

void maintain(int &p,bool flag)
{
	if(flag)
	{
		if(tree[tree[tree[p].right].right].size>tree[tree[p].left].size)left(p);
		else if(tree[tree[tree[p].right].left].size>tree[tree[p].left].size)
		{
			right(tree[p].right);
			left(p);
		}else return ;
	}
	else
	{
		if(tree[tree[tree[p].left].left].size>tree[tree[p].right].size)right(p);
		else if(tree[tree[tree[p].left].right].size>tree[tree[p].right].size)
		{
			left(tree[p].left);
			right(p);
		}else return ;
	}
	maintain(tree[p].left,false);
	maintain(tree[p].right,true);
	maintain(p,false);
	maintain(p,true);
}

void insert(int &p,int k)
{
	if(p==0)
	{
		p=++tot;
		tree[p].init();
		tree[p].key=k;
	}
	else
	{
		tree[p].size++;
		if(k<tree[p].key)
			insert(tree[p].left,k);
		else insert(tree[p].right,k);
		maintain(p,k>=tree[p].key);
	}
}

int remove(int &p,int k)
{
	if(!p)return 0;
	tree[p].size--;
	if(k==tree[p].key||k<tree[p].key&&!tree[p].left||k>tree[p].key&&!tree[p].right)
	{
        if(tree[p].left &&  tree[p].right)
        {
            int t=remove(tree[p].left,k+1);
            tree[p].key=tree[t].key;
            return t;
        }
        else
        {
            int t=p;
            p=tree[p].left+tree[p].right;
            return t;
        }
    }else
     return remove(k<tree[p].key?tree[p].left:tree[p].right,k);
}

int get(int &p,int k)
{
	int t=tree[tree[p].left].size+1;
	if(t==k)return tree[p].key;
	if(t<k)return get(tree[p].right,k-t);
	else return get(tree[p].left,k);
}

bool cmp(node a,node b)
{
    if(a.start==b.start)
    return a.end<b.end;
    return a.start<b.start;
}

bool cpp(node a,node b)
{
    return a.id<b.id;
}

void read()
{
    int n,m;      tot=root=0;
    freopen("poj2761.in","r",stdin);
    freopen("poj2761.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&val[i]);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&que[i].start,&que[i].end,&que[i].th);
        que[i].id=i;
    }
    sort(que+1,que+m+1,cmp);
    for(int i=que[1].start;i<=que[1].end;i++)
    insert(root,val[i]);
    que[1].ans=get(root,que[1].th);
    for(int i=2;i<=m;i++)
    {
       /* if(que[i].start==que[i-1].start)
        {
            for(int j=que[i-1].end;j<=que[i].end;j++)insert(root,val[j]);
            que[i].ans=get(root,que[i].th);
        }
        else*/
        if(que[i].start>=que[i-1].end)
        {
            for(int j=que[i-1].start;j<=que[i-1].end;j++)remove(root,val[j]);
            for(int j=que[i].start;j<=que[i].end;j++)insert(root,val[j]);
        }
        else
        {
            if(que[i].end<=que[i-1].end)
            {
                if(que[i].start!=que[i-1].start)
                    for(int j=que[i-1].start;j<que[i].start;j++)remove(root,val[j]);
                if(que[i].end!=que[i-1].end)
                    for(int j=que[i].end+1;j<=que[i-1].end;j++)remove(root,val[j]);
            }
            else
            if(que[i].end>que[i-1].end)
            {
                if(que[i].start!=que[i-1].start)
                    for(int j=que[i-1].start;j<que[i].start;j++)remove(root,val[j]);
                for(int j=que[i-1].end+1;j<=que[i].end;j++)insert(root,val[j]);
            }
        }
        que[i].ans=get(root,que[i].th);
    }
    //for(int i=1;i<=m;i++)printf("%d %d\n",que[i].start,que[i].end);
    sort(que+1,que+m+1,cpp);
    for(int i=1;i<=m;i++)printf("%d\n",que[i].ans);
}

int main()
{
	read();
	//work();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值