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 r strictly more than times or - 1 if there is no such number. Help Leha with such a difficult task.node
Inputc++
First line of input data contains two integers n and q (1 ≤ n, q ≤ 3·105) — number of elements in the array and number of queries respectively.spa
Next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — Leha's array.code
Each of next q lines contains three integers l, r and k (1 ≤ l ≤ r ≤ n, 2 ≤ k ≤ 5) — description of the queries.orm
Outputblog
Output answer for each query in new line.three
Examplesip
4 2
1 1 2 2
1 3 2
1 4 2
1
-1
5 3
1 2 1 3 2
2 5 3
1 2 3
5 5 2
2
1
2
題意:給定N個數,Q個詢問,每一個詢問給出L,R,K,求這個區間出現次數大於num=(R-L+1)/K的最小數,沒有則輸出-1。element
思路:參照上一篇博客的「主席樹求區間衆數」,這一題也差很少。 一樣用主席樹記錄前綴出現次數,線段樹上跑的時候,若是這個區間的出現次數小於num,那麼忽略這個區間。若是大於大於num,那麼再去子區間找是否尋在答案。input
(最近狀態不錯額,作的幾個題都有手感。ORZ
#include<bits/stdc++.h> using namespace std; const int maxn=300010; struct node{ int val,l,r; node() {} node(int L,int R,int V):l(L),r(R),val(V){} }s[maxn*20]; int rt[maxn],cnt,ans; void insert(int &now,int pre,int L,int R,int pos,int val) { s[now=++cnt]=node(s[pre].l,s[pre].r,s[pre].val+val); if(L==R) return ; int Mid=(L+R)>>1; if(pos<=Mid) insert(s[now].l,s[pre].l,L,Mid,pos,val); else insert(s[now].r,s[pre].r,Mid+1,R,pos,val); } int query(int now,int pre,int L,int R,int times) { if(L==R) return L; int Mid=(L+R)>>1; int res=maxn,tmp; if(s[s[now].l].val-s[s[pre].l].val>times){ tmp=query(s[now].l,s[pre].l,L,Mid,times); if(tmp!=-1) res=min(res,tmp); } if(s[s[now].r].val-s[s[pre].r].val>times){ tmp=query(s[now].r,s[pre].r,Mid+1,R,times); if(tmp!=-1) res=min(res,tmp); } if(res==maxn) res=-1; return res; } int main() { int N,K,Q,x,y,i,j; scanf("%d%d",&N,&Q); for(i=1;i<=N;i++){ scanf("%d",&x); insert(rt[i],rt[i-1],1,N,x,1); } for(i=1;i<=Q;i++){ scanf("%d%d%d",&x,&y,&K); printf("%d\n",query(rt[y],rt[x-1],1,N,(y-x+1)/K)); } return 0; }