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. ThreadPoolExecutor的重要参数

    一.ThreadPoolExecutor的重要参数 1.corePoolSize:核心线程数         * 核心线程会一直存活,及时没有任务需要执行         * 当线程数小于核心线程数时 ...

  2. Java中this与super的区别

    this与super关键字在java中构造函数中的应用: ** super()函数 ** super()函数在子类构造函数中调用父类的构造函数时使用,而且必须要在构造函数的第一行,例如: class ...

  3. Magento2入门之修改logo

    本文用于学习记录用 1.主题创建是在路径 /app/design/frontend/公司名/主题名称/ 我自己创建的路径为 app/design/frontend/Bman/castle,以下操作都在 ...

  4. Linux服务器Java进程突然消失排查办法

    出处:JAVA进程突然消失的原因? 问题描述 在实际生产环境下,如果我们遇见Java进程突然消失,该如何去排查问题? 思路 可能有几种原因: ①.Java应用程序的问题:发生OOM导致进程Crash ...

  5. selenium与页面交互

    selenium提供了许多API方法与页面进行交互,如点击.键盘输入.打开关闭网页.输入文字等. 一.webdriver对浏览器提供了很多属性来对浏览器进行操作,常用的如图: get(url).qui ...

  6. Codeforces 1228C. Primes and Multiplication

    传送门 当然是考虑 $n$ 的每个质数 $p$ 对答案的贡献 考虑 $p^k$ 在 $[1,m]$ 中出现了几次,显然是 $\left \lfloor \frac{m}{p^k} \right \rf ...

  7. Scala学习二——控制结构和函数

    一.if表达式有值 val s=if(x>0) 1 else -1,相当于Java中x>0?1:-1(不过不拿呢个在?:中插入语句),而且Scala中可以用混合类型(如if (x>0 ...

  8. render:h => h(App) ----render函数

    转载其他博客1 new Vue({ 2 3 router, 4 store, 5 //components: { App } vue1.0的写法 6 render: h => h(App) vu ...

  9. PropertySource顺序

    Spring Boot使用一个非常特殊的PropertySource顺序,该顺序旨在允许合理地覆盖值.按以下顺序考虑属性: $HOME/.config/spring-boot当devtools处于活动 ...

  10. busybox介绍

    BusyBox 是一个集成了一百多个最常用linux命令和工具的软件.BusyBox 将许多具有共性的小版本的UNIX工具结合到一个单一的可执行文件.这样的集合可以替代大部分常用工具比如的GNU fi ...