hdu-6579 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的更多相关文章
- hdu 6579 Operation (在线线性基)
传送门 •题意 一个数组a有n个数 m个操作 操作① 询问$[l,r]$区间的异或值 操作② 在数组末尾追加一个数x,数组长度变为$n+1$ 其中$l,r$不直接给出,其中$l=l%n+1,r=r%n ...
- 杭电多校HDU 6579 Operation (线性基 区间最大)题解
题意: 强制在线,求\(LR\)区间最大子集异或和 思路: 求线性基的时候,记录一个\(pos[i]\)表示某个\(d[i]\)是在某个位置更新进入的.如果插入时\(d[i]\)的\(pos[i]\) ...
- HDU 5063 Operation the Sequence(暴力)
HDU 5063 Operation the Sequence 题目链接 把操作存下来.因为仅仅有50个操作,所以每次把操作逆回去执行一遍,就能求出在原来的数列中的位置.输出就可以 代码: #incl ...
- HDU 5063 Operation the Sequence(仔细审题)
http://acm.hdu.edu.cn/showproblem.php?pid=5063 题目大意: 题目意思还是比较简单.所以就不多少了.注意这句话,对解题有帮助. Type4: Q i que ...
- hdu 5063 Operation the Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=5063 思路:因为3查询最多50,所以可以在查询的时候逆操作找到原来的位置,然后再求查询的值. #include ...
- hdu 5063 Operation the Sequence(Bestcoder Round #13)
Operation the Sequence Time Limi ...
- HDU 5063 Operation the Sequence(暴力 数学)
题目链接:pid=5063" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=5063 Prob ...
- 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 ...
- 计蒜客 A1607 UVALive 8512 [ACM-ICPC 2017 Asia Xi'an]XOR
ICPC官网题面假的,要下载PDF,点了提交还找不到结果在哪看(我没找到),用VJ交还直接return 0;也能AC 计蒜客题面 这个好 Time limit 3000 ms OS Linux 题目来 ...
- CodeForces 1100F Ivan and Burgers
CodeForces题面 Time limit 3000 ms Memory limit 262144 kB Source Codeforces Round #532 (Div. 2) Tags da ...
随机推荐
- python传递参数
1.脚本 # -*- coding: utf-8 -*- from sys import argvscript, first,second = argv #将命令中输入的参数解包后传递给左边 age ...
- ubuntu kylin的桌面崩溃问题
前几天安了ubuntu kylin,主题还是挺好看的,汉化也很好,就是各种报桌面错误,忍了,结果今天直接进不去桌面了 开机,输入密码,登录,然后桌面死活不显示,还弹出了错误提示我系统有问题,建议重启 ...
- 虚IP解决程序连只读服务器故障漂移
目前公司有一套核心交易数据库配置了AlWaysON,SQL 2012版本, 1主4从, 其从库(8,14, 8.15) 这2台只读的从数据库服务器, 后台程序和wms等很多程序,都是直接配置IP连接这 ...
- spring-boot-plus集成Spring Boot Admin管理和监控应用
Spring Boot Admin Spring Boot Admin用来管理和监控Spring Boot应用程序 应用程序向我们的Spring Boot Admin Client注册(通过HTTP) ...
- SpringBoot Jar包瘦身 - 跟大文件说再见!
前言 SpringBoot部署起来配置非常少,如果服务器部署在公司内网,上传速度还行,但是如果部署在公网(阿里云等云服务器上),部署起来实在头疼.就是 编译出来的 Jar 包很大,如果工程引入了许多开 ...
- Spring 源码学习(一)-容器的基础结构
关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料 展示的代码摘取了一些核心方法,去掉一些默认设置和日志输出,还有大多数错误异常也去掉了,小伙伴想看详细代码 ...
- JS中以一个方法作为参数的写法
一:以方法作为参数 这下来说直接以一个方法来作为参数的写法,直接上代码: -----------这样调用的方法------------- go(function(){ alert("succ ...
- 前端笔记之微信小程序(三)GET请求案例&文件上传和相册API&配置https
一.信息流小程序-GET请求案例 1.1服务端接口开发 一定要养成接口的意识,前端单打独斗出不来任何效果,必须有接口配合,写一个带有分页.关键词查询的接口: 分页接口:http://127.0.0.1 ...
- 02.Mybatis的动态代理方式实现增删改查
动态代理的方式实现增删改查: 通过约定的方式定位sql语句 约定 > 配置文件 > 硬编码 约定的目标是省略掉通过硬编码的方式定位sql的代码,通过接口直接定位出sql语句,以下代码为通过 ...
- 如何成为PHP程序员?
当今,互联网的蓬勃发展,移动互联网的火热,以及国家提出的“互联网+”.这些趋势可以让我们明显的感觉到互联网的重要,不可替代.网站也是大家最早接触,最早认识的一种新事物.谈到网站,无非最长脸的莫过于PH ...