【HDU - 2665】Kth number(区间第K大,主席树,模板)
題干:
Give you a sequence and ask you the kth big number of a inteval.
Input
The first line is the number of the test cases.?
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.?
The second line contains n integers, describe the sequence.?
Each of following m lines contains three integers s, t, k.?
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
Output
For each test case, output m lines. Each line contains the kth big number.
Sample Input
1 10 1 1 4 2 3 5 6 7 8 9 0 1 3 2Sample Output
2解題報告:
求區間第K小值。
第一行兩個整數n和m (n, m <= 100000)
第二行n個整數
接下來m行,每行3個整數s,t,k.表示要查詢區間[s,t]內第k小的。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 1e5 + 5; struct TREE {int l,r;int val; } tr[MAX*35];int tot; int a[MAX],b[MAX]; int root[MAX]; int build(int l,int r) {int cur = ++tot;tr[cur].val = 0;if(l == r) {tr[cur].l = tr[cur].r = 0;return cur;}int m = (l+r)>>1;tr[cur].l = build(l,m);tr[cur].r = build(m+1,r);return cur; } int update(int pre,int tar,int l,int r) {int cur = ++tot;tr[cur] = tr[pre];tr[cur].val++;if(l == r) return cur;int m = (l+r)>>1;if(tar <= m) tr[cur].l = update(tr[pre].l,tar,l,m);else tr[cur].r = update(tr[pre].r,tar,m+1,r);return cur; } int query(int pl,int pr,int l,int r,int k) {if(l == r) return l;int m = (l+r)>>1;if(tr[tr[pr].l].val - tr[tr[pl].l].val >= k) return query(tr[pl].l,tr[pr].l,l,m,k);else return query(tr[pl].r,tr[pr].r,m+1,r,k - (tr[tr[pr].l].val - tr[tr[pl].l].val)); } int main() {int n,m;int t;cin>>t;while(t--) {cin>>n>>m;tot=0;for(int i = 1; i<=n; i++) scanf("%d",a+i),b[i] = a[i];root[0] = build(1,n);sort(b+1,b+n+1);int LEN = unique(b+1,b+n+1) - b - 1;for(int i = 1; i<=n; i++) {int pos = lower_bound(b+1,b+LEN+1,a[i]) - b;root[i] = update(root[i-1],pos,1,n);}while(m--) {int l,r,k;scanf("%d%d%d",&l,&r,&k);printf("%d\n",b[query(root[l-1],root[r],1,n,k)]);}}return 0 ; }?
總結
以上是生活随笔為你收集整理的【HDU - 2665】Kth number(区间第K大,主席树,模板)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SWNETSUP.EXE - SWNET
- 下一篇: swstrtr.exe - swstrt