poj2104 K-th Number
2017-03-05 19:00
246 查看
K-th Number
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 31 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
Sample Output
56
3
Hint
This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.题目大意
多次询问给定序列区间第k大值。题解
主席数模板,不过多解释了。线下评测RE两个点。。不是很懂为什么。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int N = 100000 + 10, lg = 50; struct Node{ int s; Node *lc, *rc; }; Node nil[N * lg], *rot ; int tot; int n, m, a , b ; inline void in(int &x){ int f = 1; x = 0; char c = getchar(); while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();} x *= f; } Node* insert(Node *u, int l, int r, int p){ Node *ret = nil + (++tot); *ret = *u; ret->s ++; if(l == r) return ret; int mid = (l + r) / 2; if(p <= mid) ret->lc = insert(ret->lc, l, mid, p); else ret->rc = insert(ret->rc, mid+1, r, p); return ret; } int query(Node *u, Node *v, int l, int r, int k){ if(l == r) return b[l]; int mid = (l + r) / 2; int c = u -> lc -> s - v -> lc -> s; if(k <= c) return query(u->lc, v->lc, l, mid, k); return query(u->rc, v->rc, mid + 1, r, k - c); } void init(){ scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++){ in(a[i]); b[i] = a[i]; } sort(b+1, b+n+1); b[0] = unique(b+1, b+n+1) - b - 1; for(int i = 1; i <= n; i++) a[i] = lower_bound(b+1, b+b[0]+1, a[i]) - b; } void work(){ tot = nil->s = 0, rot[0] = nil->lc = nil->rc = nil; for(int i = 1; i <= n; i++) rot[i] = insert(rot[i-1], 1, b[0], a[i]); while(m--) { int l, r, k; in(l); in(r); in(k); printf("%d\n", query(rot[r], rot[l - 1], 1, b[0], k)); } } int main(){ init(); work(); return 0; }
相关文章推荐
- 【POJ2104】K-th Number 主席树?函数式线段树?可持久化线段树?……反正是其中一个
- [POJ2104]K-th Number(主席树,静态区间k小)
- poj2104 K-th Number(整体二分+树状数组)
- [poj2104]K-th Number
- 【poj2104】K-th Number 归并树
- [poj2104] K-th Number (主席树)
- POJ2104 K-th Number【主席树】
- poj2104 K-th Number【主席树模板】
- POJ2104 k-th number 划分树
- [POJ2104]K-th Number(区间第k值 记录初始状态)
- 【POJ2104】K-th Number-主席树(可持久化线段树)+离散化
- POJ2104--K-th Number(主席树)
- poj2104 K-th NumberK-th Number【主席树】
- 【序列莫队+二分答案+树状数组】POJ2104-K-th Number
- POJ2104 K-th Number [分块做法]
- poj2104 k-th number 主席树入门讲解
- [POJ2104]K-th Number 做题笔记 主席树
- [POJ2104]K-th Number
- kyeremal-poj2104-K-th Number-主席树
- POJ2104-K-th Number-求区间第K大数(暴力or归并树or划分树)