[HDU5919]Sequence II

试题描述

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 al_i,al_(i+1),al_(i+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)k_i (in ascending order, i.e.,p(i)1<p(i)2<⋯<p(i)k_i).

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

输入

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}

输出

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.

输入示例

  1.  

输出示例

  1. Case #:
  2. Case #:

数据规模及约定

见“输入

题解

就是这道题再强行套一个二分。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <stack>
  6. #include <vector>
  7. #include <queue>
  8. #include <cstring>
  9. #include <string>
  10. #include <map>
  11. #include <set>
  12. using namespace std;
  13.  
  14. const int BufferSize = 1 << 16;
  15. char buffer[BufferSize], *Head, *Tail;
  16. inline char Getchar() {
  17. if(Head == Tail) {
  18. int l = fread(buffer, 1, BufferSize, stdin);
  19. Tail = (Head = buffer) + l;
  20. }
  21. return *Head++;
  22. }
  23. int read() {
  24. int x = 0, f = 1; char c = Getchar();
  25. while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
  26. while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
  27. return x * f;
  28. }
  29.  
  30. #define maxn 200010
  31. #define maxnode 4000010
  32.  
  33. int ToT, sumv[maxnode], lc[maxnode], rc[maxnode];
  34. void update(int& y, int x, int l, int r, int p) {
  35. sumv[y = ++ToT] = sumv[x] + 1;
  36. if(l == r) return ;
  37. int mid = l + r >> 1; lc[y] = lc[x]; rc[y] = rc[x];
  38. if(p <= mid) update(lc[y], lc[x], l, mid, p);
  39. else update(rc[y], rc[x], mid + 1, r, p);
  40. return ;
  41. }
  42. int query(int o, int l, int r, int qr) {
  43. if(!o) return 0;
  44. if(r <= qr) return sumv[o];
  45. int mid = l + r >> 1, ans = query(lc[o], l, mid, qr);
  46. if(qr > mid) ans += query(rc[o], mid + 1, r, qr);
  47. return ans;
  48. }
  49.  
  50. int rt[maxn], lstp[maxn], ANS[maxn], cnt;
  51.  
  52. int len;
  53. char Out[maxn*7];
  54. int main() {
  55. int T = read();
  56. for(int kase = 1; kase <= T; kase++) {
  57. memset(lstp, 0, sizeof(lstp));
  58. memset(sumv, 0, sizeof(sumv));
  59. memset(lc, 0, sizeof(lc));
  60. memset(rc, 0, sizeof(rc));
  61. memset(rt, 0, sizeof(rt));
  62. ToT = 0;
  63. int n = read(), q = read();
  64. for(int i = 1; i <= n; i++) {
  65. int v = read();
  66. update(rt[i], rt[i-1], 0, n, lstp[v]);
  67. lstp[v] = i;
  68. }
  69.  
  70. cnt = 0;
  71. int lst = 0;
  72. while(q--) {
  73. int ql = (read() + lst) % n + 1, qr = (read() + lst) % n + 1;
  74. if(ql > qr) swap(ql, qr);
  75. int l = ql, r = qr, k = query(rt[qr], 0, n, ql - 1) - query(rt[ql-1], 0, n, ql - 1) + 1 >> 1, lval = query(rt[ql-1], 0, n, ql - 1);
  76. while(l < r) {
  77. int mid = l + r >> 1;
  78. if(query(rt[mid], 0, n, ql - 1) - lval < k)
  79. l = mid + 1;
  80. else r = mid;
  81. }
  82. ANS[++cnt] = lst = l;
  83. }
  84.  
  85. printf("Case #%d: ", kase);
  86. len = 0;
  87. int num[10], cntn;
  88. for(int i = 1; i <= cnt; i++) {
  89. int tmp = ANS[i];
  90. if(!tmp) Out[len++] = '0';
  91. cntn = 0; while(tmp) num[++cntn] = tmp % 10, tmp /= 10;
  92. for(int j = cntn; j; j--) Out[len++] = num[j] + '0';
  93. if(i < cnt) Out[len++] = ' ';
  94. }
  95. Out[len] = '\0';
  96. puts(Out);
  97. }
  98.  
  99. return 0;
  100. }

[HDU5919]Sequence II的更多相关文章

  1. HDU5919 Sequence II(主席树)

    Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...

  2. HDU 5919 Sequence II 主席树

    Sequence II Problem Description   Mr. Frog has an integer sequence of length n, which can be denoted ...

  3. HDU 5919 Sequence II(主席树+逆序思想)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

  4. HDOJ 5147 Sequence II 树阵

    树阵: 每个号码的前面维修比其数数少,和大量的这后一种数比他的数字 再枚举每一个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    ...

  5. bestcoder#23 1002 Sequence II 树状数组+DP

    Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. hdu 5147 Sequence II 树状数组

    Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Prob ...

  7. hdu 5147 Sequence II (树状数组 求逆序数)

    题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. Sequence II

    6990: Sequence II 时间限制: 3 Sec  内存限制: 128 MB提交: 206  解决: 23[提交][状态][讨论版][命题人:admin] 题目描述 We define an ...

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

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

随机推荐

  1. Web service简介 与servletContext的参数

    Web service顾名思义是基于web的服务,它是一种跨平台,跨语言的服务. 我们可以这样理解它,比如说我们可以调用互联网上查询天气信息的web服务,把它嵌入到我们的B/S程序中,当用户从我们的网 ...

  2. zoj3768Continuous Login

    链接 这题通过暴力可以看出最多不超过3 具体为什么..等着看大牛的题解. 可以预处理出来两个数之和 用bool存下 然后枚举一个数 二分剩余数的位置就可以了 勉强可过 #include <ios ...

  3. 【C#】.net 发送get/post请求

    基础学习 /// <summary> /// Http (GET/POST) /// </summary> /// <param name="url" ...

  4. 【前端】模拟微信上传图片(带预览,支持预览gif)

    一.Html <style type="text/css"> #previewDiv{width:50px;height:50px;overflow:hidden;po ...

  5. js中json处理总结之JSON.parse

    踩过的坑都将成为路上的风景.队友在cookie中已存以下值: address_info {"address_name":"人民大会堂","...lng ...

  6. Unity复杂的旋转-欧拉角和四元数

    一.欧拉角欧拉角最容易表示,用三个变量X,Y,Z可以直观的表示绕着某个轴的旋转角度. 在Unity里就是Transform组件的Rotation里的X Y Z三个变量代表了欧拉角 二.四元数四元数相比 ...

  7. centos下安装nodejs的三种种方式

    方法一:源码包安装 官网下载 centos下载最新版10.9 https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz mkdir / ...

  8. PHP03 移动互联网和PHP

    学习要点 移动互联网 云计算 网络通信协议 Apache http服务器 PHP运行原理 学习目标 理解网络通信协议 掌握PHP运行原理 WAMP开发环境的搭建   移动互联网 定义 移动互联网,就是 ...

  9. OpenCV2:第九章 图像比较

    一.简介 图像相似度主要是对两幅图像内容的相似程度进行打分,根据分数的高低来判断图像内容的相似程度. 常见的图像比较有两种方法:峰值信噪比PSNR和结构相似性SSIM 二.峰值信噪比PSNR(Peak ...

  10. QT5:总结篇 控件集合

    一.Layouts 二.Spacers 三.Buttons 四.Item Views(Model-Based) 五.Item Widgets(Item-Based) 六.Containers 七.In ...