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:

  1. Let answer for i - 1th query equal ansi - 1.
  2. For 0th query ans0 = 0.
  3. Define li = (ai x max(ansi - 1, 0) + bi) mod N + 1,
  4. ri = (ci x max(ansi-1, 0) + di) mod N + 1.
  5. 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

  1. Input:
  2. 4 4
  3. 3 2 1 2
  4. 0 1 0 3 2
  5. 2 0 0 3 4
  6. 1 2 1 3 2
  7. 2 0 0 3 3
  8.  
  9. Output:
  10. 2
  11. -1
  12. 2
  13. 3
  14.  
  15. Input:
  16. 10 10
  17. 9 10 6 3 8 4 9 6 4 10
  18. 0 2 0 9 3
  19. 1 9 1 3 3
  20. 1 8 1 0 3
  21. 1 2 1 7 2
  22. 1 6 1 2 3
  23. 1 4 1 3 1
  24. 1 6 1 6 1
  25. 1 4 1 8 1
  26. 1 9 1 3 3
  27. 1 9 1 2 1
  28.  
  29. Output:
  30. 6
  31. 9
  32. 10
  33. 4
  34. 6
  35. 3
  36. 10
  37. 4
  38. 6
  39. 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)

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<vector>
  7. using namespace std;
  8. const int N = 1e5+, M = 2e2+, inf = 2e9, mod = 1e9+;
  9. typedef long long ll;
  10. int n, q;
  11. ll ar[N],num[N];
  12. vector< ll > da[ * N];
  13. void merges(vector<ll> &a, vector<ll> &b, vector<ll> &c)
  14. {
  15. int lenb = , lenc = ;
  16. while(lenb < b.size() && lenc < c.size()) {
  17. if(b[lenb] == c[lenc]) {
  18. a.push_back(b[lenb]);
  19. lenb++, lenc++;
  20. }else {
  21. if(b[lenb] < c[lenc]) {
  22. a.push_back(b[lenb++]);
  23. } else a.push_back(c[lenc++]);
  24.  
  25. }
  26. }
  27. while(lenb < b.size()) {
  28. a.push_back(b[lenb++]);
  29. }
  30. while(lenc < c.size()) {
  31. a.push_back(c[lenc++]);
  32. }
  33. }
  34.  
  35. void build(int k,int l,int r) {
  36. if(r == l) {
  37. da[k].push_back(ar[l]);
  38. return ;
  39. }
  40. build(k<<,l,(l+r)/);build(k<<|,(r+l)/+,r);
  41. merges(da[k],da[k<<],da[k<<|]);
  42. }
  43. ll query(int i,int j,ll x,int k,int l,int r) {
  44. if(i==l&&j==r) return upper_bound(da[k].begin(),da[k].end(),x) - da[k].begin();
  45. else {
  46. int mid = (l+r)>>;
  47. if(j<=mid) return query(i,j,x,k<<,l,mid);
  48. else if(i>mid) return query(i,j,x,k<<|,mid+,r);
  49. else return query(i,mid,x,k<<,l,mid)+query(mid+,j,x,k<<|,mid+,r);
  50. }
  51. }
  52.  
  53. ll solve(int l,int r,int k) {
  54. int lb = , rb = n, ans = ;
  55. while(lb<=rb) {
  56. int mid = (lb+rb)>>;
  57. if(query(l,r,num[mid],,,n)>=k) rb = mid-, ans = mid;
  58. else lb = mid + ;
  59. // cout<<1<<endl;
  60. }
  61. if(query(l,r,num[ans],,,n)<k) {
  62. return -;
  63. }
  64. else return num[ans];
  65. }
  66. int main()
  67. {
  68. scanf("%d%d",&n,&q);
  69. for(int i=;i<=n;i++) scanf("%lld",&ar[i]), num[i] = ar[i];
  70. sort(num+,num+n+);
  71. build(,,n);
  72. ll pre = ;
  73. for(int i=;i<=q;i++) {
  74. ll a,b,c,d,k;
  75. scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k);
  76. int l = (a*max(pre,0ll)+b) % n + ;
  77. int r = (c*max(pre,0ll)+d) % n + ;
  78. printf("%d\n",pre = solve(l,r,k));
  79. }
  80. }

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. DIV+CSS例子

    DIV水平居中+垂直居中 #main_zone{ width:1190px; height:570px; background-color:#fff; margin:0 auto; /*左右居中*/ ...

  2. 构建seajs业务模块之grunt VS spm build

    在最开始,我并不知道grunt可以构建CMD模块.(以下spm指代spm build) 当时正困惑于如何用spm方便的构建业务模块,后来看到@twinstony (感谢@twinstony的分享)使用 ...

  3. 移动端富文本编辑器artEditor

    摘要: 由于手机上打字比较慢,并不适合长篇大论的文章,所以移动端的富文本编辑器很少.artEditor是一款基于jQuery的移动端富文本编辑器,支持插入图片,后续完善其他功能. 插件地址:https ...

  4. Power of Cryptography(用double的泰勒公式可行分析)

    Power of Cryptography Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onli ...

  5. unity缓存和浏览器缓存

    原地址:http://www.cnblogs.com/hisiqi/p/3203553.html <蒸汽之城>游戏中,为什么会黑屏?或者无法正常进入游戏? 我们在进行多项测试中发现少数用户 ...

  6. change column to bigint

    今天存储数据的时候报错,发现是3435065640超出了常规int的存储长度, RangeError (3435065640 is out of range for ActiveRecord::Typ ...

  7. rubycas-client单点登录

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 进行中,未完待续 Ruby 客户端 使用方法0. 在 Gemfile中,加入: gem 'rubyc ...

  8. 2015安徽省赛 C.LU的困惑

    题目描述 Master LU 非常喜欢数学,现在有个问题:在二维空间上一共有n个点,LU每连接两个点,就会确定一条直线,对应有一个斜率.现在LU把平面内所有点中任意两点连线,得到的斜率放入一个集合中( ...

  9. asp.net 网站 或者web Api 发布

    asp.net 发布iis时可能遇到的内部服务错误常见的有两种: 1.如下图,500.19 Internal Server Error(内部服务错误) 这种错误可能是由于本机的注册表中的asp.net ...

  10. Linux CPU负载

    昨天查看Nagios警报信息,发现其中一台服务器CPU负载过重,机器为CentOS系统.信息如下: 2011-2-15 (星期二) 17:50 WARNING - load average: 9.73 ...