链接:https://nanti.jisuanke.com/t/A1607

题面:

 

Consider an array AA with n elements . Each of its element is A[i]A[i] (1 \le i \le n)(1≤i≤n) . Then gives two integers QQ, KK, and QQ queries follow . Each query , give you LL, RR, you can get ZZ by the following rules.

To get ZZ , at first you need to choose some elements from A[L]A[L] to A[R]A[R] ,we call them A[i_1],A[i_2]…A[i_t]A[i1​],A[i2​]…A[it​] , Then you can get number Z = KZ=K or (A[i_1]A[i1​] xor A[i_2]A[i2​] … xor A[i_t]A[it​]) .

Please calculate the maximum ZZ for each query .

Input

Several test cases .

First line an integer TT (1 \le T \le 10)(1≤T≤10) . Indicates the number of test cases.Then TT test cases follows . Each test case begins with three integer NN, QQ, KK (1 \le N \le 10000,\ 1 \le Q \le 100000 , \ 0 \le K \le 100000)(1≤N≤10000, 1≤Q≤100000, 0≤K≤100000) . The next line has NN integers indicate A[1]A[1] to A[N]A[N] (0 \le A[i] \le 10^8)(0≤A[i]≤108). Then QQ lines , each line two integer LL, RR (1 \le L \le R \le N)(1≤L≤R≤N) .

Output

For each query , print the answer in a single line.

样例输入复制

1
5 3 0
1 2 3 4 5
1 3
2 4
3 5

样例输出复制

3
7
7

题目来源

ACM-ICPC 2017 Asia Xi'an

跟树套树差不多,线段树每个节点建个线性基,写个合并函数,对k取反,每个数对取反后的k取且,就可以转化成线性基中取与k异或最大值了

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 1e4+;
int a[M];
struct L_B{
int b[],nb[],tot;
void init(){
memset(b,,sizeof(b));
} bool Insert(int x){
for(int i = ;i >= ;i --){
if(x&(<<i)){
if(!b[i]){
b[i] = x;
break;
}
x ^= b[i];
}
}
return x > ;
} int Max(int x){
int ret = x;
for(int i = ;i >= ;i --)
ret = max(ret,ret^b[i]);
return ret;
} int Min(int x){
int ret = x;
for(int i = ;i <= ;i ++)
if(b[i]) ret ^= b[i];
return ret;
} void rebuild(){
for(int i = ;i >= ;i --)
for(int j = i-;j >= ;j --)
if(b[i]&(<<j)) b[i]^=b[j];
for(int i = ;i <= ;i ++)
if(b[i]) nb[tot++] = b[i];
} int K_Min(int k){
int res = ;
if(k >= (<<tot))
return -;
for(int i = ;i >= ;i --)
if(k&(<<i))
res ^= nb[i];
return res;
} L_B merge(L_B v){
L_B ret;
for(int i = ;i <= ;i ++) ret.b[i] = b[i];
for(int i = ;i <= ;i ++){
for(int j = i;j >= ;j --){
if(v.b[i]&(<<j)){
if(ret.b[j]) v.b[i] ^= ret.b[j];
else {
ret.b[j] = v.b[i]; break;
}
}
}
}
return ret;
}
}t[M<<]; void build(int l,int r,int rt){
if(l == r){
t[rt].init();
t[rt].Insert(a[l]);
return ;
}
mid;
build(lson); build(rson);
t[rt] = t[rt<<].merge(t[rt<<|]);
} L_B query(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
return t[rt];
}
mid;
if(L <= m&&m < R) return query(L,R,lson).merge(query(L,R,rson));
if(L <= m) return query(L,R,lson);
if(R > m) return query(L,R,rson);
} int main()
{
int T,n,q,k,l,r;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&q,&k);
k = ~k;
for(int i = ;i <= n;i ++)
scanf("%d",&a[i]),a[i]&=k;
k = ~k;
build(,n,);
for(int i = ;i <= q;i ++){
scanf("%d%d",&l,&r);
L_B ans = query(l,r,,n,);
int an = ans.Max(k);
printf("%d\n",an);
}
}
return ;
}

2017 ICPC西安区域赛 A - XOR (线段树并线性基)的更多相关文章

  1. UVALive 8519 Arrangement for Contests 2017西安区域赛H 贪心+线段树优化

    题意 等价于给一个数列,每次对一个长度为$K$的连续区间减一 为最多操作多少次 题解: 看样例猜的贪心,10分钟敲了个线段树就交了... 从1开始,找$[i,i+K]$区间的最小值,然后区间减去最小值 ...

  2. 2017西安区域赛A / UVALive - 8512 线段树维护线性基合并

    题意:给定\(a[1...n]\),\(Q\)次询问求\(A[L...R]\)的异或组合再或上\(K\)的最大值 本题是2017的西安区域赛A题,了解线性基之后你会发现这根本就是套路题.. 只要用线段 ...

  3. $CF938G\ Shortest\ Path\ Queries$ 线段树分治+线性基

    正解:线段树分治+线性基 解题报告: 传送门$QwQ$ 考虑如果只有操作3,就这题嘛$QwQ$ 欧克然后现在考虑加上了操作一操作二 于是就线段树分治鸭 首先线段树叶子节点是询问嘛这个不用说$QwQ$. ...

  4. BZOJ.4184.shallot(线段树分治 线性基)

    BZOJ 裸的线段树分治+线性基,就是跑的巨慢_(:з」∠)_ . 不知道他们都写的什么=-= //41652kb 11920ms #include <map> #include < ...

  5. bzoj 4184: shallot (线段树维护线性基)

    题面 \(solution:\) 这一题绝对算的上是一道经典的例题,它向我们诠释了一种新的线段树维护方式(神犇可以跳过了).像这一类需要加入又需要维护删除的问题,我们曾经是遇到过的像莫对,线段树... ...

  6. 【2017西安邀请赛:A】XOR(线段树+线性基)

    前言:虽然已经有很多题解了,但是还是想按自己的理解写一篇. 思路:首先分析题目 一.区间操作 —— 线段树 二.异或操作 —— 线性基 这个两个不难想,关键是下一步的技巧 “或”运算 就是两个数的二进 ...

  7. UVAlive7141 BombX 14年上海区域赛D题 线段树+离散化

    题意:一个无限大的棋盘, 有n个小兵, 给出了n个兵的坐标, 现在有一个长为width 高为height的炸弹放在棋盘上, 炸弹只能上下左右平移, 不能旋转. 且放炸弹的区域不能含有士兵, 炸弹可以一 ...

  8. 【luogu3733】【HAOI2017】 八纵八横 (线段树分治+线性基)

    Descroption 原题链接 给你一个\(n\)个点的图,有重边有自环保证连通,最开始有\(m\)条固定的边,要求你支持加边删边改边(均不涉及最初的\(m\)条边),每一次操作都求出图中经过\(1 ...

  9. 【线段树分治 线性基】luoguP3733 [HAOI2017]八纵八横

    不知道为什么bzoj没有HAOI2017 题目描述 Anihc国有n个城市,这n个城市从1~n编号,1号城市为首都.城市间初始时有m条高速公路,每条高速公路都有一个非负整数的经济影响因子,每条高速公路 ...

随机推荐

  1. Mybatis 传入多个参数查询数据 (3种方法)

    第一种方案 DAO层的函数方法 public User selectUser(String name,String area); 对应的Mapper.xml <select id="s ...

  2. U盘exFAT格式转NTFS

    先格式化成FAT或者FAT32(这个简单,右键格式化就成),然后点开始,运行,输入cmd,在里面输入: convert I:/fs:ntfs I是你U盘的字母(大写),完成

  3. spring-boot学习 (Groovy与Spring Boot Cli)

    一.使用idea创建一个spring-boot项目,选择groovy语言 二.编写相应代码 1.创建实例类 package com.zhi.example class Man { Long id St ...

  4. Qt pro工程文件介绍

    app - 建立一个应用程序的makefile.这是默认值,所以如果模板没有被指定,这个将被使用. lib - 建立一个库的makefile. vcapp - 建立一个应用程序的Visual Stud ...

  5. Appium Desktop 元素定位和脚本录制功能

    Appium Desktop除了可以做Server之外还可以进行元素定位和脚本录制功能,点击放大镜按钮,进入页面设置.开始配置Desired Capabilities. 配置Desired Capab ...

  6. Android Popwindow使用总结

    Android Popwindow使用总结 转 https://www.jianshu.com/p/3812ff5ef272 1.基本使用方法 View view = getLayoutInflate ...

  7. OS X以及iOS中与硬件环境相关的预定义宏

    由于现在ARM处理器的飞速发展,从Apple A4到现在的Apple A7,从32位到64位,每一代处理器几乎都增加了不少特性,从而在架构上也有所不同.比如Apple A6引入了ARMv7S架构,增加 ...

  8. Build Telemetry for Distributed Services之OpenTracing指导:C#

    官网链接:https://opentracing.io/guides/ 官方微博:https://medium.com/opentracing Welcome to the OpenTracing G ...

  9. 【leetcode_easy】541. Reverse String II

    problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...

  10. 【手记】解决Intel Management Engine Interface黄色感叹号

    安装补丁KB2685811.重启.