codeforces 840D 主席树
2017-11-10 23:51
309 查看
Once, Leha found in the left pocket an array consisting of n integers, and in the right pocket q queries of the form l r k.If there are queries, then they must be answered. Answer for the query is minimal x such that x occurs in the interval l rstrictlymore than times or - 1 if there isno such number. Help Leha with such a difficult task.InputFirst line of input data contains two integers n and q (1 ≤ n, q ≤ 3·105) — number of elements inthe array and number of queries respectively.Next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n)— Leha's array.Each of next q lines contains three integers l, r and k (1 ≤ l ≤ r ≤ n, 2 ≤ k ≤ 5)— description of the queries.OutputOutput answer for each query in new line.ExampleInput
4 2 1 1 2 2 1 3 2 1 4 2Output
1 -1Input
5 3 1 2 1 3 2 2 5 3 1 2 3 5 5 2Output
2 12一开始想不到怎么搞,因为树的权值无法同时代表着次数和具体的值,如果暴力找感觉会很慢。然而其实并不会,因为一个区间出现的总数是固定的,如果每个链出现的次数多,那么别的链访问的次数就少了,所以只要sum[you]-sum[zuo]<k 我们就不访问了,树的下标是具体的值,权值的出现的次数,就很简单了#include <bits/stdc++.h>using namespace std;const int N = 3e5+10;int ls[N*21],rs[N*21],sum[N*21];int T,a;int tot;void update(int &now,int pre,int l,int r,int d){now=++tot;sum[now]=sum[pre]+1;ls[now]=ls[pre];rs[now]=rs[pre];if(l==r)return ;int mid=(l+r)>>1;if(d<=mid) update(ls[now],ls[pre],l,mid,d);else update(rs[now],rs[pre],mid+1,r,d);}int ans;void query(int zuo,int you,int l,int r,int d){if(sum[you]-sum[zuo]<d) return ;if(l==r){if(ans==-1)ans=l;return ;}int mid=(l+r)>>1;query(ls[zuo],ls[you],l,mid,d);query(rs[zuo],rs[you],mid+1,r,d);}int main(){int n,q;tot=0;scanf("%d%d",&n,&q);for(int i=1;i<=n;i++)scanf("%d",&a[i]);for(int i=1;i<=n;i++)update(T[i],T[i-1],1,n,a[i]);while(q--){int l,r,k;scanf("%d%d%d",&l,&r,&k);int vv=(r-l+1+k)/k;ans=-1;query(T[l-1],T[r],1,n,vv);printf("%d\n",ans );}}
相关文章推荐
- CodeForces - 840D:(主席树求出现区间出现次数大于某值的最小数)
- Codeforces 840D Destiny 【主席树】
- codeforces 840D Destiny 主席树
- [主席树] Codeforces 840D .Destiny
- Codeforces 840D [主席树]
- CodeForces 547E Mike and Friends [后缀树组+主席树]
- 【CodeForces】960 F. Pathwalks 主席树+动态规划
- CodeForces 669E Little Artem and Time Machine(主席树+树状数组)
- Codeforces 786C. Till I Collapse 主席树
- Codeforces 813E Army Creation 主席树(在线,求[l,r]内比x大的数的个数)
- Codeforces 464E #265 (Div. 1) E. The Classic Problem 主席树+Hash
- Codeforces 811B Vladik and Complicated Book[主席树]
- Codeforces 813E Army Creation 主席树(在线,求[l,r]内比x大的数的个数)
- Codeforces 893F Subtree Minimum Query(Hard) 主席树
- [最短路 主席树 Hash] Codeforces 464E #265 (Div. 1) E. The Classic Problem
- Codeforces 813E Army Creation 主席树(在线,求[l,r]内比x大的数的个数)
- Codeforces 813E Army Creation 主席树(在线,求[l,r]内比x大的数的个数)
- Codeforces 813E Army Creation 主席树(在线,求[l,r]内比x大的数的个数)
- Codeforces 458C Elections 贿赂选票抢主席! 线段树
- Codeforces 813E Army Creation 主席树(在线,求[l,r]内比x大的数的个数)