博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2761 主席树的应用(查询区间第k小值)
阅读量:5240 次
发布时间:2019-06-14

本文共 3304 字,大约阅读时间需要 11 分钟。

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 21 5 2 6 3 7 41 5 32 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;
}

转载于:https://www.cnblogs.com/linruier/p/9485187.html

你可能感兴趣的文章
转:基于InfluxDB&Grafana的JMeter实时性能测试数据的监控和展示
查看>>
结对编程博客
查看>>
Kendo MVVM 数据绑定(四) Disabled/Enabled
查看>>
python学习笔记3-列表
查看>>
程序的静态链接,动态链接和装载 (补充)
查看>>
关于本博客说明
查看>>
C++11 生产者消费者
查看>>
IO multiplexing 与 非阻塞网络编程
查看>>
hdu4105  Electric wave
查看>>
基于内容的图片检索CBIR(Content Based Image Retrieval)简介
查看>>
线程androidAndroid ConditionVariable的用法
查看>>
程序电脑VS2008 应用程序配置不正确,未能启动该应用程序。重新安装程序可以修复此问题。解决方法...
查看>>
设置类UIColor使用colorWithRed定义颜色
查看>>
文件语音识别Google语音识别学习札记 - Windows PC机上测试语音识别Strut2教程-java教程...
查看>>
μC/OS-III---I笔记13---中断管理
查看>>
:after,:before,content
查看>>
FTTB FTTC FTTH FTTO FSA
查看>>
OpenAI Gym
查看>>
stap-prep 需要安装那些内核符号
查看>>
网易杭研后台技术中心的博客 -MYSQL :OOM
查看>>