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. cocos2d-x step by step(2) 鼠标事件,键盘事件,等等事件

    各种小控件加载进去了,那么问题来了,这些东西如何接受事件呢? good job,let us find the answer 首先我们去看文档,官方尼玛有好多文档,而且大,全,详细,感觉还是不错的 h ...

  2. Why is chkconfig no longer available in Ubuntu?

    Question: I can not use chkconfig tools in Ubuntu 12.10 It's a very useful tools to configure the se ...

  3. Git使用笔记01

    本文基本的參考文章链接例如以下所看到的: Pro Git(中文版) Git教程 Git使用教程 简单介绍与说明 Git是一个分布式版本号管理系统,是为了更好地管理Linux内核开发而创立的.Git能够 ...

  4. binary-tree-level-order-traversal I、II——输出二叉树的数字序列

    I Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to righ ...

  5. C++11 并发指南系列(转)

    本系列文章主要介绍 C++11 并发编程,计划分为 9 章介绍 C++11 的并发和多线程编程,分别如下: C++11 并发指南一(C++11 多线程初探)(本章计划 1-2 篇,已完成 1 篇) C ...

  6. python 大小写转换方法(全)

    http://blog.csdn.net/liuxincumt/article/details/7945337  python大小写转换(全)

  7. python(21)- python内置函数练习

    题目一:用map来处理字符串列表啊,把列表中所有人都变成sb,比方alex_sbname=['alex','wupeiqi','yuanhao'] name=['alex','wupeiqi','yu ...

  8. Hnu 11187 Emoticons :-) (ac自己主动机+贪心)

    题目大意: 破坏文本串.使之没有没有出现表情.破坏就是用空格替换.问最少须要破坏多少个字符. 思路分析: 初看跟Hdu 2457 没什么差别,事实上Hdu2457是要求将字符替换成ACGT,而这个仅仅 ...

  9. linux find prune排除某目录或文件

    http://blog.csdn.net/ysdaniel/article/details/7995681 查找cache目录下不是html的文件 find ./cache ! -name '*.ht ...

  10. caffe2--Install

    Install Welcome to Caffe2! Get started with deep learning today by following the step by step guide ...