zoukankan      html  css  js  c++  java
  • POJ 2761 Feed the dogs

    Feed the dogs
    Time Limit: 6000MS   Memory Limit: 65536K
    Total Submissions: 11808   Accepted: 3409

    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

    //早上起来说吧昨天写的的SBT练习一下、然后、一早上就渡过去了、
    //中间出了许多小问题、、、悲惨,万恶的复制、粘贴呀
     

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #define N 100013
    using namespace std;
    struct node
    {
        int l,r,s,key;
        void init()
        {
            l=r=key=0;
            s=1;
        }
    };
    node sbt[N];
    int nu;
    void right_rotate(int &t)
    {
        int k=sbt[t].l;
        sbt[t].l=sbt[k].r;
        sbt[k].r=t;
        sbt[k].s=sbt[t].s;
        sbt[t].s=sbt[sbt[t].l].s+sbt[sbt[t].r].s+1;
        t=k;
    }
    void left_rotate(int &t)
    {
        int k=sbt[t].r;
        sbt[t].r=sbt[k].l;
        sbt[k].l=t;     //开始这里错了
        sbt[k].s=sbt[t].s;
        sbt[t].s=sbt[sbt[t].l].s+sbt[sbt[t].r].s+1;
        t=k;
    }
    void Maintain(int &t,bool flag)
    {
        if(flag==false)
        {
            if(sbt[sbt[sbt[t].l].l].s>sbt[sbt[t].r].s)
                 right_rotate(t);
            else if(sbt[sbt[sbt[t].l].r].s>sbt[sbt[t].r].s)
                  left_rotate(sbt[t].l),right_rotate(t);
                  else return ;
        }
        else
        {
            if(sbt[sbt[sbt[t].r].r].s>sbt[sbt[t].l].s)
                  left_rotate(t);
            else if(sbt[sbt[sbt[t].r].l].s>sbt[sbt[t].l].s)
                  right_rotate(sbt[t].r),left_rotate(t);//该死的复制粘贴、这里又出了问题
                  else return ;
        }
        Maintain(sbt[t].l,false);
        Maintain(sbt[t].r,true);
        Maintain(t,false);
        Maintain(t,true);
    }
    void insert(int &t,int &v)
    {
        if(t==0)
        {
            t=++nu;
            sbt[t].init();
            sbt[t].key=v;
            return ;
        }
        sbt[t].s++;
        if(v<=sbt[t].key)
         insert(sbt[t].l,v);
        else
         insert(sbt[t].r,v);
        Maintain(t,v>sbt[t].key);
        return ;
    }
    int del(int &t,int v)
    {
        if(t==0) return 0;//开始没写这个、出现负数了都、、
        sbt[t].s--;      
        if(v==sbt[t].key||(v>sbt[t].key&&!sbt[t].r)||(v<sbt[t].key&&!sbt[t].l))
        {

            if(!sbt[t].l||!sbt[t].r)//把问题都搞定后、这里少了个 '!',然后华丽丽的WA了
            {     int k=sbt[t].key;
                  t=sbt[t].r+sbt[t].l;
                  return k;
            }
            else
            {
                return sbt[t].key=del(sbt[t].l,v+1);
            }
        }
        if(v>sbt[t].key)
         return del(sbt[t].r,v);
        return del(sbt[t].l,v);
    }
    int getkth(int &t,int k)
    {
        if(k==sbt[sbt[t].l].s+1)
         return sbt[t].key;
        if(k<sbt[sbt[t].l].s+1)
          return getkth(sbt[t].l,k);
        return getkth(sbt[t].r,k-sbt[sbt[t].l].s-1);
    }
    struct inteval
    {
        int i,j,k;
        int index,record;
    };
    inteval sor_t[50003];
    bool cmp1(const inteval&a,const inteval&b)
    {
        return a.i<b.i;
    }
    bool cmp2(const inteval&a,const inteval&b)
    {
        return  a.index<b.index;
    }
    int re[N];
    int main()
    {
        nu=0;
        int root=0;
        int n,m,i,j;
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
         scanf("%d",&re[i]);
        for(i=0;i<m;i++)
         {
             scanf("%d%d%d",&sor_t[i].i,&sor_t[i].j,&sor_t[i].k);
             sor_t[i].index=i;
         }

        sort(sor_t,sor_t+m,cmp1);

        for(i=sor_t[0].i;i<=sor_t[0].j;i++)
            insert(root,re[i]);
         sor_t[0].record=getkth(root,sor_t[0].k);
         for(i=1;i<m;i++)
         {
            if(sor_t[i].i>sor_t[i-1].j)
            {
                for(j=sor_t[i-1].i;j<=sor_t[i-1].j;j++)
                   del(root,re[j]);
                for(j=sor_t[i].i;j<=sor_t[i].j;j++)
                   insert(root,re[j]);
                sor_t[i].record=getkth(root,sor_t[i].k);
            }
            else
            {
                for(j=sor_t[i-1].i;j<sor_t[i].i;j++)//万恶的复制粘贴呀
                     del(root,re[j]);
                for(j=sor_t[i-1].j+1;j<=sor_t[i].j;j++)
                     insert(root,re[j]);
                sor_t[i].record=getkth(root,sor_t[i].k);
            }
         }
         sort(sor_t,sor_t+m,cmp2);
         for(i=0;i<m;i++)
           printf("%d\n",sor_t[i].record);
        return 0;
    }

  • 相关阅读:
    VS 2015 GIT操作使用说明
    态度以及业余付出决定程序生涯
    Magicodes.WeiChat——使用OAuth 2.0获取微信用户信息
    Magicodes.WeiChat——ASP.NET Scaffolding生成增删改查、分页、搜索、删除确认、批量操作、批量删除等业务代码
    Magicodes.WeiChat——利用纷纭打造云日志频道
    Magicodes.WeiChat——自定义knockoutjs template、component实现微信自定义菜单
    产品管理之敏捷之路(一)——携手同行,走自己的敏捷之路
    Magicodes.NET框架之路——V0.0.0.5 Beta版发布
    Spring mvc中@RequestMapping 6个基本用法小结
    spring.net aop 讲解
  • 原文地址:https://www.cnblogs.com/372465774y/p/2595079.html
Copyright © 2011-2022 走看看