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

    return 0;
}
时间: 2024-10-25 21:39:46

POJ 2761 Feed the dogs (主席树)(K-th 值)的相关文章

POJ 2761 Feed the dogs

静态区间第K大,主席树.... Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 15491   Accepted: 4780 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 Wi

Splay树(区间第k小)——POJ 2761 Feed the dogs

对应POJ题目:点击打开链接 Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 16655   Accepted: 5203 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 Win

POJ 2761 Feed the dogs 基础Treap

同样是插入和寻找第k大,这里因为区间不存在包含的情况,所以可以现将区间排序然后直接搞就行了.如果存在包含的情况那就只能上主席树或者是莫队算法来搞了. #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cstdlib> using namespace std; struct Node { Node *ch[2]; int r

POJ 2761 Feed the dogs(树状数组求区间第K大)

题目链接: 戳我 题目大意:Jiajia要为宠物狗,宠物狗按成一排站好(1 < i <= n),第 i 只狗的喜欢程度是 a[i], 之后他会先喂某个区间内第k个 即 n 个数, m个询问,接着是 n个数 接下来 m 行,每行是 l r k即 l 到 r 这个区间第 k 小的数,每个询问输出一个答案,即 a[i] 求区间第k大有很多算法, 详见此博客 [数据结构练习] 求区间第K大数的几种方法 我用的树状数组解法,来自 树状数组从前往后求和,用来解第k大(或小)的数 poj 2985 The

【POJ】3264 Balanced Lineup ——线段树 区间最值

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

【POJ 2104】 K-th Number 主席树模板题

达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没有评测,但我立下flag这个代码一定能A.我的同学在自习课上考语文,然而机房党都跑到机房来避难了\(^o^)/~ #include<cstdio> #include<cstring> #include<algorithm> #define for1(i,a,n) for(i

POJ 2104 K-th Number(主席树)

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 57427   Accepted: 19856 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse

[HDU 2665&amp;POJ 2104]K-th Number(主席树)

Description Give you a sequence and ask you the kth big number of a inteval. Solution 主席树模板题 敲完辣,心情瞬间变好,我要下楼看运动会 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #define MAXN 100005

poj 2104 K-th Number (主席树)

/* 求l,r这个死序列中第k小的数 */ #include <stdio.h> #include <algorithm> # include <string.h> using namespace std; # define lson l,m # define rson m+1,r # define N 100005 int a[N],Hash[N]; int T[N];///树祖宗节点的编号 int sum[N<<5];//数目 int L[N<&l