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

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;
}

时间: 2024-11-08 17:20:41

kyeremal-poj2104-K-th Number-主席树的相关文章

【POJ2104】K-th Number——主席树

早上刷NOIP的题刷到有点烦就想学点新东西,然后.....一个早上就这样过去了QAQ.虽然主席树不是NOIP考点,但是...或许我能活到省选呢?(美好的幻想) 题目链接 题目的大意就是给定一个长度为n的区间,给出m个询问,每次询问一个区间[l,r]中第k小的树. 主席树(一种可持久化线段树)的入门题. 推荐一发学习资料:戳这里 感觉人家讲得很仔细了我也没什么讲的必要了...... 总算是学了一种可持久化树了,好像也没想象中那么难?这道题的重点在query函数方面,建议自己在纸上模拟一下建树和查询

【POJ2104】K-th Number 主席树?函数式线段树?可持久化线段树?……反正是其中一个

题意:区间静态第K大. 题解: 可持久化线段树. 可持久化线段树: 基本思想:我们维护插入每个节点后的线段树. 朴素写法(MLE+TLE)我们对于每次插入,都复制一棵线段树而后插入,这样保证了"可持久化". 但是显然,时间复杂度和空间复杂度都是n^2的.233. 所以有了优化写法:我们发现每次的插入只有logn个节点被改变,所以只需要这些点新建,其它点都指向原来版本的节点就好了. 空间复杂度nlogn. 然后这道题是区间第K大,对于区间[a,b],插入到b时的线段树,节点的size-(

AcWing 255. 第K小数 (主席树写法)

区间k小数是主席树的模板题目,如果区间不包含,用莫队+权值线段树也能解 主席树是可持久化线段树,所为可持久化,就是每次只新增不一样的节点,而保留前面的版本,这样可以做到查询. 如果询问时1-r,那么直接主席树,询问的是l-r,就用到前缀和思想,具体看代码注释 #include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<vector> #inclu

POJ2104 K-th Number[主席树]

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 51440   Accepted: 17594 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse

poj2104 K-th Number 主席树入门;

题目链接:K-th Number 题解:我们先把数组离散离散化一下,然后先不考虑L,R的区间的关系,我们有一个棵线段树sum[]保存的是第几大到第几大出现的个数,这样我们想要询问这颗线段数的第k大是多少可以在log(n)次下就找到,但是区间的不同,一颗线段树是解决不了的,那我们如何得到L,R区间的sum数组呢?.我们可以建N棵线段树,第一棵树是空树,然后第一个数过来我们再建一课线段树在原来树的基础上,加上这个数对sum数组的贡献,这样从第一个到第N个建N棵线段树建好,我们可以发现sum[]有前缀

poj2104求区间第k小,静态主席树入门模板

看了很久的主席树,最后看https://blog.csdn.net/williamsun0122/article/details/77871278这篇终于看懂了 #include <stdio.h> #include<algorithm> using namespace std; typedef long long ll; const int maxn = 1e5+5; int T[maxn],L[maxn*20],R[maxn*20],sum[maxn*20]; //sz[]为原

zoj2112 主席树动态第k大 (主席树&amp;&amp;树状数组)

Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They

hdoj 2665 Kth number主席树裸

Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9417    Accepted Submission(s): 2938 Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The fi

4504: K个串 主席树+优先队列

这道题因为有一个数在序列中出现多次只算一次的限制.我们可以这样搞.假设在当前题意下求给定右端点的区间最值.那么我们可以预处理出每个数前一次出现的位置pre[i] .接下来从左到右加入每一个值,就是在 pre[i] + 1 —— i 这个区间内加上 v[i] 的值,这样就可以得到以当前 i 点为右端点的各个区间的值(很明显维护一下最值就好了).接下来很明显有n个版本的线段树(如果你说一开始那个空的线段树也算一个版本的话,就有n+1个),那就要用主席树动态开点.而取第K大值的操作有点像超级钢琴,不过

【POJ 2104】 K-th Number 主席树模板题

达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没有评测,但我立下flag这个代码一定能A.我的同学在自习课上考语文,然而机房党都跑到机房来避难了\(^o^)/~ #include<cstdio> #include<cstring> #include<algorithm> #define for1(i,a,n) for(i