题目链接

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. 原生应用使用cordova并与h5应用分离

    个人原创地址:https://www.jianshu.com/p/1ad536e76640 1.需求与使用场景   打开一个新页面,要求能够加载本地zip格式的h5应用,该应用使用了某些原生能力:能够 ...

  2. 【iOS】CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable

    CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SH ...

  3. 【iOS】stringWithFormat 保留小数点位数 float double

    以前就见过,如下: text = [NSString stringWithFormat:@"%.1f", percentageCompleted]; 但一直没在意.刚一时好奇,查了 ...

  4. Mac Android 配置环境变量

    进入终端,输入以下命令: cd ~ touch .bash_profile //没有该文件的话新建一个 vi .bash_profile //vim 形式打开 输入内容jdk变量配置内容: expor ...

  5. 调用ffmpeg视频压缩工具类

    package com.example.demo; import com.alibaba.fastjson.JSONObject;import com.aliyun.oss.ClientExcepti ...

  6. bug探索常识

    1.什么是Bug探索测试? 探索测试是将测试设计和测试执行整合在一起,形成的一种测试方法. 2.探索性测试怎么做? 需要认真思考和分析结果,并且在探索测试的过程中做记录. 3.探索性测试的好处? 可以 ...

  7. Django REST framework的使用简单介绍

    官方文档:https://www.django-rest-framework.org/ GitHub源码:https://github.com/encode/django-rest-framework ...

  8. python基础之循环与迭代器

    循环 python 循环语句有for循环和while循环. while循环while循环语法 while 判断条件: 语句 #while循环示例 i = 0 while i < 10: i += ...

  9. webpack4核心模块tapable源码解析

    _ 阅读目录 一:理解Sync类型的钩子 1. SyncHook.js 2. SyncBailHook.js 3. SyncWaterfallHook.js 4. SyncLoopHook.js 二: ...

  10. 【转载】C# 中的委托和事件(详解)

    <div class="postbody"> <div id="cnblogs_post_body" class="blogpost ...