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 个操作,分以下两种:
- query ( l , r ) \operatorname{query}(l,r) query(l,r):求 max ( 0 , max [ u , v ] ∈ [ l , r ] ∑ i = u v a i ) \max(0,\max\limits_{[u,v] \in [l,r]}\sum\limits_{i=u}^v a_i) max(0,[u,v]∈[l,r]maxi=u∑vai)。
- modify ( l , r , k ) \operatorname{modify}(l,r,k) modify(l,r,k):对于每个 i ∈ [ l , r ] i\in [l,r] i∈[l,r] 执行 a i ← a i or k a_i \leftarrow a_i \operatorname{or} k ai←aiork,其中 or \operatorname{or} or 为按位或。
Limitations
1
≤
n
,
m
≤
1
0
5
1 \le n, m \le 10^5
1≤n,m≤105
∣
a
i
∣
,
∣
k
∣
≤
2
30
|a_i|,|k| \le 2^{30}
∣ai∣,∣k∣≤230
1
≤
l
≤
r
≤
n
1 \le l \le r \le n
1≤l≤r≤n
1
s
,
128
MB
1\text{s},128\text{MB}
1s,128MB
Solution
modify
\operatorname{modify}
modify 操作无法打标记,考虑暴力修改,但直接暴力改会
TLE
\text{TLE}
TLE.
考虑对每个区间维护
and
\operatorname{and}
and 和,当发现区间
and
\operatorname{and}
and 和
or
\operatorname{or}
or 上
k
k
k 不变,那么继续修改显然不会改变
a
i
a_i
ai,可以不向该区间递归.
剩下的就是 P4513 了.
时间复杂度
O
(
n
log
n
log
V
)
O(n\log n\log V)
O(nlognlogV).
Code
3.17
KB
,
5.87
s
,
10.4
MB
(
in
total,
C++20
with
O2
)
3.17\text{KB},5.87\text{s},10.4\text{MB} \; (\texttt{in total, C++20 with O2})
3.17KB,5.87s,10.4MB(in total, C++20 with O2)
重构过了.
// Problem: P7492 [传智杯 #3 决赛] 序列
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P7492
// Memory Limit: 128 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#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 seg_tree {
struct Node {
int l, r;
i64 lmax, rmax, tsum, sum, andd;
};
inline void merge(Node& res, const Node& le, const Node& ri) {
res.lmax = max(le.lmax, le.sum + ri.lmax);
res.rmax = max(le.rmax + ri.sum, ri.rmax);
res.tsum = max({le.tsum, ri.tsum, le.rmax + ri.lmax});
res.sum = le.sum + ri.sum;
res.andd = le.andd & ri.andd;
}
inline int ls(int u) { return 2 * u + 1; }
inline int rs(int u) { return 2 * u + 2; }
struct SegTree {
vector<Node> tr;
inline SegTree() {}
inline SegTree(const vector<i64>& a) {
const int n = a.size();
tr.resize(n << 1);
build(0, 0, n - 1, a);
}
inline void pushup(int u, int mid) {
merge(tr[u], tr[ls(mid)], tr[rs(mid)]);
}
inline void build(int u, int l, int r, const vector<i64>& a) {
tr[u].l = l;
tr[u].r = r;
if (l == r) {
tr[u].sum = tr[u].andd = a[l];
tr[u].lmax = tr[u].rmax = tr[u].tsum = max(a[l], 0LL);
return;
}
const int mid = (l + r) >> 1;
build(ls(mid), l, mid, a);
build(rs(mid), mid + 1, r, a);
pushup(u, mid);
}
inline void update(int u, int l, int r, i64 val) {
if (tr[u].l == tr[u].r) {
tr[u].sum |= val;
tr[u].andd = tr[u].sum;
tr[u].lmax = tr[u].rmax = tr[u].tsum = max(tr[u].sum, 0LL);
return;
}
const int mid = (tr[u].l + tr[u].r) >> 1;
if (l <= mid && (tr[ls(mid)].andd | val) != tr[ls(mid)].andd) update(ls(mid), l, r, val);
if (r > mid && (tr[rs(mid)].andd | val) != tr[rs(mid)].andd) update(rs(mid), l, r, val);
pushup(u, mid);
}
inline Node query(int u, int l, int r) {
if (l <= tr[u].l && tr[u].r <= r) {
return tr[u];
}
const int mid = (tr[u].l + tr[u].r) >> 1;
if (r <= mid) return query(ls(mid), l, r);
if (l > mid) return query(rs(mid), l, r);
Node le = query(ls(mid), l, r), ri = query(rs(mid), l, r), res;
merge(res, le, ri);
return res;
}
inline void range_or(int l, int r, i64 x) { update(0, l, r, x); }
inline i64 range_gss(int l, int r) { return query(0, l, r).tsum; }
};
}
using seg_tree::SegTree;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m; scanf("%d %d", &n, &m);
vector<i64> a(n);
for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
SegTree sgt(a);
for (int i = 0, op, l, r, v; i < m; i++) {
scanf("%d %d %d", &op, &l, &r), l--, r--;
if (op == 1) printf("%lld\n", sgt.range_gss(l, r));
else {
scanf("%d", &v);
sgt.range_or(l, r, v);
}
}
return 0;
}