POJ 2761 Feed the dogs (主席树)(K-th 值)

本文介绍了一种使用主席树解决特殊喂狗问题的方法。该问题要求在不重复覆盖喂食区间的情况下找到每次喂食的狗狗。通过构建主席树并进行查询操作,实现了对每个喂食请求的有效响应。

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

                                                            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\n",pos[query(root[l-1],root[r],1,n,k)-1]);
        }

    return 0;
}

 

转载于:https://www.cnblogs.com/jianrenfang/p/6373570.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值