zoukankan      html  css  js  c++  java
  • 划分树---Feed the dogs

    POJ  2761

    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

    题意:给了一个数列,求某个区间的第k大数。

    思路:使用划分树算法,方便多次查询指定区间的第k大数。

    代码如下:
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int maxn=100005;
    int sor[maxn];
    
    struct node
    {
        int num[maxn];
        int cnt[maxn];
    }tree[20];
    
    void buildtree(int l, int r, int d)
    {
        if (l == r)
        {
            return;
        }
        int mid = (l+r)>>1;
        int oplift = l, opright = mid+1;   ///对左右子树的操作位置的初始化
        int same_as_mid = 0;
        ///用来计算在mid左边有多少个和sor[mid]相同的数(包括mid),这些都要放到左子树
        for (int i = mid; i > 0; i--)
        {
            if (sor[i] == sor[mid])
                same_as_mid++;
            else
                break;
        }
        int cnt_lift = 0;
        for (int i = l; i <= r; i++)
        {
            if (tree[d].num[i] < sor[mid])
            {
                tree[d+1].num[oplift++] = tree[d].num[i];
                cnt_lift++;
                tree[d].cnt[i] = cnt_lift;
            }
            else if(tree[d].num[i] == sor[mid] && same_as_mid)
            {
                tree[d+1].num[oplift++] = tree[d].num[i];
                cnt_lift++;
                tree[d].cnt[i] = cnt_lift;
                same_as_mid--;
            }
            else
            {
                tree[d].cnt[i] = cnt_lift;
                tree[d+1].num[opright++] = tree[d].num[i];
            }
        }
        buildtree(l, mid, d+1);
        buildtree(mid+1, r, d+1);
    }
    
    int query(int l, int r, int d, int ql, int qr, int k)
    {
        if (l == r)
            return tree[d].num[l];
        int mid = (l+r)>>1;
        int sum_in_lift, lift;
        if (ql == l)
        {
            sum_in_lift = tree[d].cnt[qr];
            lift = 0;
        }
        else
        {
            sum_in_lift = tree[d].cnt[qr] - tree[d].cnt[ql-1];
            /// 区间进入左子树的总和
            lift = tree[d].cnt[ql-1];
        }
        if (sum_in_lift >= k)
        ///证明要找的点在左子树
        {
            int new_ql = l+lift;
            int new_qr = l+lift+sum_in_lift-1;
            return query(l, mid, d+1, new_ql, new_qr, k);
            ///这里有必要解释一下,我们要确定下一步询问的位置,如果在ql的左边有i个进入左子树,
            ///那么ql到qr中第一个进入左子树的必定在l+i的位置
        }
        else
        {
            int a = ql - l - lift;
            int b = qr - ql + 1 - sum_in_lift;
            int new_ql = mid + a + 1;
            int new_qr = mid + a + b;
            return query(mid+1, r, d+1, new_ql, new_qr, k - sum_in_lift);
        }
    }
    
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&tree[0].num[i]);
                sor[i]=tree[0].num[i];
            }
            sort(sor+1,sor+n+1);
            buildtree(1,n,0);
            while(m--)
            {
                int l,r,k;
                scanf("%d%d%d",&l,&r,&k);
                int ans=query(1,n,0,l,r,k);
                printf("%d
    ",ans);
            }
        }
    }
  • 相关阅读:
    关于音视频同步
    redis JedisConnectionException: Could not get a resource from the pool
    ping指定IP的指定端口号
    如何查看端口号是否被占用
    mongodb增删改查基础语法
    mongodb重置密码
    本地MongoDB服务开启与连接本地以及远程服务器MongoDB服务
    MongoDB服务的安装与删除
    可视化
    Elasticsearch下载安装
  • 原文地址:https://www.cnblogs.com/chen9510/p/5369580.html
Copyright © 2011-2022 走看看