P5670 秘籍-反复异或 Solution

Description

给定序列 a=(a1,a2,⋯ ,an)a=(a_1,a_2,\cdots,a_n)a=(a1,a2,,an) 和常数 mmm,有 qqq 个操作分两种:

  • add⁡(l,r,x)\operatorname{add}(l,r,x)add(l,r,x):对每个 i∈[l,r]i\in[l,r]i[l,r] 执行 ai←ai+xa_i\gets a_i+xaiai+x.
  • query⁡(l,r)\operatorname{query}(l,r)query(l,r):求 (xor⁡i=lrai) mod 2m(\mathop{\operatorname{xor}}\limits_{i=l}^ra_i)\bmod 2^m(i=lxorrai)mod2m.

Limitations

1≤n,q≤1051 \le n,q \le 10^51n,q105
1≤m≤10\textcolor{red}{1\le m \le 10}1m10
0≤ai,x<2m0\le a_i,x<2^m0ai,x<2m
0.5s,256MB\textcolor{red}{0.5\text{s}},256\text{MB}0.5s,256MB

Solution

mmm 很小,考虑基于值域的做法.
由于异或性质,我们只用维护一个数出现次数奇偶性,这可以压在一个 102410241024 位的 bitset fff 里,第 iii 位表示 iii 的出现次数奇偶性.
那么合并就是将两个 bitset xor⁡\operatorname{xor}xor 起来.
区间加 xxx 就是将 fff 循环左移 xxx 次,可以写作:

 f = (f >> (1024 - x)) | (f << x);

显然可用线段树维护,记得卡常,用 fastio.

Code

4.63KB,2.79s,55.88MB  (in total, C++20 with O2)4.63\text{KB},2.79\text{s},55.88\text{MB}\;\texttt{(in total, C++20 with O2)}4.63KB,2.79s,55.88MB(in total, C++20 with O2)
fastiolazy_segment 删了.

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;

template<class T>
bool chmax(T &a, const T &b){
	if(a < b){ a = b; return true; }
	return false;
}

template<class T>
bool chmin(T &a, const T &b){
	if(a > b){ a = b; return true; }
	return false;
}

namespace fastio {}   // Removed

using fastio::read;
using fastio::write;

template<class Info, class Tag>
struct lazy_segment{};   // Removed

constexpr int B = 1024, mask = 1023;

struct Tag {
	int tag;
	inline Tag() : tag(0) {}
	inline Tag(int _tag) : tag(_tag) {}
	inline void apply(const Tag& t) { tag += t.tag; tag &= mask; }
};

struct Info {
	bitset<B> bs;
	inline Info() {}
	inline Info(int x) { bs.reset(); bs[x] = 1; }
	inline void apply(const Tag& t) {
		bs = (bs >> (B - t.tag)) | (bs << t.tag);
	}
};

inline Info operator+(const Info& lhs, const Info& rhs) {
	Info res;
	res.bs = lhs.bs ^ rhs.bs;
	return res;
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	const int n = read<int>(), m = read<int>(), q = read<int>();
	vector<int> a(n);
	for (int i = 0; i < n; i++) a[i] = read<int>();
	
	lazy_segment<Info, Tag> sgt(a);
	for (int i = 0, op, l, r; i < q; i++) {
		op = read<int>(), l = read<int>(), r = read<int>();
		l--, r--;
		if (op == 1) sgt.apply(l, r, read<int>());
		else {
			auto bs = sgt.query(l, r).bs;
			int ans = 0;
			for (int i = 0; i < B; i++) ans ^= bs[i] * i;
			write(ans & ((1 << m) - 1));
			putchar_unlocked('\n');
		}
	}
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值