【模板】可持久化线段树 1(主席树)
题目背景
这是个非常经典的主席树入门题——静态区间第K小
数据已经过加强,请使用主席树。同时请注意常数优化
题目描述
如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值。
输入输出格式
输入格式:
第一行包含两个正整数N、M,分别表示序列的长度和查询的个数。
第二行包含N个正整数,表示这个序列各项的数字。
接下来M行每行包含三个整数l,r,k l, r, kl,r,k , 表示查询区间[l,r][l, r][l,r]内的第k小值。
输出格式:
输出包含k行,每行1个正整数,依次表示每一次查询的结果
输入输出样例
输入样例#1: 复制
5 5 25957 6405 15770 26287 26465 2 2 1 3 4 1 4 5 1 1 2 2 4 4 1
输出样例#1: 复制
6405 15770 26287 25957 26287
主席树入门;
注意要离散化
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#pragma GCC optimize(2) //#include<cctype> //#pragma GCC optimize("O3") using namespace std; #define maxn 700005 #define inf 0x3f3f3f3f #define INF 9999999999 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9 + 7; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-3 typedef pair<int, int> pii; #define pi acos(-1.0) //const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) inline ll rd() { ll x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } ll sqr(ll x) { return x * x; } /*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ans = exgcd(b, a%b, x, y); ll t = x; x = y; y = t - a / b * y; return ans; } */ ll qpow(ll a, ll b, ll c) { ll ans = 1; a = a % c; while (b) { if (b % 2)ans = ans * a%c; b /= 2; a = a * a%c; } return ans; } int n, m, q; int cnt = 0; int a[maxn], b[maxn], T[maxn]; int sum[maxn << 5], lson[maxn << 5], rson[maxn << 5]; int build(int l, int r) { int rt = ++cnt; sum[rt] = 0; if (l < r) { int mid = (l + r) >> 1; lson[rt] = build(l, mid); rson[rt] = build(mid + 1, r); } return rt; } int upd(int pre, int l, int r, int x) { int rt = ++cnt; lson[rt] = lson[pre]; rson[rt] = rson[pre]; sum[rt] = sum[pre] + 1; if (l < r) { int mid = (l + r) >> 1; if (x <= mid)lson[rt] = upd(lson[pre], l, mid, x); else rson[rt] = upd(rson[pre], mid + 1, r, x); } return rt; } int query(int u, int v, int l, int r, int k) { if (l >= r)return l; int x = sum[lson[v]] - sum[lson[u]]; int mid = (l + r) >> 1; if (x >= k)return query(lson[u], lson[v], l, mid, k); else return query(rson[u], rson[v], mid + 1, r, k - x); } int main(){ //ios::sync_with_stdio(0); rdint(n); rdint(q); for (int i = 1; i <= n; i++)rdint(a[i]), b[i] = a[i]; sort(b + 1, b + 1 + n); m = unique(b + 1, b + 1 + n) - b - 1; T[0] = build(1, m); for (int i = 1; i <= n; i++) { int t = lower_bound(b + 1, b + 1 + m, a[i]) - b; T[i] = upd(T[i - 1], 1, m, t); } while (q--) { int x, y, z; rdint(x); rdint(y); rdint(z); int t = query(T[x - 1], T[y], 1, m, z); cout << b[t] << endl; } return 0; }
EPFL - Fighting