zoukankan      html  css  js  c++  java
  • POJ 2761 Feed the dogs (主席树)(K-th 值)

                                                                Feed the dogs
    Time Limit: 6000MS   Memory Limit: 65536K
    Total Submissions: 20634   Accepted: 6494

    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
    【分析】这题跟POJ2104很像,所以可以用划分树来做。这里只是为了练习主席树就用了主席树的板子,不过主席树还不是很懂。。。
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <time.h>
    #include <string>
    #include <map>
    #include <stack>
    #include <vector>
    #include <set>
    #include <queue>
    #define met(a,b) memset(a,b,sizeof a)
    #define pb push_back
    #define lson(x) ((x<<1))
    #define rson(x) ((x<<1)+1)
    using namespace std;
    typedef long long ll;
    const int N=1e5+50;
    const int M=N*N+10;
    struct seg {
        int lson,rson;
        int cnt;
    };
    seg T[N*20];
    int root[N],tot;
    vector<int>pos;
    int arr[N];
    int last_pos[N];
    
    void init() {
        pos.clear();
        met(root,0);met(last_pos,0);
        tot=0;
        T[0].cnt=T[0].lson=T[0].rson=0;
    }
    void update(int &cur,int ori,int l,int r,int pos,int flag) {
        cur=++tot;
        T[cur]=T[ori];
        T[cur].cnt+=flag;
        if(l==r)
            return ;
        int mid=(l+r)/2;
        if(pos<=mid)
            update(T[cur].lson,T[ori].lson,l,mid,pos,flag);
        else
            update(T[cur].rson,T[ori].rson,mid+1,r,pos,flag);
    }
    int query(int S,int E,int l,int r,int k) {
        if(l==r)return l;
        int mid=(l+r)/2;
        int sum=T[T[E].lson].cnt-T[T[S].lson].cnt;
        if(sum>=k)return query(T[S].lson,T[E].lson,l,mid,k);
        else query(T[S].rson,T[E].rson,mid+1,r,k-sum);
    }
    int main(void) {
        int n,m,i,l,r,k;
       scanf("%d%d",&n,&m);
            init();
            for (i=1; i<=n; ++i) {
                scanf("%d",&arr[i]);
                pos.push_back(arr[i]);
            }
            sort(pos.begin(),pos.end());
            pos.erase(unique(pos.begin(),pos.end()),pos.end());
            int temp_rt=0;
            for (i=1; i<=n; ++i) {
                arr[i]=lower_bound(pos.begin(),pos.end(),arr[i])-pos.begin()+1;
                update(root[i],root[i-1],1,n,arr[i],1);
            }
            for (i=0; i<m; ++i) {
                scanf("%d%d%d",&l,&r,&k);
                printf("%d
    ",pos[query(root[l-1],root[r],1,n,k)-1]);
            }
    
        return 0;
    }
  • 相关阅读:
    [Effective C++ --009]确定对象被使用前已先被初始化
    [Effective C++ --008]别让异常逃离析构函数
    [Effective C++ --007]为多态基类声明virtual析构函数
    [Effective C++ --006]若不能使用编译器自动生成的函数,就该明确拒绝
    [Effective C++ --005]了解C++默默编写并调用哪些函数
    [000]socket通信--server和client实现的简单例子
    [014]模板-模板实参推导
    [013]模板-非类型模板参数
    [012]泛型--lambda表达式捕获
    U3d keyCode值对应的按键
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6373570.html
Copyright © 2011-2022 走看看