HDU 2665 Kth number 主席树裸题

题目链接

主席树详解

每次插入logn个点 这样就不需要重新建树了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9) pt(x / 10);
	putchar(x % 10 + '0');
}
typedef long long ll;
typedef pair<int, int> pii;
const int N = 100000 + 100;
#define L(x) tree[x].lson
#define R(x) tree[x].rson
#define S(x) tree[x].sum
struct Node {
	int lson, rson;
	int sum;
}tree[N << 5];
int top;
vector<int>Hash;
int gethash(int val) {
	return lower_bound(Hash.begin(), Hash.end(), val) - Hash.begin() + 1;
}
int build(int l, int r) {
	int id = ++top;
	S(id) = 0;
	if (l < r) {
		int mid = (l + r) >> 1;
		L(id) = build(l, mid);
		R(id) = build(mid + 1, r);
	}
	return id;
}
int update(int pre, int l, int r, int val) {
	int id = ++top;
	L(id) = L(pre); R(id) = R(pre);
	S(id) = S(pre) + 1;
	if (l < r) {
		int mid = (l + r) >> 1;
		if (val <= mid)
			L(id) = update(L(pre), l, mid, val);
		else
			R(id) = update(R(pre), mid + 1, r, val);
	}
	return id;
}
int query(int old, int cur, int l, int r, int k) {
	if (l >= r)return l;
	int mid = (l + r) >> 1;
	int siz = S(L(cur)) - S(L(old));
	if (siz < k)
		return query(R(old), R(cur), mid + 1, r, k - siz);
	else
		return query(L(old), L(cur), l, mid, k);
}
int n, q;
int a[N];
int T[N];
int main() {
	int cas; rd(cas);
	while (cas--) {
		rd(n); rd(q);
		Hash.clear();
		top = 0;
		for (int i = 1; i <= n; i++) {
			rd(a[i]);
			Hash.push_back(a[i]);
		}
		sort(Hash.begin(), Hash.end()); Hash.erase(unique(Hash.begin(), Hash.end()), Hash.end());
		T[0] = build(1, Hash.size());
		for (int i = 1; i <= n; i++) {
			T[i] = update(T[i - 1], 1, Hash.size(), gethash(a[i]));
		}
		while (q--) {
			int l, r, k;
			rd(l); rd(r); rd(k);
			int id = query(T[l - 1], T[r], 1, Hash.size(), k);
			pt(Hash[id - 1]); puts("");
		}
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 13:34:23

HDU 2665 Kth number 主席树裸题的相关文章

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

HDU 2665 Kth number (主席树)

题意:给定一个序列,求给定区间的第 k 小的值. 析:就是一个主席树的裸板. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring&g

【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

hdu 2665 Kth number(划分树)

题意:给定一列数,每次查询区间[s,t]中的第k大: 参考:http://www.cnblogs.com/kane0526/archive/2013/04/20/3033212.html http://www.cnblogs.com/kuangbin/archive/2012/08/14/2638829.html 思路:快排思想+线段树=划分树,也就是树的每一层都按规则划分: 对于本题,建树前,保存原数列排序后的数列,原数列作为树的顶层: 建树时,考虑当前区间中的中间值,若大于中间值,在下一层中

Poj 2104 K-th Number 主席树模版题

题意:离线询问[l,r]区间第k大 题解:模版题,入门题 #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <map> #include <queue> #include <vector> #include <cstring> #include <iomanip> #include

hdu 2665 Kth number(划分树)

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

POJ 2104&amp;HDU 2665 Kth number(主席树入门+离散化)

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

HDU 2665.Kth number 区间第K小

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

hdu 2665 Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12119    Accepted Submission(s): 3697 Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The first line i