2017 ACM-ICPC Asia Xi'an Problem A XOR(异或线性基 )

题目链接  2017西安赛区 Problem A

题意  给定一个数列,和$q$个询问,每个询问中我们可以在区间$[L, R]$中选出一些数。

   假设我们选出来的这个数列为$A[i_{1}]$, $A[i_{2}]$, ..., $A[i_{t}]$

   求$K$ $or$ $($$A[i_{1}]$ $xor$ $A[i_{2}]$ ... $xor$ $A[i_{t}]$$)$的最大值

首先常规操作,每次在线段树上求出区间$[L, R]$代表的线性基。

然后把这个线性基中所有$K$的二进制表示为$1$的位全部削成$0$。

这样得到了新的不超过$30$个数,把新的这些数单独用一个线性基表示。

最后答案就是新的这个线性基中选出某些数能异或出来的最大值或上$K$的结果。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b)	for (int i(a); i >= (b); --i)
#define ls		i << 1
#define	rs		i << 1 | 1
#define lson		i << 1, L, mid
#define rson		i << 1 | 1, mid + 1, R

const int N = 1e4 + 10;
const int all = (1 << 27) - 1;

int n, q, k;
int T;
int a[N], cnt;

struct lb{
	int d[30];
	void clear(){
		memset(d, 0, sizeof d);
		cnt = 0;
	}
	bool ins(int val){
		dec(i, 28, 0) if (val & (1 << i)){
			if (!d[i]){ d[i] = val; break; }
			val ^= d[i];
		}
		return val > 0;
	}
	int qmax(){
		int ret = 0;
		dec(i, 28, 0) if ((k | (ret ^ d[i])) > (k | ret)) ret ^= d[i];
		return k | ret;
	}
};

lb t[N << 3], c;

lb merge(const lb &n1, const lb &n2){
	lb ret = n1;
	dec(i, 28, 0) if (n2.d[i]) ret.ins(n2.d[i]);
	return ret;
}

void build(int i, int L, int R){
	if (L == R){
		t[i].ins(a[L]);
		return;
	}

	int mid = (L + R) >> 1;
	build(lson);
	build(rson);
	t[i] = merge(t[ls], t[rs]);
}

lb query(int i, int L, int R, int l, int r){
	if (L == l && R == r) return t[i];
	int mid = (L + R) >> 1;
	if (r <= mid) return query(lson, l, r);
	else if (l > mid) return query(rson, l, r);
	else return merge(query(lson, l, mid), query(rson, mid + 1, r));
}

int main(){

	scanf("%d", &T);
	while (T--){
		scanf("%d%d%d", &n, &q, &k);
		rep(i, 0, 4e4) t[i].clear();
		rep(i, 1, n) scanf("%d", a + i);
		build(1, 1, n);

		while (q--){
			int x, y;
			scanf("%d%d", &x, &y);
			lb now = query(1, 1, n, x, y);
			cnt = all ^ k;
			dec(i, 28, 0) now.d[i] &= cnt;
			c.clear();
			dec(i, 28, 0) if (now.d[i]) c.ins(now.d[i]);
			printf("%d\n", c.qmax() | k);
		}
	}

	return 0;
}

  

2017 ACM-ICPC Asia Xi'an Problem A XOR(异或线性基 )

原文地址:https://www.cnblogs.com/cxhscst2/p/8449971.html

时间: 2024-11-07 09:49:02

2017 ACM-ICPC Asia Xi'an Problem A XOR(异或线性基 )的相关文章

hdu6206 Apple 2017 ACM/ICPC Asia Regional Qingdao Online

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6206 题目: Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 530    Accepted Submission(s): 172 Problem Description Apple is Taotao's favouri

2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 1496    Accepted Submission(s): 723 Problem Description Kelukin is a businessman. Every day, he travels arou

2017 ACM/ICPC Asia Regional Shenyang Online

cable cable cable Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2084    Accepted Submission(s): 1348 Problem Description Connecting the display screen and signal sources which produce differen

2017 ACM/ICPC Asia Regional Shenyang Online 记录

这场比赛全程心态爆炸…… 开场脑子秀逗签到题WA了一发.之后0贡献. 前期状态全无 H题想复杂了,写了好久样例过不去. 然后这题还是队友过的…… 后期心态炸裂,A题后缀数组理解不深,无法特判k = 1时的情况. 然后也没有心思读题了,心静不下来. 比赛题目链接 Problem B $ans = k(n - k + 1)$ #include <bits/stdc++.h> using namespace std; typedef long long LL; LL n, k; int main()

2017 ACM/ICPC Asia Regional Qingdao Online 1011 A Cubic number and A Cubic Number

A Cubic number and A Cubic Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description A cubic number is the result of using a whole number in a mul

2017 ACM/ICPC Asia Regional Qingdao Online

The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 6778    Accepted Submission(s): 713 Problem Description Here you have a set of strings. A dominator is a string of the se

2017 ACM/ICPC Asia Regional Beijing Online

A Visiting Peking University 直接模拟 B Reverse Suffix Array unsolved C Matrix 预处理每列前缀和以及每个每个行区间的每列的最小值,枚举行的所有区间,求最长连续字段和. D Agent Communication unsolved E Territorial Dispute n=1,2时不行 n=3时必须三点共线 n>3一定可以,任意找四个点判断即可. 好像也可以跑凸包,不在凸包上的点单独分开,如果全部在凸包上,找两个不相邻的点

2017 ACM/ICPC Asia Regional Shenyang Online:number number number hdu 6198【矩阵快速幂】

Problem Description We define a sequence F: ? F0=0,F1=1;? Fn=Fn?1+Fn?2 (n≥2). Give you an integer k, if a positive number n can be expressed byn=Fa1+Fa2+...+Fak where 0≤a1≤a2≤?≤ak, this positive number is mjf?good. Otherwise, this positive number is 

HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting

Brute Force Sorting Time Limit: 1 Sec  Memory Limit: 128 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=6215 Description Beerus needs to sort an array of N integers. Algorithms are not Beerus's strength. Destruction is what he excels. He can destr