zoukankan      html  css  js  c++  java
  • 【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
  • 相关阅读:
    P2330 05四川 繁忙的都市
    Winform中实现更改DevExpress的RadioGroup的选项时更改其他控件(TextEdit、ColorPickEdit)的值
    Winform中实现读取xml配置文件并动态配置DevExpress的RadioGroup的选项
    Winform中对DevExpress的RadioGroup进行数据源绑定,即通过代码添加选项
    Winform中对自定义xml配置文件进行Xml节点的添加与删除
    Winform中自定义xml配置文件后对节点进行读取与写入
    C#中使用StreamReader实现文本文件的读取与写入
    Python编程书籍高清PDF免费下载
    Winform中自定义xml配置文件,并配置获取文件路径
    Winform中实现ZedGraph新增自定义Y轴上下限、颜色、标题功能
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/3923809.html
Copyright © 2011-2022 走看看