• 【POJ】2104 K-th Number(区间k大+主席树)


    http://poj.org/problem?id=2104

    裸题不说。主席树水过。

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define dbg(x) cout << #x << "=" << x << endl
    #define read(x) x=getint()
    #define for1(i, a, n) for(int i=a; i<=(n); ++i)
    #define MID (l+r)>>1
    inline int getint() { char c; int ret=0, k=1; for(c=getchar(); c<'0' || c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0' && c<='9'; c=getchar()) ret=ret*10+c-'0'; return ret*k; }
    
    const int N=100010;
    int id[N], a[N], b[N], n, m, root[N], cnt, key;
    struct node { int l, r, s; } T[N*50];
    void update(const int &l, const int &r, int &pos) {
    	T[++cnt]=T[pos]; pos=cnt; ++T[pos].s;
    	if(l==r) return;
    	int m=MID;
    	if(key<=m) update(l, m, T[pos].l); else update(m+1, r, T[pos].r);
    }
    int query(const int &l, const int &r, const int &x, const int &y, const int &k) {
    	if(l==r) return l;
    	int m=MID, s=T[T[y].l].s-T[T[x].l].s;
    	if(k<=s) return query(l, m, T[x].l, T[y].l, k); else return query(m+1, r, T[x].r, T[y].r, k-s);
    }
    bool cmp(const int &x, const int &y) { return a[x]<a[y]; }
    int main() {
    	read(n); read(m);
    	for1(i, 1, n) read(a[i]), id[i]=i;
    	sort(id+1, id+1+n, cmp);
    	for1(i, 1, n) b[id[i]]=i;
    	for1(i, 1, n) {
    		root[i]=root[i-1]; key=b[i];
    		update(1, n, root[i]);
    	}
    	int x, y;
    	while(m--) {
    		read(x); read(y); read(key);
    		printf("%d
    ", a[id[query(1, n, root[x-1], root[y], key)]]);
    	}
    	return 0;
    }
    

    Description

    You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
    That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
    For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.

    Input

    The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000).
    The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
    The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

    Output

    For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

    Sample Input

    7 3
    1 5 2 6 3 7 4
    2 5 3
    4 4 1
    1 7 3

    Sample Output

    5
    6
    3

    Hint

    This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

    Source

    Northeastern Europe 2004, Northern Subregion
  • 相关阅读:
    Golang 数组
    Golang 定时任务库使用
    Golang 选项设计模式
    使用阿里云构建海外docker镜像
    大数相加算法实现
    递归思想和迭代思想
    阶乘函数(factorial)——结果在整型范围内的阶乘计算
    最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)
    日结(2018年12月23日)
    日结(2018年12月22日)
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/3923809.html
Copyright © 2020-2023  润新知