关闭

CodeForces 587E Duff as a Queen (线段树+线性基)

472人阅读 评论(0) 收藏 举报
分类:
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <deque>
#include <map>
#include <bitset>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;

#define LL long long
#define ULL unsigned long long
#define eps 1e-9
#define N 200020
#define M 100010
#define pii pair<int,int>
#define MP make_pair
#define inf 0x3f3f3f3f
#define md (ll+rr>>1)
#define lson ll, md, ls
#define rson md + 1, rr, rs
#define ls (i << 1)
#define rs (ls | 1)
#define mod 1000000007
#define Pi acos(-1.0)


struct node {
	int a[30];
	void clear() {
		memset(a, 0, sizeof a);
	}
	int count() {
		int ret = 0;
		for(int i = 0; i <= 29; ++i) ret += a[i] > 0;
		return ret;
	}
	void add(int x) {
		for(int i = 0; i <= 29; ++i) {
			if(x >> i & 1) {
				if(a[i]) x ^= a[i];
				else {
					a[i] = x;
					break;
				}
			}
		}
	}
};


node s[N << 2];
int add[N << 2], val[N << 2], b[N << 2];
int n, q;

void push_up(int i) {
	s[i].clear();
	for(int j = 0; j <= 29; ++j) s[i].add(s[ls].a[j]);
	for(int j = 0; j <= 29; ++j) s[i].add(s[rs].a[j]);
}
void build(int ll, int rr, int i) {
	if(ll == rr) {
		scanf("%d", &val[ll]);
		s[i].clear();
		b[i] = val[ll] ^ val[ll - 1];
		s[i].add(b[i]);
		return;
	}
	build(lson);
	build(rson);
	push_up(i);
}

void update1(int x, int v, int ll, int rr, int i) {
	if(ll == rr) {
		s[i].clear();
		b[i] ^= v;
		s[i].add(b[i]);
		return;
	}
	if(x <= md) update1(x, v, lson);
	else update1(x, v, rson);
	push_up(i);
}

void update2(int l, int r, int v, int ll, int rr, int i) {
	if(ll == l && rr == r) {
		add[i] ^= v;
		return;
	}
	if(r <= md) update2(l, r, v, lson);
	else if(l > md) update2(l, r, v, rson);
	else {
		update2(l, md, v, lson);
		update2(md + 1, r, v, rson);
	}
}

node query1(int l, int r, int ll, int rr, int i) {
	if(ll == l && rr == r) return s[i];
	if(r <= md) return query1(l, r, lson);
	if(l > md) return query1(l, r, rson);
	node x = query1(l, md, lson);
	node y = query1(md + 1, r, rson);
	for(int j = 0; j <= 29; ++j) x.add(y.a[j]);
	return x;
}
int query2(int x, int ll, int rr, int i) {
	int ret = add[i];
	if(ll == rr) {
		return val[ll] ^ ret;
	}
	if(x <= md) ret ^= query2(x, lson);
	else ret ^= query2(x, rson);
	return ret;
}


		


int main() {
	scanf("%d%d", &n, &q);
	build(1, n, 1);

	while(q--) {
		int op, l, r, v;
		scanf("%d%d%d", &op, &l, &r);
		if(op == 1) scanf("%d", &v);

		if(op == 1) {
			update1(l, v, 1, n, 1);
			if(r != n) update1(r + 1, v, 1, n, 1);
			update2(l, r, v, 1, n, 1);
		}
		else {
			node ans;
			ans.clear();
			if(l != r) ans = query1(l + 1, r, 1, n, 1);
			int x = query2(l, 1, n, 1);
			ans.add(x);
			printf("%d\n", 1 << ans.count());
		}
	}
	return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:89166次
    • 积分:3079
    • 等级:
    • 排名:第11683名
    • 原创:228篇
    • 转载:1篇
    • 译文:0篇
    • 评论:24条
    最新评论