Feed the dogs
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 22084   Accepted: 7033

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

参考源代码:

#include <iostream>
#include<stdio.h>
#include<cmath>
#include<string.h>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
//主席树

const int MAXN = 100005;

struct node {
    int ls, rs, sum;
} ns[MAXN * 20];

int ct;
int rt[MAXN * 20];

void cpy(int& now, int old) {
    now = ++ct;
    ns[now] = ns[old];
}

void pushUp(int& now) {
    ns[now].sum = ns[ns[now].ls].sum + ns[ns[now].rs].sum;
}

void build(int& now, int l, int r) {
    now = ++ct;
    ns[now].sum = 0;
    if (l == r) return;
    int m = (l + r) >> 1;
    build(ns[now].ls, l, m);
    build(ns[now].rs, m + 1, r);
}

void update(int& now, int old, int l, int r, int x) {
    cpy(now, old);
    if (l == r) {
        ns[now].sum++;
        return;
    }
    int m = (l + r) >> 1;
    if (x <= m) update(ns[now].ls, ns[old].ls, l, m, x);
    else update(ns[now].rs, ns[old].rs, m + 1, r, x);
    pushUp(now);
}

int query(int s, int t, int l, int r, int k) {
    if (l == r) return l;
    int m = (l + r) >> 1;
    int cnt = ns[ns[t].ls].sum - ns[ns[s].ls].sum;
    //cout << s << " " << t << " " << cnt << endl;
    if (k <= cnt) return query(ns[s].ls, ns[t].ls, l, m, k);
    return query(ns[s].rs, ns[t].rs, m + 1, r, k - cnt);
}

void init(int n) {
    ct = 0;
    build(rt[0], 1, n);
}

int value[100005];
int b[100005];

using namespace std;
int main()
{
    freopen("in.txt", "r", stdin);
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) {
            scanf("%d", &value[i]);
            b[i] = value[i];
        }
        sort(b + 1, b + n + 1);
        int sz = unique(b + 1, b + 1 + n) - b - 1;
        init(sz);
        for (int i = 1; i <= n; i++) {
            value[i] = lower_bound(b + 1, b + 1 + sz, value[i]) - b;
            update(rt[i], rt[i - 1], 1, sz, value[i]);
        }
        while (m--) {
            int s, t, k;
            scanf("%d%d%d", &s, &t, &k);
            printf("%d\n", b[query(rt[s - 1], rt[t], 1, sz, k)]);
        }
    return 0;
}

poj 2761 主席树的应用(查询区间第k小值)的更多相关文章

  1. 【可持久化线段树】POJ2104 查询区间第k小值

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 61284   Accepted: 21504 Ca ...

  2. 主席树总结(经典区间第k小问题)(主席树,线段树)

    接着上一篇总结--可持久化线段树来整理吧.点击进入 这两种数据结构确实有异曲同工之妙.结构是很相似的,但维护的主要内容并不相同,主席树的离散化.前缀和等思想也要更难理解一些. 闲话 话说刚学习主席树的 ...

  3. 主席树初步 HDU2665的区间第k小

    首先看一下这个人的blog吧,讲的精炼http://blog.sina.com.cn/s/blog_4a0c4e5d0101c8fr.html 然后再推荐一下这个人的blog:http://www.c ...

  4. A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)

    题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...

  5. 洛谷3834 hdu2665主席树模板,动态查询区间第k小

    题目链接:https://www.luogu.com.cn/problem/P3834 对于区间查询第k小的问题,在区间数量达到5e5的时候是难以用朴素数据结构实现的,这时候主席树就应运而生了,主席树 ...

  6. 「BZOJ3065」带插入区间第K小值 替罪羊树×线段树

    题目描述 从前有\(n\)只跳蚤排成一行做早操,每只跳蚤都有自己的一个弹跳力\(a_i\).跳蚤国王看着这些跳蚤国欣欣向荣的情景,感到非常高兴.这时跳蚤国王决定理性愉悦一下,查询区间\(k\)小值.他 ...

  7. BZOJ 3065 带插入区间第K小值

    题目描述 Description 从前有n只跳蚤排成一行做早操,每只跳蚤都有自己的一个弹跳力a[i].跳蚤国王看着这些跳蚤国欣欣向荣的情景,感到非常高兴.这时跳蚤国王决定理性愉悦一下,查询区间k小值. ...

  8. ZOJ -2112 Dynamic Rankings 主席树 待修改的区间第K大

    Dynamic Rankings 带修改的区间第K大其实就是先和静态区间第K大的操作一样.先建立一颗主席树, 然后再在树状数组的每一个节点开线段树(其实也是主席树,共用节点), 每次修改的时候都按照树 ...

  9. 线段树专题2-(加强版线段树-可持续化线段树)主席树 orz! ------用于解决区间第k大的问题----xdoj-1216

    poj-2104(区间第K大问题) #include <iostream> #include <algorithm> #include <cstdio> #incl ...

随机推荐

  1. Redis数据库连接

    1.建立maven项目pox.xml导入依赖包 <dependency> <groupId>io.lettuce</groupId> <artifactId& ...

  2. Entity Framework常用方法及案例

    ⒈Skip(int count) 说明:跳过集合的前n个元素:延迟.即我们跳过给定的数目返回后面的结果集. ⒉Take(int count) 说明:获取集合的前n个元素:延迟.即只返回限定数量的结果集 ...

  3. <<C++ Primer>> 第 7 章 类

    术语表 第 7 章 类 抽象数据类型(abstract data type): 封装(隐藏)了实现细节的数据结构.    访问说明符(access specifier): 包括关键字 public 和 ...

  4. HDU 4292 Food (建图思维 + 最大流)

    (点击此处查看原题) 题目分析 题意:某个餐馆出售f种食物,d种饮料,其中,第i种食物有fi份,第i种饮料有di份:此时有n个人来餐馆吃饭,这n个人必须有一份食物和一份饮料才会留下来吃饭,否则,他将离 ...

  5. const和static const的区别(未整理)

    对于C/C++语言来讲,const就是只读的意思,只在声明中使用;static一般有2个作用,规定作用域和存储方式.对于局部变量,static规定其为静态存储方式,每次调用的初始值为上一次调用的值,调 ...

  6. command----常用命令更新ing

    common commands 1:split---拆分文件 [root@localhost split]# split -b 1M split.tar.gz split_ #按1M拆分文件 [roo ...

  7. 关于NGINX在wnidows下面和linux下面的多站点的反向代理的配置

    原创文章,转载注明出处 nginx作为一款优秀的反向代理软件,以其好用,易于搭建负载均衡的网站集群而著称,这里分别记录一下工作中用到nginx作为负载以及多站点发布的时候一些配置和注意事项 一  ng ...

  8. JS基础_标识符

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. O018、理解 Keystone 核心概念

    参考https://www.cnblogs.com/CloudMan6/p/5365474.html   作为OpenStack的基础支持服务,Keystone做了下面几件事情:       1.管理 ...

  10. SQLAlchemy技术手册

    一.ORM 框架简介 对象-关系映射(Object/Relation Mapping,简称ORM),是随着面向对象的软件开发方法发展而产生的.面向对象的开发方法是当今企业级应用开发环境中的主流开发方法 ...