kyeremal-poj2104-K-th Number-主席树
来源:互联网 发布:java投票统计 编辑:程序博客网 时间:2024/05/22 05:21
poj2104-K-th Number
K-th Number
Time Limit: 20000MS Memory Limit: 65536KTotal Submissions: 31790 Accepted: 9838Case 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.
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
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 100010int 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;}
0 0
- kyeremal-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 主席树
- 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【主席树】
- HDU2665 POJ2104 K-th Number(主席树)
- poj2104 K-th Number【主席树模板】
- poj2104 K-th NumberK-th Number【主席树】
- nginx反代mogilefs实现
- 点击LinearLayout使用selector改变TextView字体颜色
- Android快速开发系列 10个常用工具类
- HTTP头
- 《实体解析与信息质量》- 2.1.4 信息质量管理
- kyeremal-poj2104-K-th Number-主席树
- 加载项目时:-pie can only be used when targeting iOS 4.2 or later 错误
- nyoj1112 求次数 (对结构体字符串排序)
- 浮点数的存储
- IOS开发之—程序截图
- 轻量级Java EE企业应用实战-----18、事件机制
- excel2010 把两个表分开方法
- excel转PDF转换器推荐
- M3U8格式讲解及实际应用