P11071 「QMSOI R1」 Distorted Fate Solution

Description

给定序列 a = ( a 1 , a 2 , ⋯   , a n ) a=(a_1,a_2,\cdots,a_n) a=(a1,a2,,an),有 m m m 个操作分两种:

  • modify ⁡ ( l , r , x ) \operatorname{modify}(l,r,x) modify(l,r,x):对每个 i ∈ [ l , r ] i \in [l,r] i[l,r] 执行 a i ← a i xor ⁡ x a_i \gets a_i \operatorname{xor} x aiaixorx.
  • query ⁡ ( l , r ) \operatorname{query}(l,r) query(l,r):求 ∑ i = l r ( a l or ⁡ a l + 1 or ⁡ ⋯ or ⁡ a i )   m o d   2 30 \sum\limits_{i=l}^r (a_l \operatorname{or} a_{l+1} \operatorname{or} \cdots \operatorname{or} a_i) \bmod 2^{30} i=lr(aloral+1ororai)mod230.

Limitations

1 ≤ n , m ≤ 2 × 1 0 5 1 \le n,m \le 2 \times 10^5 1n,m2×105
1 ≤ l ≤ r ≤ n 1 \le l \le r \le n 1lrn
0 ≤ a i , x ≤ 2 30 0 \le a_i,x \le 2^{30} 0ai,x230
3 s , 100 MB 3\text{s},\textcolor{red}{100\text{MB}} 3s,100MB

Solution

位运算,考虑拆位.
对每个位开一个线段树,于是 modify ⁡ \operatorname{modify} modify 就是区间异或 1 1 1.
容易发现对于某个位 k k k,前缀 or ⁡ \operatorname{or} or 中第 k k k 位为 1 1 1 的元素是一段后缀,二分左端点 m m m ,那么这一位的贡献是 2 k × ( r − m + 1 ) 2^k\times(r-m+1) 2k×(rm+1).
时间复杂度 O ( n log ⁡ n log ⁡ V ) O(n\log n \log V) O(nlognlogV),空间复杂度 O ( n log ⁡ n log ⁡ V ) O(n\log n\log V) O(nlognlogV),其中 V V V 为值域.
离线下来逐位处理即可去掉空间的 log ⁡ V \log V logV.

Code

3.20 KB , 2.05 s , 18.01 MB    (in   total,   C++20   with   O2) 3.20\text{KB},2.05\text{s},18.01\text{MB}\;\texttt{(in total, C++20 with O2)} 3.20KB,2.05s,18.01MB(in total, C++20 with O2)

// Problem: P11071 「QMSOI R1」 Distorted Fate
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P11071
// Memory Limit: 100 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

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

using ui32 = unsigned int;
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;
}

constexpr int mask = (1 << 30) - 1;
struct Node {
    int l, r;
    int size;
    bool rev;
};
using Tree = vector<Node>;
inline int ls(int u) { return 2 * u + 1; }
inline int rs(int u) { return 2 * u + 2; }

inline void pushup(Tree& tr, int u) {
    tr[u].size = tr[ls(u)].size + tr[rs(u)].size;
}

inline void rev(Tree& tr, int u) {
    tr[u].rev ^= 1;
    tr[u].size = tr[u].r - tr[u].l + 1 - tr[u].size;
}

inline void pushdown(Tree& tr, int u) {
    if (tr[u].rev) {
        rev(tr, ls(u)), rev(tr, rs(u));
        tr[u].rev = false;
    }
}

inline void build(Tree& tr, int u, int l, int r, int bit, const vector<int>& a) {
    tr[u].l = l;
    tr[u].r = r;
    tr[u].rev = false;
	if (l == r) {
		tr[u].size = a[l] >> bit & 1;
		return;
	}
	int mid = (l + r) >> 1;
	build(tr, ls(u), l, mid, bit, a);
	build(tr, rs(u), mid + 1, r, bit, a);
	pushup(tr, u);
}

inline void update(Tree& tr, int u, int l, int r) {
    if (l <= tr[u].l && tr[u].r <= r) return rev(tr, u);
	int mid = (tr[u].l + tr[u].r) >> 1;
	pushdown(tr, u);
	if (l <= mid) update(tr, ls(u), l, r);
	if (r > mid) update(tr, rs(u), l, r);
	pushup(tr, u);
}

inline int query(Tree& tr, int u, int l, int r) {
    if (l > r) return 0;
	if (l <= tr[u].l && tr[u].r <= r) return tr[u].size;
	int mid = (tr[u].l + tr[u].r) >> 1, res = 0;
	pushdown(tr, u);
	if (l <= mid) res += query(tr, ls(u), l, r);
	if (r > mid) res += query(tr, rs(u), l, r);
	return res;
}

inline int find(Tree& tr, int u, int k) {
    if (tr[u].l == tr[u].r) return tr[u].l;
	pushdown(tr, u);
	if (tr[ls(u)].size > k) return find(tr, ls(u), k);
	return find(tr, rs(u), k - tr[ls(u)].size);
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int n, m;
	scanf("%d %d", &n, &m);
	
	vector<int> a(n);
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);
	
	vector<array<int, 4>> queries(m);
	for (auto& [op, l, r, x] : queries) {
	    scanf("%d %d %d", &op, &l, &r);
	    l--, r--;
	    if (op == 1) scanf("%d", &x);
	} 
	
	vector<ui64> ans(m);
	Tree tr(n << 2);
	for (int lg = 0; lg < 30; lg++) {
		build(tr, 0, 0, n - 1, lg, a);
		for (int i = 0; i < m; i++) {
			auto& [op, l, r, x] = queries[i];
			if (op == 1) {
				if (x >> lg & 1) update(tr, 0, l, r);
			} else {
				int s = query(tr, 0, 0, l - 1), m = find(tr, 0, s);
				if (m > r || tr[0].size <= s) continue;
				ans[i] += 1ULL * (r - m + 1) * (1 << lg);
			}
		}
	}
	for (int i = 0; i < m; i++) 
	    if (queries[i][0] == 2) printf("%llu\n", ans[i] & mask);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值