Description
给定长为 n n n 的序列 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 , k ) \operatorname{modify}(l,r,k) modify(l,r,k):对于所有满足 i ∈ [ l , r ] , a i ≠ k i \in [l,r],\; a_i \neq k i∈[l,r],ai=k 的 i i i,执行 a i ← a i − k a_i \leftarrow a_i -k ai←ai−k
- query ( l , r , k ) \operatorname{query}(l,r,k) query(l,r,k):求 ( ∑ i = l r a i ) m o d 2 32 (\sum\limits_{i=l}^r a_i) \bmod 2^{32} (i=l∑rai)mod232
Limitations
1
≤
n
,
m
≤
5
×
1
0
5
1 \le n,m \le 5\times 10^5
1≤n,m≤5×105
1
≤
l
≤
r
≤
n
1 \le l \le r \le n
1≤l≤r≤n
0
≤
a
i
,
k
≤
1
0
9
0 \le a_i,k \le 10^9
0≤ai,k≤109
6
s
,
512
MB
6\text{s},512\text{MB}
6s,512MB
Solution
考虑上线段树,但
modify
\operatorname{modify}
modify 很棘手,先分讨一下。
下面设线段树当前节点的最小值为
u
u
u。
- u > k u > k u>k,直接打标记即可。
-
u
<
k
u < k
u<k,遍历所有
u
≤
k
u \le k
u≤k 的儿子进行修改,由于每个数至多只会修改
一次,所以这部分耗费 O ( log n ) \mathcal{O}(\log n) O(logn) 时间(单次操作)。 - u = k u = k u=k,最难搞的情况,考虑维护次小值 v v v。
- v > 2 k v > 2k v>2k,次小值无法变成最小值,打上最小值以外的数减去 k k k 的标记即可。
- v ≤ 2 k v \le 2k v≤2k,次小值会变成最小值,暴力遍历儿子进行更新,由于 v − k ≤ v 2 v-k \le \frac{v}{2} v−k≤2v,所以 v v v 每次至少减半,这部分耗费 O ( log n log V ) \mathcal{O}(\log n \log V) O(lognlogV) 时间。( V V V 是值域)
综上述,时间复杂度 O ( n log n log V ) \mathcal{O}(n\log n \log V) O(nlognlogV)。
Code
4.41 KB , 18.88 s , 109.38 MB (in total, C++ 20 with O2) 4.41\text{KB},18.88\text{s},109.38\text{MB}\; \texttt{(in total, C++ 20 with O2)} 4.41KB,18.88s,109.38MB(in total, C++ 20 with O2)
// Problem: P9069 [Ynoi Easy Round 2022] 堕天作战 TEST_98
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P9069
// Memory Limit: 512 MB
// Time Limit: 6000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#pragma struct pack(1)
#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;
}
const ui64 inf = ULLONG_MAX;
struct Node {
int l, r, cnt;
ui64 sum, min, sec, tag, mark;
};
using Tree = vector<Node>;
int ls(int u) { return 2 * u + 1; }
int rs(int u) { return 2 * u + 2; }
inline void pushup(Tree& tr, int u) {
tr[u].sum = tr[ls(u)].sum + tr[rs(u)].sum;
if (tr[ls(u)].min == tr[rs(u)].min) {
tr[u].min = tr[ls(u)].min;
tr[u].cnt = tr[ls(u)].cnt + tr[rs(u)].cnt;
tr[u].sec = min(tr[ls(u)].sec, tr[rs(u)].sec);
}
else if (tr[ls(u)].min < tr[rs(u)].min) {
tr[u].min = tr[ls(u)].min;
tr[u].cnt = tr[ls(u)].cnt;
tr[u].sec = min(tr[ls(u)].sec, tr[rs(u)].min);
}
else {
tr[u].min = tr[rs(u)].min;
tr[u].cnt = tr[rs(u)].cnt;
tr[u].sec = min(tr[ls(u)].min, tr[rs(u)].sec);
}
}
inline void addT(Tree& tr, int u, ui64 tag) {
int len = tr[u].r - tr[u].l + 1;
tr[u].sum -= tag * len;
tr[u].min -= tag;
tr[u].sec -= tag;
tr[u].tag += tag;
}
inline void addM(Tree& tr, int u, ui64 tag) {
int len = tr[u].r - tr[u].l + 1;
tr[u].sum -= tag * (len - tr[u].cnt);
tr[u].sec -= tag;
tr[u].mark += tag;
}
inline void pushdown(Tree& tr, int u) {
if (tr[u].tag) {
addT(tr, ls(u), tr[u].tag);
addT(tr, rs(u), tr[u].tag);
tr[u].tag = 0;
}
if (tr[u].mark) {
if (tr[ls(u)].min == tr[u].min) {
addM(tr, ls(u), tr[u].mark);
}
else {
addT(tr, ls(u), tr[u].mark);
}
if (tr[rs(u)].min == tr[u].min) {
addM(tr, rs(u), tr[u].mark);
}
else {
addT(tr, rs(u), tr[u].mark);
}
tr[u].mark = 0;
}
}
inline void build(Tree& tr, int u, int l, int r, vector<int>& a) {
tr[u].l = l;
tr[u].r = r;
if (l == r) {
tr[u].sum = tr[u].min = a[l];
tr[u].cnt = 1;
tr[u].sec = inf;
return;
}
int mid = (l + r) >> 1;
build(tr, ls(u), l, mid, a);
build(tr, rs(u), mid + 1, r, a);
pushup(tr, u);
}
inline void update(Tree& tr, int u, int val) {
int len = tr[u].r - tr[u].l + 1;
if (tr[u].min == val && len == tr[u].cnt) {
return;
}
if (tr[u].l == tr[u].r) {
tr[u].sum -= val;
tr[u].min = tr[u].sum;
return;
}
if (tr[u].min < val || tr[u].sec <= val * 2) {
pushdown(tr, u);
update(tr, ls(u), val);
update(tr, rs(u), val);
pushup(tr, u);
return;
}
if (tr[u].min == val) {
addM(tr, u, val);
}
else {
addT(tr, u, val);
}
}
inline void modify(Tree& tr, int u, int l, int r, int val) {
if (l <= tr[u].l && tr[u].r <= r) {
update(tr, u, val);
return;
}
int mid = (tr[u].l + tr[u].r) >> 1;
pushdown(tr, u);
if (l <= mid) {
modify(tr, ls(u), l, r, val);
}
if (r > mid) {
modify(tr, rs(u), l, r, val);
}
pushup(tr, u);
}
inline ui64 query(Tree& tr, int u, int l, int r) {
if (l <= tr[u].l && tr[u].r <= r) {
return tr[u].sum;
}
int mid = (tr[u].l + tr[u].r) >> 1;
ui64 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;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
Tree tree(n << 2);
build(tree, 0, 0, n - 1, a);
ui64 lst = 0;
for (int i = 0; i < m; i++) {
int op, l, r, x;
cin >> op >> l >> r;
l ^= lst, r ^= lst;
l--, r--;
if (op == 1) {
cin >> x;
x ^= lst;
if (x != 0) {
modify(tree, 0, l, r, x);
}
}
else {
cout << (lst = query(tree, 0, l, r)) << endl;
lst &= 1048575;
}
}
return 0;
}