划分树 K-th Number
来源:互联网 发布:2016年网络直播元年 编辑:程序博客网 时间:2024/05/21 23:31
K-th Number
Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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.
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).
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 42 5 34 4 11 7 3
Sample Output
563
#include <iostream>#include <algorithm>#include <string.h>#define MAXN 100010using namespace std;class Node{public:int l, r;};class SGtree{public:Node node[MAXN << 2];int num_left[20][MAXN];int sg_node[20][MAXN];int parray[MAXN];void init();void Maketree(int i, int d, int l, int r);int Query(int i, int d, int x, int y, int k);}tree;void SGtree::init(){memset(num_left, 0, sizeof(0));memset(sg_node, 0, sizeof(sg_node));memset(node, 0, sizeof(node));}void SGtree::Maketree(int o, int d, int l, int r){node[o].l = l;node[o].r = r;if (l == r){return ;}int mid = (l + r) >> 1;int issame = mid - l + 1;for (int i = l; i <= r; i++){if (sg_node[d][i] < parray[mid]){issame--;}}int pl = l, pr = mid + 1;for (int i = l; i <= r; i++){if (i == l){num_left[d][i] = 0;}else{num_left[d][i] = num_left[d][i - 1];}if (sg_node[d][i] < parray[mid]){num_left[d][i]++;sg_node[d + 1][pl++] = sg_node[d][i];}if (sg_node[d][i] > parray[mid]){sg_node[d + 1][pr++] = sg_node[d][i];}if (sg_node[d][i] == parray[mid]){if (issame > 0){issame--;num_left[d][i]++;sg_node[d + 1][pl++] = sg_node[d][i];}else{sg_node[d + 1][pr++] = sg_node[d][i];}}}Maketree(2 * o, d + 1, l, mid);Maketree(2 * o + 1, d + 1, mid + 1, r);}int SGtree::Query(int i, int d, int x, int y, int k){int l = node[i].l;int r = node[i].r;int mid = (l + r) >> 1;if (l == r){return sg_node[d][x];}int lnum, ltornum;if (x == node[i].l){lnum = 0;}else{lnum = num_left[d][x - 1];}ltornum = num_left[d][y] - lnum;if (ltornum >= k){return Query(i * 2, d + 1, l + lnum, l + lnum + ltornum - 1, k);}else{int a = x - l - lnum;int b = y - x - ltornum;return Query(i * 2 + 1, d + 1, mid + a + 1, mid + a + b + 1, k - ltornum);}}void input(){int t, n, m, x, y, k;while (scanf("%d %d", &n, &m) != EOF){tree.init();for (int i = 1; i <= n; i++){scanf("%d", &tree.parray[i]);tree.sg_node[1][i] = tree.parray[i];}sort(tree.parray + 1, tree.parray + n + 1);tree.Maketree(1, 1, 1, n);while (m--){scanf("%d %d %d", &x, &y, &k);printf("%d\n", tree.Query(1, 1, x, y, k));}}}int main(){input();return 0;}
- 划分树 K-th Number
- 【划分树】K-th Number
- POJ2104 POJ2761 K-th Number, 划分树
- POJ 2104 K-th Number 划分树
- ★【划分树】K-th Number
- poj 2104 K-th Number--划分树
- poj 2104 K-th Number(划分树)
- POJ 2104 K-th Number 划分树
- 【划分树】K-th Number POJ2104 POJ2761
- [POJ 2104] K-th Number [划分树]
- K-th Number (划分树学习)
- poj 2104 K-th Number(划分树)
- POJ 2104 K-th Number 划分树
- POJ 2104 K-th Number 划分树
- poj2104 K-th Number(划分树)
- poj2104--K-th Number(划分树)
- K-th Number - POJ 2104 划分树
- K-th Number (POJ_2104) 划分树
- ios 学习之你画我话绘图七 椭圆形
- HDU 2121 Ice_cream’s world II 最小树形图(不定根)
- web应用安全防范(1)—为什么要重视web应用安全漏洞
- IOS回调机制——代理,通知中心以及Block
- "No previous prototype for function" warning警告错误解决
- 划分树 K-th Number
- 使用Spring提供的 MethodInvokingJobDetailFactoryBean 代理类调度定时器
- 杭电ACM Step 纪念
- lua split
- 一列固定宽度居中
- 鼠鼠百科--固态硬盘
- 面试(二)
- 划分树 Kth number
- 如何正确理解PHP获取显示数据库数据函数