题意

description

You are given an array a1,a2,...,an(∀i∈[1,n],1≤ai≤n). Initially, each element of the array is **unique**.

Moreover, there are m instructions.

Each instruction is in one of the following two formats:

  1. (1,pos),indicating to change the value of apos to apos+10,000,000;
  2. (2,r,k),indicating to ask the minimum value which is not equal to any ai ( 1≤i≤r ) and **not less ** than k.

Please print all results of the instructions in format 2.

### input

The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.

In each test case, there are two integers n(1≤n≤100,000),m(1≤m≤100,000) in the first line, denoting the size of array a and the number of instructions.

In the second line, there are n distinct integers a1,a2,...,an (∀i∈[1,n],1≤ai≤n),denoting the array.

For the following m lines, each line is of format (1,t1) or (2,t2,t3).

The parameters of each instruction are generated by such way :

For instructions in format 1 , we defined pos=t1⊕LastAns . (It is promised that 1≤pos≤n)

For instructions in format 2 , we defined r=t2⊕LastAns,k=t3⊕LastAns. (It is promised that 1≤r≤n,1≤k≤n )

(Note that ⊕ means the bitwise XOR operator. )

Before the first instruction of each test case, LastAns is equal to 0 .After each instruction in format 2, LastAns will be changed to the result of that instruction.

(∑n≤510,000,∑m≤510,000 )

## 分析
题目中,共有两种操作,一种是将第k个数字+1000,0000,一种是求一个最小的、不等于ai(1
注意 这个数字,可以不出现在array中

因为array中的数字各不相同,因此,不等于ai(1 <= i <= r),等价于 这个数字可能会出现在 ai(r + 1 <= i <= n)。

为什么是可能?因为由于第一种操作+1000,10000,会使得进行第一种操作的那个数字,无论如何都不会和ai重复(1000,0000 比 51,0000大太多了)。

所以,这个题,我们可以将主席树进行适当的修改,每次查找ai[r + 1, n]中,不小于k的最小数字。

由于本题中,array由1-n的n个数字组成,因此不用考虑数字的去重(unique),以及 离散化问题。

除了上面两种情况,还有一种情况,我们没考虑到,如果k = n (已经过异或运算),那么这个答案还有可能是 n + 1。

综上,答案一共有三种情况:

  • ai[r + 1, n] 中的某个数字 (可以用主席树求解)
  • 进行过第一种操作的某个数字 (将进行过第一种操作的数字放入set中)
  • n + 1

三个数字,求最小值即可

代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <set>
  4. #include <algorithm>
  5. int const maxn = 530000;
  6. int const inf = 0x3f3f3f3f;
  7. using namespace std;
  8. int a[maxn], b[maxn];
  9. int root[maxn << 5];//第几个版本的根节点编号
  10. int lc[maxn << 5], rc[maxn << 5], sum[maxn << 5];
  11. int sz;//节点个数
  12. int n, m;
  13. void build(int &rt, int l, int r) {
  14. rt = ++sz;
  15. if (l == r) return;
  16. int mid = (l + r) >> 1;
  17. build(lc[rt], l, mid);
  18. build(rc[rt], mid + 1, r);
  19. }
  20. int update(int id, int l, int r, int pos) {
  21. int _id = ++sz;
  22. lc[_id] = lc[id], rc[_id] = rc[id], sum[_id] = sum[id] + 1;
  23. if (l == r) return _id;
  24. int mid = (r + l) >> 1;
  25. if (pos <= mid)
  26. lc[_id] = update(lc[id], l, mid, pos);
  27. else
  28. rc[_id] = update(rc[id], mid + 1, r, pos);
  29. return _id;
  30. }
  31. //查询 不比k大的最小数字
  32. int query(int p, int q, int l, int r, int k) {
  33. if (l == r) return l;
  34. int x1 = sum[lc[q]] - sum[lc[p]];
  35. int x2 = sum[rc[q]] - sum[rc[p]];
  36. int mid = (l + r) >> 1;
  37. int ans = inf;
  38. if (x1 > 0 && mid >= k)
  39. ans = query(lc[p], lc[q], l, mid, k);
  40. //这个if不能写为else,因为第一个if可能无法得到结果,返回inf
  41. if(ans == inf && x2 > 0 && r >= k)
  42. ans = query(rc[p], rc[q], mid + 1, r, k);
  43. return ans;
  44. }
  45. int main() {
  46. int T;
  47. scanf("%d", &T);
  48. while (T--) {
  49. while (~scanf("%d %d", &n, &m)) {
  50. sz = 0;
  51. set<int> s;
  52. int lastAns = 0;
  53. for (int i = 1; i <= n; i++) {
  54. scanf("%d", &a[i]);
  55. b[i] = a[i];
  56. }
  57. sort(b + 1, b + n + 1);
  58. build(root[0], 1, n);
  59. for (int i = 1; i <= n; i++) {
  60. int pos = lower_bound(b + 1, b + n + 1, a[i]) - b;
  61. root[i] = update(root[i - 1], 1, n, pos);
  62. }
  63. while (m--) {
  64. int l;
  65. scanf("%d", &l);
  66. if (l == 1) {
  67. int pos = 1; // 随意初始化
  68. scanf("%d", &pos);
  69. pos ^= lastAns;
  70. //cout << "pos = " << pos << endl;
  71. s.insert(a[pos]);
  72. }
  73. else {
  74. int l, k;
  75. scanf("%d %d", &l, &k);
  76. l ^= lastAns;
  77. k ^= lastAns;
  78. //cout << "l = " << l << "k = " << k << endl;
  79. int ansPos = query(root[l - 1 + 1], root[n], 1, n, k);
  80. lastAns = (ansPos == inf) ? inf : b[ansPos];
  81. set<int>::iterator it = s.lower_bound(k);
  82. //
  83. if (it != s.end()) lastAns = min(lastAns, *it);
  84. lastAns = min(lastAns, n + 1);
  85. printf("%d\n", lastAns);
  86. }
  87. }
  88. }
  89. }
  90. return 0;

hdoj6703 2019 CCPC网络选拔赛 1002 array的更多相关文章

  1. hdoj6708 2019 CCPC网络选拔赛 1007 Windows Of CCPC

    #include <cstdio> #include <iostream> #include <algorithm> using namespace std; ch ...

  2. 2016 ccpc 网络选拔赛 F. Robots

    Robots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  3. CCPC 2019 网络赛 1002 array (权值线段树)

    HDU 6703 array   题意:   给定一个数组 \(a_1,a_2, a_3,...a_n\) ,满足 \(1 \le a[i]\le n\) 且 \(a[i]\) 互不相同.   有两种 ...

  4. 2019CCPC网络选拔赛 hdu6703 array(主席树+set)

    题意 给你一个1~n的排列,由两种操作: 1 pos:将a[pos]+10 000 000 2 r k:求大于等于k且不等于a[1~r]的数的最小值. 强制在线. 思路 如果没有1操作,那么我们直接主 ...

  5. 2016 CCPC网络选拔赛 部分题解

    HDU 5832 - A water problem 题意:有两颗星球,一年的长度分别为37天和173天.问第n天时它们是否为新年的第一天. 思路:显然  n 同时被37和173整除时,两种历法都在新 ...

  6. 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛(8/11)

    $$2019中国大学生程序设计竞赛(CCPC)\ -\ 网络选拔赛$$ \(A.\hat{} \& \hat{}\) 签到,只把AB都有的位给异或掉 //#pragma comment(lin ...

  7. 【赛后总结+部分题解】2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛

    赛后总结: T:今天状态一般,甚至有点疲惫.然后12点比赛开始,和队友开始看题,从最后往前面看,发现数学题公式看不懂.然后发现队友已经双开做1001和1006了,我看着1007有人A,开始做1007. ...

  8. [BFS,A*,k短路径] 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 path (Problem - 6705)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6705 path Time Limit: 2000/2000 MS (Java/Others)    Mem ...

  9. [贪心,dp] 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 Fishing Master (Problem - 6709)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6709 Fishing Master Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. Java进阶学习(4)之继承与多态.demo

    多媒体数据库小练习 package com.dome; import java.util.ArrayList; public class Database { // private ArrayList ...

  2. React源码解析之React.Children.map()(五)

    一,React.Children是什么? 是为了处理this.props.children(this.props.children表示所有组件的子节点)这个属性提供的工具,是顶层的api之一 二,Re ...

  3. TestNG单元测试与使用详解

    TestNG的基本注解与执行顺序 在类里编辑程序时,在@Test后面,摁 alt+回车,选择对应的插件,可以把目前用到的插件自动添加到 pom.xml 文件中,如下面的testng,每摁一次,就多添加 ...

  4. 中国大学MOOC 邮箱验证的问题

    在使用 中国大学 MOOC 过程中,在PC端修改个人资料时,其中有项“常用邮箱”,于是写了QQ邮箱,结果发现一直无法验证,连邮件都无法收到. 经过多番尝试,重新使用邮箱注册的方式注册账号,然后注册成功 ...

  5. windows10下VS2013搭建opencv2.4.9吐血之作

    1.下载opencv2.4.9.exe文件,然后双击extract文件到指定目录 2.按此链接安装opencv:https://www.cnblogs.com/cuteshongshong/p/405 ...

  6. List 线性表:ArrayLis,LinkedList

    package seday11.list; import java.util.ArrayList;import java.util.List; /*** @author xingsir * java. ...

  7. __dirname和__filename和process.cwd()三者的区别

    1.process cwd() 方法返回 Node.js 进程当前工作的目录 例:我在F:\自己的文件\自己在网上学习的知识点\node学习\node-API\process 这个文件加下面创建了一个 ...

  8. Node.js Learning Notes

    简介 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件驱动I/O服务 ...

  9. 介绍Mobility Group

    Mobility或Roaming是无线客户端能够安全地从一个AP无缝关联到另一个AP的能力,并且延迟尽可能的短. 当无线客户端和AP关联并通过AP进行身份验证时,注册AP的WLC会将客户端条目放在自己 ...

  10. 广度优先搜索(BFS)与深度优先搜索(DFS)的对比及优缺点

    深搜,顾名思义,是深入其中.直取结果的一种搜索方法. 如果深搜是一个人,那么他的性格一定倔得像头牛!他从一点出发去旅游,只朝着一个方向走,除非路断了,他绝不改变方向!除非四个方向全都不通或遇到终点,他 ...