Description

You are given an array A consisting of N positive integers. You have to answer Q queries on it of following type:

  • l r k : Let S denote the sorted (in increasing order) set of elements of array A with its indices between l and r. Note that set Scontains distinct elements (i.e. no duplicates).
    You need to find kth number in it. If such a number does not exist, i.e. the S has less than k elements, output -1.

All the indices in the queries are 1-based.

Input

The first line of input contains two space separated integers N and Q denoting the number of elements in A, and the number of queries, respectively.

The second line of input contains N space separated integers denoting the array A.

Each of the next Q lines contains five integers aibicidiki.
We will generate liri indices for this query as follows:

Let answer for i - 1th query equal ansi - 1.
For 0th query ans0 = 0.
Define li = (ai x max(ansi - 1, 0) + bi) mod N + 1,
ri = (ci x max(ansi-1, 0) + di) mod N + 1.
If li > ri, then swap li and ri.

Output

For each query, output the answer to the query in a single line. If such a number doesn't exist, output -1.

Constraints

  • 1 ≤ N, Q ≤ 105
  • 1 ≤ Ai ≤ 109
  • 0 ≤ aibicidi ≤ N
  • 1 ≤ li ≤ ri ≤ N
  • 1 ≤ ki ≤ N

Example

Input:
4 4
3 2 1 2
0 1 0 3 2
2 0 0 3 4
1 2 1 3 2
2 0 0 3 3 Output:
2
-1
2
3 Input:
10 10
9 10 6 3 8 4 9 6 4 10
0 2 0 9 3
1 9 1 3 3
1 8 1 0 3
1 2 1 7 2
1 6 1 2 3
1 4 1 3 1
1 6 1 6 1
1 4 1 8 1
1 9 1 3 3
1 9 1 2 1 Output:
6
9
10
4
6
3
10
4
6
4

Subtasks

  • Subtask #1 (10 points) : Q x ≤ 107
  • Subtask #2 (20 points) : ki = 1
  • Subtask #3 (30 points) : ai = 0, ci = 0
  • Subtask #4 (40 points) : Original constraints

Explanation

Example #1:

Query 1. Sorted set of elements : {1, 2}. Second number in this is 2.

Query 2. Sorted set of elements : {1, 2, 3}. Fourth number doesn't exist, hence answer is -1.

Query 3. Sorted set of elements : {1, 2}. Second number in this set is 2.

Query 4. Sorted set of elements : {1, 2, 3}. Third number in this set is 3.

题意:

  给定长度为N的序列A,其中每个元素都有正整数。

  你需要回答Q个询问:

    l,r,k:记s为序列 A下标在l到r之间的元素按照升序排列得到的序列(重复元素只留一个)。

    你需要求出其第k个元素的值,如果包含小于k个元素,则输出-1.

    下标从1开始编号

题解:

  线段树,每个节点保存不含重复元素的动态数组

  查询的时候二分就OK 复杂度O( q*logn*logn)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 1e5+, M = 2e2+, inf = 2e9, mod = 1e9+;
typedef long long ll;
int n, q;
ll ar[N],num[N];
vector< ll > da[ * N];
void merges(vector<ll> &a, vector<ll> &b, vector<ll> &c)
{
int lenb = , lenc = ;
while(lenb < b.size() && lenc < c.size()) {
if(b[lenb] == c[lenc]) {
a.push_back(b[lenb]);
lenb++, lenc++;
}else {
if(b[lenb] < c[lenc]) {
a.push_back(b[lenb++]);
} else a.push_back(c[lenc++]); }
}
while(lenb < b.size()) {
a.push_back(b[lenb++]);
}
while(lenc < c.size()) {
a.push_back(c[lenc++]);
}
} void build(int k,int l,int r) {
if(r == l) {
da[k].push_back(ar[l]);
return ;
}
build(k<<,l,(l+r)/);build(k<<|,(r+l)/+,r);
merges(da[k],da[k<<],da[k<<|]);
}
ll query(int i,int j,ll x,int k,int l,int r) {
if(i==l&&j==r) return upper_bound(da[k].begin(),da[k].end(),x) - da[k].begin();
else {
int mid = (l+r)>>;
if(j<=mid) return query(i,j,x,k<<,l,mid);
else if(i>mid) return query(i,j,x,k<<|,mid+,r);
else return query(i,mid,x,k<<,l,mid)+query(mid+,j,x,k<<|,mid+,r);
}
} ll solve(int l,int r,int k) {
int lb = , rb = n, ans = ;
while(lb<=rb) {
int mid = (lb+rb)>>;
if(query(l,r,num[mid],,,n)>=k) rb = mid-, ans = mid;
else lb = mid + ;
// cout<<1<<endl;
}
if(query(l,r,num[ans],,,n)<k) {
return -;
}
else return num[ans];
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++) scanf("%lld",&ar[i]), num[i] = ar[i];
sort(num+,num+n+);
build(,,n);
ll pre = ;
for(int i=;i<=q;i++) {
ll a,b,c,d,k;
scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k);
int l = (a*max(pre,0ll)+b) % n + ;
int r = (c*max(pre,0ll)+d) % n + ;
printf("%d\n",pre = solve(l,r,k));
}
}

CodeChef DISTNUM2 Easy Queries 节点数组线段树的更多相关文章

  1. 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树

    正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...

  2. Can you answer these queries? HDU 4027 线段树

    Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来 ...

  3. 树状数组 && 线段树应用 -- 求逆序数

    参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...

  4. hdu1394(枚举/树状数组/线段树单点更新&区间求和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...

  5. BZOJ 4636 (动态开节点)线段树

    思路: 偷懒 懒得离散化 搞了个动态开节点的线段树 (其实是一样的--..) 注意会有a=b的情况 要判掉 //By SiriusRen #include <cstdio> #includ ...

  6. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  7. hdu 5147 Sequence II【树状数组/线段树】

    Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  8. SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)

    GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...

  9. hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. 回家前的挣扎——SQLite增删改查

    引言 最后一天,公司就两个人,也不知道弄点什么,就在网上找了Sqlite的文档,看了看,这里也是现学现卖,给自己找点事做,感觉时间过得还是比较快的,不然焦急等待,滋味不好受啊. SQLite简介 SQ ...

  2. 【原创】angularjs1.3.0源码解析之directive

    # Angular指令编译原理 前言 angular之所以使用起来很方便,是因为通常我们只需要在html里面引入一个或多个(自定义或内置的)指令就可以完成一个特定的功能(这也是angular推荐的方式 ...

  3. jQuery 事件用法详解

    jQuery 事件用法详解 目录 简介 实现原理 事件操作 绑定事件 解除事件 触发事件 事件委托 事件操作进阶 阻止默认事件 阻止事件传播 阻止事件向后执行 命名空间 自定义事件 事件队列 jque ...

  4. SQL Server2008ldf文件太大

    --适用于SQL Server 2008的方法 USE [master] GO ALTER DATABASE RmyyHisDW SET RECOVERY SIMPLE WITH NO_WAIT GO ...

  5. 黑色30s高并发IIS设置

    在这篇博文中,我们抛开对阿里云的怀疑,完全从ASP.NET的角度进行分析,看能不能找到针对问题现象的更合理的解释. “黑色30秒”问题现象的主要特征是:排队的请求(Requests Queued)突增 ...

  6. java中堆栈(stack)和堆(heap)

    原文地址:http://blog.csdn.net/jerryao/article/details/874101 1.内存分配策略 按照编译原理的观点,程序运行时的内存分配有三种策略,分别是静态的,栈 ...

  7. php面试题之三——PHP语言基础(基础部分)

    三.PHP语言基础 1. strlen( )与 mb_strlen( )的作用分别是什么(新浪网技术部) strlen和mb_strlen都是用于获取字符串长度. strlen只针对单字节编码字符,也 ...

  8. CentOS 7安装Splunk

    导读 Splunk是探索和搜索数据的最有力工具,从收集和分析应用程序.Web服务器.数据库和服务器平台的实时可视化海量数据流,分析出IT企业产生的海量数据,安全系统或任何商业应用,给你一个总的见解获得 ...

  9. Unity3d iOS基本优化和高级优化

    原地址:http://www.cocoachina.com/bbs/read.php?tid=70395&page=1 分享看见的2篇好文.简单翻译了一下并且放出原文 http://www.c ...

  10. ris'In App Purchase总结

    原地址:http://www.cocoachina.com/bbs/read.php?tid=38555&page=1 In App Purchase属于iPhone SDK3.0的新特性,用 ...