首页 > 其他分享 >POJ2761 Feed the dogs(Treap)

POJ2761 Feed the dogs(Treap)

时间:2023-02-03 10:01:16浏览次数:48  
标签:Feed rt ch int Treap counts include dogs

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

给你N个数,然后要你对下面M个查询回答:(L,R,K)。回答第L个数到第R个数之间的第K小数的值是多少。其中任意给定的两个区间[Li,Ri]和[Lj,Rj]之间不存在包含关系

Treap,每次超出区间的删掉,不在区间内的加上,再找第k小值

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<set>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const int maxn=100000+5;
int ch[maxn][2],val[maxn],counts[maxn],r[maxn],size[maxn],tot,root;
int num[maxn];
struct A
{
  int l,r,k,id,ans;
}a[50005];
bool cmp1(A x,A y)
{
  if(x.l == y.l) return x.r < y.r;
  return x.l  < y.l;
}
bool cmp2(A x,A y)
{
  return x.id < y.id;
}
struct Treap
{
    void newnode(int &rt,int v)
    {
        rt=++tot;
        val[rt]=v;
        ch[rt][0]=ch[rt][1]=0;
        counts[rt]=size[rt]=1;
        r[rt]=rand();
    }
    void pushup(int rt)
    {
        size[rt]=size[ch[rt][0]]+size[ch[rt][1]]+counts[rt];
    }
    void rate(int &x,int kind)
    {
        int y=ch[x][kind^1];
        ch[x][kind^1]=ch[y][kind];
        ch[y][kind]=x;
        pushup(x);
        pushup(y);
        x=y;
    }
    void add(int &rt,int v)
    {
        if(rt==0)
        {
            newnode(rt,v);
            return ;
        }
        if(v==val[rt]) counts[rt]++;
        else
        {
            int kind=(v>val[rt]);
            add(ch[rt][kind],v);
            if(r[ch[rt][kind]]<r[rt])
                rate(rt,kind^1);
        }
        pushup(rt);
    }
    int select(int rt,int k)
    {
        if(size[ch[rt][0]]>=k) return select(ch[rt][0],k);
        if(size[ch[rt][0]]+counts[rt]>=k) return val[rt];
        return select(ch[rt][1],k-size[ch[rt][0]]-counts[rt]);
    }
    void rmove(int &rt,int v)
    {
        if(val[rt]==v)
        {
            if(counts[rt]>1)
                counts[rt]--;
            else if(!ch[rt][0]&&!ch[rt][1])
            {rt=0;return ;}
            else
            {
                int kind=r[ch[rt][0]]<r[ch[rt][1]];
                rate(rt,kind);
                rmove(rt,v);
            }
        }
        else rmove(ch[rt][v>val[rt]],v);
        pushup(rt);
    }
    void init()
    {
        ch[0][0]=ch[0][1]=0;
        size[0]=counts[0]=val[0]=0;
        tot=root=0;
        r[0]=(1LL<<31)-1;
        newnode(root,2000000001);
    }
}Treap;
int main()
{
    int n,m,lastl,lastr;
    while(~scanf("%d%d",&n,&m))
    {
    	Treap.init();
        for (int i=1; i<=n; i++)
        scanf("%d",&num[i]);
        for(int i = 1;i <=m ;i ++)
		{
          scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].k);
          a[i].id = i;
		}
		sort(a+1,a+1+m,cmp1);
		for(int i = 1;i <= m;i ++)
		{
		  if(i == 1)
		  {
		    for(int j = a[i].l; j <= a[i].r;j ++)
			Treap.add(root, num[j]);
			a[i].ans = Treap.select(root, a[i].k);
			lastl = a[i].l;
			lastr = a[i].r;
		  }
		  else
		  {
            if (a[i].l>lastr)
            {
                for (int j=lastl; j<=lastr; j++)
                Treap.rmove(root,num[j]);
                for (int j=a[i].l; j<=a[i].r; j++)
                Treap.add(root,num[j]);
            }
            else
            {
                for (int j=lastl; j<a[i].l; j++)
                Treap.rmove(root,num[j]);
                for (int j=lastr+1; j<=a[i].r; j++)
                Treap.add(root,num[j]);
            }
            a[i].ans=Treap.select(root,a[i].k);
            lastl=a[i].l;
            lastr=a[i].r;
          }
       }
        sort(a+1,a+1+m,cmp2);
        for(int i = 1;i <= m;i ++)
		printf("%d\n",a[i].ans);
	 }
     return 0;
}

 

标签:Feed,rt,ch,int,Treap,counts,include,dogs
From: https://blog.51cto.com/u_15952369/6034943

相关文章

  • BZOJ1503 郁闷的出纳员 (Treap)
    DescriptionOIER公司是一家大型专业化软件公司,有着数以万计的员工。作为一名出纳员,我的任务之一便是统计每位员工的工资。这本来是一份不错的工作,但是令人郁闷的是,我们......
  • 点赞收藏关注feed的实现
    packagecom.hmdp.service.impl;importcn.hutool.core.bean.BeanUtil;importcom.baomidou.mybatisplus.core.conditions.query.QueryWrapper;importcom.baomidou.my......
  • 学习笔记:二叉平衡树之 fhp-treap
    问题背景:你需要维护一个整数集合,可以满足一下几种操作:插入一个整数xx。删除一个整数xx(若有多个相同的数,只删除一个)。查询整数xx的排名(排名定义为比当前数小......
  • FHQ-treap 学习笔记
    FHQ-Treap学习这种平衡树不需要了解treap,据说treap和splay能干的事情他也能干。update:2023.1.12以前写的博客看起来太仓促了,修改了一下。前置芝士二叉搜索树的性......
  • fhqTreap学习笔记/做题记录
    \(\rmfhqTreap\)学习笔记&做题记录发大电部分我是\(\rmfhqTreap\)批众所周知,\(\rmfhqTreap\)可以部分(或者完全?)替代splay的区间功能,而且写起来又方便,所以去tm的s......
  • 在Chrome中使用Feedbro订阅RSS信息流
    Chrome插件英雄榜096《Feedbro》在Chrome中订阅RSS信息流RSS是一种标准的网站内容投递协议,通过解析RSS我们可以获取网站的内容更新。Feedbro是一款可以在浏览器中直接......
  • 「Note」闲话 2022.12.30(《浅谈Splay与Treap的性质及其应用》学习笔记)
    屎一样的一年还有两天就过去了呢。感觉都阳了一周了还是没有回复到正常状态,真的挺讨厌的。今天随便找了篇论文假学习了一会儿。由于懒,所以大量内容属摘抄。平衡树的fin......
  • Feederbot - a bot using DNS as carrier for its C&C(DNS信道马)
    Feederbot-abotusingDNSascarrierforitsC&C(DNS信道马)DNSascarrierforbotnetC&Cseemstobegettingpopular.Concerningitsus......
  • KingbaseES V8R6在解决复制冲突中hot_standby_feedback参数的重要性
    背景如果我们看到这样的类似报错:那说明可能遇到了复制冲突。复制冲突的理解:当备库正在应用主库传输过来的wal日志与备库正在进行的查询产生冲突就会有此报错。比如说备库......
  • Feed Adapter(适配器)
    Spring集成通过馈送适配器为联合提供支持。执行工作以《罗马框架》为基础。您需要将此依赖项包含在项目中:<dependency><groupId>org.springframework.integration</gr......