码迷,mamicode.com
首页 > 其他好文 > 详细

kyeremal-poj2104-K-th Number-主席树

时间:2015-05-20 18:28:27      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:oiacm   poj   

poj2104-K-th Number
K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 31790   Accepted: 9838
Case Time Limit: 2000MS

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

主席树
#include <iostream>
#include <cstdio>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

#define rep(i, l, r) for (int i = l; i <= r; i++)
#define REP(i, l, r) for (int i = l; i >= r; i--)
#define X first
#define Y second
#define MAXN 100010

int n, T_T, num[MAXN], head, tail, root[MAXN], m = 0, hash[MAXN], maxvalue;
struct Tree{int l, r, lc, rc, s;} a[MAXN*25];
pair<int, int> f[MAXN];

bool cmp1(pair<int, int> a, pair<int, int> b) {return a.X < b.X;}
bool cmp2(pair<int, int> a, pair<int, int> b) {return a.Y < b.Y;}

inline void discretization() {
    sort(f+1, f+1+n, cmp1);
    int pre = f[1].X;
    hash[1] = f[1].X;
    f[1].X = 1;
    rep(i, 2, n)
	if (f[i].X != pre) pre = f[i].X, hash[f[i-1].X+1] = f[i].X, f[i].X = f[i-1].X + 1;
	else f[i].X = f[i-1].X;
    maxvalue = f[n].X;
    sort(f+1, f+1+n, cmp2);
    rep(i, 1, n) num[i] = f[i].X;
}

inline void build_tree(int i, int L, int R, int x) {
    int mid = (L + R) >> 1;
    a[i].l = L, a[i].r = R;
    if (L == R) {a[i].s = (L == x ? 1 : 0); return;}
    build_tree(a[i].lc = ++m, L, mid, x);
    build_tree(a[i].rc = ++m, mid+1, R, x);
    a[i].s = a[a[i].lc].s + a[a[i].rc].s;
}

inline void add_tree(int i, int j, int L, int R, int x) {
    int mid = (L + R) >> 1;
    a[i].l = L, a[i].r = R;
    if (L == R) {a[i].s = a[j].s + 1; return;}
    if (x <= mid) a[i].rc = a[j].rc, add_tree(a[i].lc = ++m, a[j].lc, L, mid, x);
    else a[i].lc = a[j].lc, add_tree(a[i].rc = ++m, a[j].rc, mid+1, R, x);
    a[i].s = a[a[i].lc].s + a[a[i].rc].s;
}

inline int query(int i, int j, int ql, int qr, int k) {
    int L = a[i].l, R = a[i].r;
    int mid = (L + R) >> 1;
    if (L == R) return L;
    if ((a[a[i].lc].s - a[a[j].lc].s) >= k) query(a[i].lc, a[j].lc, ql, qr, k);
    else query(a[i].rc, a[j].rc, ql, qr, k-(a[a[i].lc].s - a[a[j].lc].s));
}

inline void write(int i) {
    printf("%d : %d %d %d %d %d\n", i, a[i].l, a[i].r, a[i].lc, a[i].rc, a[i].s);
    if (a[i].lc) write(a[i].lc);
    if (a[i].rc) write(a[i].rc);
}

int main() {
    cin >> n >> T_T;
    rep(i, 1, n) scanf("%d", &f[i].X), f[i].Y = i;
    discretization();
    head = 1, tail = maxvalue;
    build_tree(m = root[1] = 1, head, tail, num[1]);
    rep(i, 2, n) add_tree(root[i] = ++m, root[i-1], head, tail, num[i]);
    while (T_T--) {
	int tl, tr, tk;
	scanf("%d%d%d", &tl, &tr, &tk);
	cout << hash[query(root[tr], root[tl-1], head, tail, tk)] << endl;
    }

    return 0;
}




kyeremal-poj2104-K-th Number-主席树

标签:oiacm   poj   

原文地址:http://blog.csdn.net/kyeremal/article/details/45873291

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!