Sequence II

Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 849    Accepted Submission(s): 204

Problem Description
Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,an There are m queries.

In the i-th query, you are given two integers li and ri. Consider the subsequence ali,ali+1,ali+2,⋯,ari.

We can denote the positions(the positions according to the original sequence) where an integer appears first in this subsequence as p(i)1,p(i)2,⋯,p(i)ki (in ascending order, i.e.,p(i)1<p(i)2<⋯<p(i)ki).

Note that ki is the number of different integers in this subsequence. You should output p(i)⌈ki2⌉for the i-th query.

 
Input
In the first line of input, there is an integer T (T≤2) denoting the number of test cases.

Each test case starts with two integers n (n≤2×105) and m (m≤2×105). There are n integers in the next line, which indicate the integers in the sequence(i.e., a1,a2,⋯,an,0≤ai≤2×105).

There are two integers li and ri in the following m lines.

However, Mr. Frog thought that this problem was too young too simple so he became angry. He modified each query to l‘i,r‘i(1≤l‘i≤n,1≤r‘i≤n). As a result, the problem became more exciting.

We can denote the answers as ans1,ans2,⋯,ansm. Note that for each test case ans0=0.

You can get the correct input li,ri from what you read (we denote them as l‘i,r‘i)by the following formula:

li=min{(l‘i+ansi−1) mod n+1,(r‘i+ansi−1) mod n+1}
ri=max{(l‘i+ansi−1) mod n+1,(r‘i+ansi−1) mod n+1}
 
Output
You should output one single line for each test case.

For each test case, output one line “Case #x: p1,p2,⋯,pm”, where x is the case number (starting from 1) and p1,p2,⋯,pm is the answer.

 
Sample Input
2
5 2
3 3 1 5 4
2 2
4 4
5 2
2 5 2 1 2
2 3
2 4
 
Sample Output
Case #1: 3 3
Case #2: 3 1

Hint

  1. /*
  2. hdu 5919 主席树(区间不同数的个数 + 区间第k大)
  3.  
  4. problem:
  5. 给你n个数字,和m个查询.
  6. 将[l,r]之间数第一次出现的位置信息弄成一个新的数组,然后找出其中k/2大的数.(k为位置的数量)
  7.  
  8. solve:
  9. 通过主席树能够找出[l,r]之间有多少个不同的数,然后利用再用一个查询找出第k大的即可.
  10. (都是类似与线段树的操作, T[i]存的是[1,n]的信息, 尽管说的只是[i,n] ,只是[1,i-1]的还没更新而已. 所以查询的时候出了点问题)
  11.  
  12. hhh-2016-10-07 16:48:19
  13. */
  14. #include <algorithm>
  15. #include <iostream>
  16. #include <cstdlib>
  17. #include <stdio.h>
  18. #include <cstring>
  19. #include <vector>
  20. #include <math.h>
  21. #include <queue>
  22. #include <set>
  23. #include <map>
  24. //#define lson i<<1
  25. //#define rson i<<1|1
  26. #define ll long long
  27. #define clr(a,b) memset(a,b,sizeof(a))
  28.  
  29. using namespace std;
  30.  
  31. const int maxn = 200100;
  32. const int N = maxn * 100;
  33.  
  34. template<class T> void read(T&num)
  35. {
  36. char CH;
  37. bool F=false;
  38. for(CH=getchar(); CH<'0'||CH>'9'; F= CH=='-',CH=getchar());
  39. for(num=0; CH>='0'&&CH<='9'; num=num*10+CH-'0',CH=getchar());
  40. F && (num=-num);
  41. }
  42. int stk[70], tp;
  43. template<class T> inline void print(T p)
  44. {
  45. if(!p)
  46. {
  47. puts("0");
  48. return;
  49. }
  50. while(p) stk[++ tp] = p%10, p/=10;
  51. while(tp) putchar(stk[tp--] + '0');
  52. putchar('\n');
  53. }
  54.  
  55. int lson[N],rson[N],c[N];
  56. int a[maxn],T[maxn];
  57. int tot,n,m;
  58.  
  59. int build(int l,int r)
  60. {
  61. int root = tot++;
  62. c[root ] = 0;
  63. if(l != r)
  64. {
  65. int mid = (l+r)>>1;
  66. lson[root] = build(l,mid);
  67. rson[root] = build(mid+1,r);
  68. }
  69. return root;
  70. }
  71.  
  72. int update(int root,int pos,int val)
  73. {
  74. int newroot = tot ++ ,tmp= newroot;
  75. c[newroot] = c[root] + val;
  76. int l = 1,r = n;
  77. while(l < r)
  78. {
  79. int mid = (l + r) >> 1;
  80. if(pos <= mid)
  81. {
  82. lson[newroot] = tot++;
  83. rson[newroot] = rson[root];
  84. newroot = lson[newroot] ;
  85. root = lson[root];
  86. r = mid;
  87. }
  88. else
  89. {
  90. lson[newroot] = lson[root],rson[newroot] = tot++;
  91. newroot = rson[newroot],root = rson[root];
  92. l = mid + 1;
  93. }
  94. c[newroot] = c[root] + val;
  95. }
  96. return tmp;
  97. }
  98.  
  99. int query(int root,int pos)
  100. {
  101. int cnt = 0;
  102. int l = 1,r = n;
  103. while(pos < r)
  104. {
  105. int mid = (l + r) >> 1;
  106. if(pos <= mid)
  107. {
  108. root = lson[root];
  109. r = mid;
  110. }
  111. else
  112. {
  113. cnt += c[lson[root]];
  114. root = rson[root];
  115. l = mid + 1;
  116. }
  117. }
  118. return cnt + c[root];
  119. }
  120.  
  121. int Find(int root,int k)
  122. {
  123. int l = 1,r = n;
  124. while(l <= r)
  125. {
  126. int mid = (l + r) >> 1;
  127. if(l == r)
  128. return l;
  129. if(c[lson[root]] >= k)
  130. {
  131. root = lson[root];
  132. r = mid;
  133. }
  134. else
  135. {
  136. k -= c[lson[root]];
  137. root = rson[root];
  138. l = mid +1 ;
  139. }
  140. }
  141. }
  142.  
  143. int main()
  144. {
  145. int t,cas = 1;
  146. // freopen("in.txt","r",stdin);
  147. read(t);
  148. while(t--)
  149. {
  150. tot = 0;
  151. read(n),read(m);
  152. for(int i = 1; i <= n; i++)
  153. scanf("%d",&a[i]);
  154. T[n + 1] = build(1,n);
  155. map<int,int> mp;
  156. for(int i = n; i >= 1; i--)
  157. {
  158. if(mp.find(a[i]) == mp.end())
  159. {
  160. T[i] = update(T[i + 1],i,1);
  161. }
  162. else
  163. {
  164. int tp = update(T[i+1],mp[a[i]],-1);
  165. T[i] = update(tp,i,1);
  166. }
  167. mp[a[i]] = i;
  168. }
  169. int ans = 0;
  170. int l,r;
  171. printf("Case #%d:",cas++);
  172. for(int i = 1; i <= m; i++)
  173. {
  174. read(l),read(r);
  175. // cout << l <<" " <<r << endl;
  176. l = (l + ans) % n + 1;
  177. r = (r + ans)%n + 1;
  178. if(l > r)
  179. swap(l,r);
  180. int num = (query(T[l],r)+1) >> 1;
  181. // if(!num) num = 1;
  182. ans = Find(T[l],num);
  183. printf(" %d",ans);
  184. }
  185. printf("\n");
  186. }
  187. return 0;
  188. }

  

hdu 5919 主席树(区间不同数的个数 + 区间第k大)的更多相关文章

  1. HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数

    xiaoxin and his watermelon candy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5654 Description Du ...

  2. Super Mario HDU 4417 主席树区间查询

    Super Mario HDU 4417 主席树区间查询 题意 给你n个数(编号从0开始),然后查询区间内小于k的数的个数. 解题思路 这个可以使用主席树来处理,因为这个很类似查询区间内的第k小的问题 ...

  3. SPOJ 3267 求区间不同数的个数

    题意:给定一个数列,每次查询一个区间不同数的个数. 做法:离线+BIT维护.将查询按右端点排序.从左到右扫,如果该数之前出现过,则将之前出现过的位置相应删除:当前位置则添加1.这样做就保证每次扫描到的 ...

  4. 主席树的各类模板(区间第k大数【动,静】,区间不同数的个数,区间<=k的个数)

    取板粗   好东西来的 1.(HDOJ2665)http://acm.hdu.edu.cn/showproblem.php?pid=2665 (POJ2104)http://poj.org/probl ...

  5. HDU 4348 主席树区间更新

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  6. SPOJ:D-query(非常规主席树求区间不同数的个数)

    Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...

  7. SPOJ - DQUERY (主席树求区间不同数的个数)

    题目链接:https://vjudge.net/problem/SPOJ-DQUERY 题目大意:给定一个含有n个数的序列,有q个询问,每次询问区间[l,r]中不同数的个数. 解题思路:从左向右一个一 ...

  8. HDU 6278 主席树(区间第k大)+二分

    Just h-index Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)To ...

  9. POJ 2104 HDU 2665 主席树 解决区间第K大

    两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...

随机推荐

  1. 201621123031 《Java程序设计》第6周学习总结

    作业06-接口.内部类 1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多 ...

  2. 201621123043 《Java程序设计》第1周学习总结

    1. 本章学习总结 Jdk的安装: eclipse的基本使用方法 Java发展史 jdk.jre.jvm 关键词之间的联系:是整个java的核心,包括了一堆java.java基础的类库.java运行环 ...

  3. C简单实现双向链表

    <pre name="code" class="cpp">//链表结构 typedef struct DulNode { DataType data ...

  4. 【iOS】swift 让程序挂起后,能在后台继续运行任务

    1,程序的挂起和退出 由于iOS设备资源有限.当用户点击了home键,或者另一个应用程序启动了.那么原先那个程序便进入后台被挂起,不是退出,只是停止执行代码,同时它的内存被锁定.当应用程序恢复时,它会 ...

  5. 新手入门 git

    Git是目前世界上最先进的分布式版本控制系统 特点:高端大气上档次 什么是版本控制系统 系统自动记录文件改动 方便同事协作管理 不用自己管理一堆类似的文件了,也不需要把文件传来传去.如果想查看某次改动 ...

  6. python使用tesseract-ocr完成验证码识别(安装部分)

    一.tesseract-ocr安装 Ubuntu版本: 1.tesseract-ocr安装 sudo apt-get install tesseract-ocr 2.pytesseract安装 sud ...

  7. TFTP通信原理

    TFTP的通信流程 TFTP共定义了五种类型的包格式,格式的区分由包数据前两个字节的Opcode字段区分,分别是: · l 读文件请求包:Read request,简写为RRQ,对应Opcode字段值 ...

  8. Python内置函数(26)——enumerate

    英文文档: enumerate(iterable, start=0) Return an enumerate object. iterable must be a sequence, an itera ...

  9. Apollo单向SSL认证(2)

    一.生成ks和ts 二.连接测试 1.配置 2.测试

  10. kubernetes入门(09)kubernetes1.7集群安装(2017/11/13)

    CentOS7.3利用kubeadm安装kubernetes1.7.3完整版(官方文档填坑篇) https://www.cnblogs.com/liangDream/p/7358847.html 一. ...