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.

Input

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.

Next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — Leha's array.

Each of next q lines contains three integers lr and k (1 ≤ l ≤ r ≤ n, 2 ≤ k ≤ 5) — description of the queries.

Output

Output answer for each query in new line.

Examples

Input
4 2
1 1 2 2
1 3 2
1 4 2
Output
1
-1
Input
5 3
1 2 1 3 2
2 5 3
1 2 3
5 5 2
Output
2
1
2

题意:给定N个数,Q个询问,每个询问给出L,R,K,求这个区间出现次数大于num=(R-L+1)/K的最小数,没有则输出-1。

思路:参照上一篇博客的“主席树求区间众数”,这一题也差不多。 同样用主席树记录前缀出现次数,线段树上跑的时候,如果这个区间的出现次数小于num,那么忽略这个区间。如果大于大于num,那么再去子区间找是否寻在答案。

(最近状态不错额,做的几个题都有手感。ORZ

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node{
int val,l,r;
node() {}
node(int L,int R,int V):l(L),r(R),val(V){}
}s[maxn*];
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)>>;
if(pos<=Mid) insert(s[now].l,s[pre].l,L,Mid,pos,val);
else insert(s[now].r,s[pre].r,Mid+,R,pos,val);
}
int query(int now,int pre,int L,int R,int times)
{
if(L==R) return L;
int Mid=(L+R)>>;
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!=-) 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+,R,times);
if(tmp!=-) res=min(res,tmp);
}
if(res==maxn) res=-;
return res;
}
int main()
{
int N,K,Q,x,y,i,j;
scanf("%d%d",&N,&Q);
for(i=;i<=N;i++){
scanf("%d",&x);
insert(rt[i],rt[i-],,N,x,);
}
for(i=;i<=Q;i++){
scanf("%d%d%d",&x,&y,&K);
printf("%d\n",query(rt[y],rt[x-],,N,(y-x+)/K));
}
return ;
}

CodeForces - 840D:(主席树求出现区间出现次数大于某值的最小数)的更多相关文章

  1. 主席树——求静态区间第k大

    例题:poj2104 http://poj.org/problem?id=2104 讲解:http://blog.sina.com.cn/s/blog_6022c4720102w03t.html ht ...

  2. 主席树——求区间第k个不同的数字(向右密集hdu5919)

    和向左密集比起来向右密集只需要进行小小的额修改,就是更新的时候从右往左更新.. 自己写的被卡死时间.不知道怎么回事,和网上博客的没啥区别.. /* 给定一个n个数的序列a 每次询问区间[l,r],求出 ...

  3. luogu P3834 【模板】可持久化线段树 1(主席树) 查询区间 [l, r] 内的第 k 小/大值

    ————————————————版权声明:本文为CSDN博主「ModestCoder_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https:// ...

  4. L - A Heap of Heaps CodeForces - 538F 主席树

    L - A Heap of Heaps CodeForces - 538F 这个是一个还比较裸的静态主席树. 这个题目的意思是把这个数组变成k叉树,然后问构成的树的子树小于等于它的父节点的对数有多少. ...

  5. SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

  6. HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)

    题目链接 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2 ...

  8. 主席树——求区间[l,r]不同数字个数的模板(向左密集 D-query)

    主席树的另一种用途,,(还有一种是求区间第k大,区间<=k的个数) 事实上:每个版本的主席树维护了每个值最后出现的位置 这种主席树不是以权值线段树为基础,而是以普通的线段树为下标的 /* 无修改 ...

  9. SPOJ DQUERY (主席树求区间不同数个数)

    题意:找n个数中无修改的区间不同数个数 题解:使用主席树在线做,我们不能使用权值线段树建主席树 我们需要这么想:从左向右添加一到主席树上,添加的是该数字处在的位置 但是如果该数字前面出现过,就在此版本 ...

随机推荐

  1. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers 远程Event Receivers App级别生命周期

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers  远程Event Receivers App级别生命周期 ...

  2. DICOM:DICOM万能编辑工具之Sante DICOM Editor

    版权声明:本文为zssure原创文章,转载请注明出处,未经允许不得转载.   目录(?)[-] 背景 DICOM Service的配置 Sante DICOM Editor自启动的服务 PACS查询下 ...

  3. NMM3DViewer 设计

    在FrameworkInterfaces工程的INMM3DServer.cs中定义了 岩石材料结构 BlockMaterial  -----> StrBLOCKProperty     publ ...

  4. netd ResponseCode

    100 Requestion action was initiated; expect another reply before proceeding with a new command. 200 ...

  5. ssh bitbucket github

    $ ssh-keygen -t rsa -C "mac" $ vi ~/.ssh/config Host bb User git HostName bitbucket.org Id ...

  6. Odoo webinar

    分享些 odoo9 webinar 视频     https://pan.baidu.com/s/1pLF5njt

  7. python(36)- 测试题

    1.8<<2等于? 32 “<<”位运算 264 132 64 32 16 8 4 2 1 原始位置 0 0 0 0 0 1 0 0 0 想左位移2位 0 0 0 1 0 0 ...

  8. 函数式编程( Functional)与命令式编程( Imperative)对比

    1.函数式编程带来的好处 函数式编程近些年异军突起,又重新回到了人们的视线,并得到蓬勃发展.总结起来,无外乎如下好处: 1.减少了可变量(Immutable Variable)的声明,程序更为安全.  ...

  9. Android摄像头採集的视频数据流怎样通过Socket实时发送到目标服务端

    分两块: 1.取得摄像头採集的视频流 2.发送到server端 protected MediaRecorder mMediaRecorder; private LocalServerSocket mL ...

  10. Zoj2421 广搜

    <span style="color:#330099;">/* M - 广搜 加强 Time Limit:2000MS Memory Limit:65536KB 64b ...