题目链接

Operation

Problem Description

There is an integer sequence a of length n and there are two kinds of operations:

  • 0 l r: select some numbers from al...ar so that their xor sum is maximum, and print the maximum value.

  • 1 x: append x to the end of the sequence and let n=n+1.

Input

There are multiple test cases. The first line of input contains an integer T(T≤10), indicating the number of test cases.

For each test case:

The first line contains two integers n,m\((1≤n≤5×10^5,1≤m≤5×10^5)\), the number of integers initially in the sequence and the number of operations.

The second line contains n integers \(a1,a2,...,an(0≤ai<2^{30})\), denoting the initial sequence.

Each of the next m lines contains one of the operations given above.

It's guaranteed that \(∑n≤10^6,∑m≤10^6,0≤x<2^{30}\).

And operations will be encrypted. You need to decode the operations as follows, where lastans denotes the answer to the last type 0 operation and is initially zero:

For every type 0 operation, let l=(l xor lastans)mod n + 1, r=(r xor lastans)mod n + 1, and then swap(l, r) if l>r.

For every type 1 operation, let x=x xor lastans.

Output

For each type 0 operation, please output the maximum xor sum in a single line.

Sample Input

1

3 3

0 1 2

0 1 1

1 3

0 3 4

Sample Output

1

3

题意

给一个长度为n的数组m个操作

  • 0 x y 查询区间\([x,y]\)取任意个数能异或出的最大值
  • 1 x 向数组尾部添加一个数x

强制在线

题解

朴素的线性基只能查询1-n能异或出的最大值,这题我们可以保存\([1,n]\)每个前缀线性基的状态,查询x,y时只需要查询第y个前缀的线性基就行

但是前缀里会有1-x的线性基影响结果,我们可以在插入线性基时做处理,如果在第pos位上已经有数,且这个数的插入时间比我当前数的插入时间早,那么就把当前要插入的数与该数交换,当前插入时间也交换,直至当前数无法插入或变为0

这样可以让前缀线性基里的数都是越新的,查询的时候判断线性基上数的插入时间是否大于等于x,如果大于x就可以使用这个数。这样处理的正确性是因为线性基插入不受顺序影响,同一组数以不同顺序插入,最后得到的线性基都是等价的

代码

#include <bits/stdc++.h>

const int mx = 1e6+5;
typedef long long ll; int sum[mx][32];
int pos[mx][32];
int tot; void add(int num) {
++tot;
for (int i = 0; i < 32; i++) {
sum[tot][i] = sum[tot-1][i];
pos[tot][i] = pos[tot-1][i];
} int now = tot;
for (int i = 30; i >= 0; i--) {
if (num & (1<<i)) {
if (sum[tot][i] == 0) {
sum[tot][i] = num;
pos[tot][i] = now;
break;
} if (now > pos[tot][i]) {
std::swap(now, pos[tot][i]);
std::swap(num, sum[tot][i]);
}
num ^= sum[tot][i];
}
}
} int query(int l, int r) {
int ans = 0;
for (int i = 30; i >= 0; i--) {
if (sum[r][i] && pos[r][i] >= l) {
ans = std::max(ans, ans ^ sum[r][i]);
}
}
return ans;
} int main() {
int T;
scanf("%d", &T); while (T--) {
int lastans = 0; tot = 0;
int n, m, num;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &num);
add(num);
} while (m--) {
int op, l, r;
scanf("%d", &op);
if (op == 0) {
scanf("%d%d", &l, &r);
l = (l ^ lastans) % n + 1;
r = (r ^ lastans) % n + 1;
if (l > r) std::swap(l, r);
lastans = query(l, r);
printf("%d\n", lastans);
} else {
scanf("%d", &r);
add(r ^ lastans);
n++;
}
}
}
return 0;
}

hdu-6579 Operation的更多相关文章

  1. hdu 6579 Operation (在线线性基)

    传送门 •题意 一个数组a有n个数 m个操作 操作① 询问$[l,r]$区间的异或值 操作② 在数组末尾追加一个数x,数组长度变为$n+1$ 其中$l,r$不直接给出,其中$l=l%n+1,r=r%n ...

  2. 杭电多校HDU 6579 Operation (线性基 区间最大)题解

    题意: 强制在线,求\(LR\)区间最大子集异或和 思路: 求线性基的时候,记录一个\(pos[i]\)表示某个\(d[i]\)是在某个位置更新进入的.如果插入时\(d[i]\)的\(pos[i]\) ...

  3. HDU 5063 Operation the Sequence(暴力)

    HDU 5063 Operation the Sequence 题目链接 把操作存下来.因为仅仅有50个操作,所以每次把操作逆回去执行一遍,就能求出在原来的数列中的位置.输出就可以 代码: #incl ...

  4. HDU 5063 Operation the Sequence(仔细审题)

    http://acm.hdu.edu.cn/showproblem.php?pid=5063 题目大意: 题目意思还是比较简单.所以就不多少了.注意这句话,对解题有帮助. Type4: Q i que ...

  5. hdu 5063 Operation the Sequence

    http://acm.hdu.edu.cn/showproblem.php?pid=5063 思路:因为3查询最多50,所以可以在查询的时候逆操作找到原来的位置,然后再求查询的值. #include ...

  6. hdu 5063 Operation the Sequence(Bestcoder Round #13)

    Operation the Sequence                                                                     Time Limi ...

  7. HDU 5063 Operation the Sequence(暴力 数学)

    题目链接:pid=5063" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=5063 Prob ...

  8. HDU - 5036 Operation the Sequence

    Problem Description You have an array consisting of n integers: a1=1,a2=2,a3=3,-,an=n. Then give you ...

  9. 计蒜客 A1607 UVALive 8512 [ACM-ICPC 2017 Asia Xi'an]XOR

    ICPC官网题面假的,要下载PDF,点了提交还找不到结果在哪看(我没找到),用VJ交还直接return 0;也能AC 计蒜客题面 这个好 Time limit 3000 ms OS Linux 题目来 ...

  10. CodeForces 1100F Ivan and Burgers

    CodeForces题面 Time limit 3000 ms Memory limit 262144 kB Source Codeforces Round #532 (Div. 2) Tags da ...

随机推荐

  1. 绿色版的mysql 下载安装配置方式

    解压下载好的压缩包 下载地址 mysql-5.6.26-win64 绿色版 copy 一份my-default.ini改名字为my.ini为mysql的配置文件 打开my.ini 修改配置文件 默认的 ...

  2. 在WPF中嵌入WebBrowser可视化页面

    无论是哪种C/S技术,涉及数据可视化就非常的累赘了,当然大神也一定有,只不过面向大多数人,还是通过网页来实现,有的时候不想把这两个功能分开,一般会是客户的原因,所以我们打算在WPF中嵌入WebBrow ...

  3. 【Java】You have an error in your SQL syntax ...

    详情如下: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server v ...

  4. 七分钟理解什么是 KMP 算法

    本文是介绍 什么是 BF算法.KMP算法.BM算法 三部曲之一. KMP算法 内部涉及到的数学原理与知识太多,本文只会对 KMP算法 的运行过程. 部分匹配表 .next数组 进行介绍,如果理解了这三 ...

  5. ubuntu 下常用的mysql 命令

    一.mysql服务操作  0.查看数据库版本 sql-> status;  1.net start mysql //启动mysql服务  2.net stop mysql //停止mysql服务 ...

  6. ubuntu .deb .tar.gz .tar.bz2 .rmp 和命令方式安装软件的方法

    今天在Ubuntu11.10中安装Google chrome浏览器是遇到了问题,下载好的".deb"格式的安装文件google-chrome-stable.deb双击后或者右键快捷 ...

  7. docker挂载volume的用户权限问题,理解docker容器的uid

    docker挂载volume的用户权限问题,理解docker容器的uid 在刚开始使用docker volume挂载数据卷的时候,经常出现没有权限的问题. 这里通过遇到的问题来理解docker容器用户 ...

  8. 1.Go语言copy函数、sort排序、双向链表、list操作和双向循环链表

    1.1.copy函数 通过copy函数可以把一个切片内容复制到另一个切片中 (1)把长切片拷贝到短切片中 package main import "fmt" func main() ...

  9. Java Lambda表达式forEach无法跳出循环的解决思路

    Java Lambda表达式forEach无法跳出循环的解决思路 如果你使用过forEach方法来遍历集合,你会发现在lambda表达式中的return并不会终止循环,这是由于lambda的底层实现导 ...

  10. .net开源生态,WTM与NCC

    天下大势,分久必合,合久必分.改朝换代都如花开花谢,过眼云烟,更别提开发语言的更迭了. 我们所坚持的,只是那最初的感动,那“只是在人群中多看了你一眼”的惊艳.三十年河东,三十年河西,不忘初心,方得始终 ...