zoukankan      html  css  js  c++  java
  • poj 2761 主席树的应用(查询区间第k小值)

    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 ", b[query(rt[s - 1], rt[t], 1, sz, k)]);
            }
        return 0;
    }



  • 相关阅读:
    20159302 《网络攻击与防范》第三周学习总结
    Json序列化空时间字段出异常
    IIS 原理
    谴责盛大Bambook 的ADB.EXE流氓进程
    Fix: ADB server didn’t ACK
    在线UI设计
    解决装Windows蓝屏问题。
    如何成为一个C++高级程序员
    Python编程规范及性能优化
    iDempiere 使用指南 插件安装过程
  • 原文地址:https://www.cnblogs.com/linruier/p/9485187.html
Copyright © 2011-2022 走看看