stegatxins0 @ 2024-04-27 00:55:16
真奇怪,能在跟这题一模一样的 SP1716 通过,可是在这题只拿27分,剩下WA,而且都是出错在 line 1760 column 2, line 2344 column 3等较后面的数据。请问到底是那里出错呢?谢谢
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct sum_t {
ll sum,val,lv,rv,lazy;
static const long long null_l = 0;
sum_t(): sum(0), val(0), lv(0), rv(0), lazy(null_l) {}
sum_t(ll v): sum(v), val(v), lv(v), rv(v), lazy(null_l) {};
sum_t(ll v, ll w): sum(v), val(v), lv(v), rv(v), lazy(w) {};
sum_t op(sum_t& other) {
sum_t res;
res.sum = sum + other.sum;
res.val = max({val, other.val, rv + other.lv});
res.lv = max(lv, sum + other.lv);
res.rv = max(other.rv, other.sum + rv);
return res;
}
void lazy_app(int size) {
if(lazy){
val = sum = lv = rv = lazy;
}
}
ll lazy_comb(sum_t& v) {
return v.lazy;
}
};
template <typename num_t>
struct segtree {
int n, depth;
vector<num_t> tree;
void init(int s, long long* arr) {
n = s;
tree = vector<num_t>(4 * s);
init(0, 0, n - 1, arr);
}
num_t init(int i, int l, int r, long long* arr) {
if (l == r){
return tree[i] = num_t(arr[l]);
}
int mid = (l + r) / 2;
num_t a = init(2 * i + 1, l, mid, arr),
b = init(2 * i + 2, mid + 1, r, arr);
tree[i] = a.op(b);
return tree[i];
}
void update(int l, int r, num_t v) {
if (l > r) return;
update(0, 0, n - 1, l, r, v);
}
num_t update(int i, int tl, int tr, int ql, int qr, num_t v) {
eval_lazy(i, tl, tr);
if (tr < ql || qr < tl) return tree[i];
if (ql <= tl && tr <= qr) {
tree[i].lazy = tree[i].lazy_comb(v);
eval_lazy(i, tl, tr);
return tree[i];
}
int mid = (tl + tr) / 2;
num_t a = update(2 * i + 1, tl, mid, ql, qr, v),
b = update(2 * i + 2, mid + 1, tr, ql, qr, v);
return tree[i] = a.op(b);
}
num_t query(int l, int r) {
assert(r >= l);
return query(0, 0, n-1, l, r);
}
num_t query(int i, int tl, int tr, int ql, int qr) {
eval_lazy(i, tl, tr);
if (ql <= tl && tr <= qr) return tree[i];
int mid = (tl + tr) / 2;
if(mid >= ql && qr >= tl && tr >= ql && qr >= mid+1){
num_t a = query(2 * i + 1, tl, mid, ql, qr);
num_t b = query(2 * i + 2, mid + 1, tr, ql, qr);
return a.op(b);
}else if(mid >= ql && qr >= tl){
num_t a = query(2 * i + 1, tl, mid, ql, qr);
return a;
}else if(tr >= ql && qr >= mid+1){
num_t b = query(2 * i + 2, mid + 1, tr, ql, qr);
return b;
}
return tree[i]; // wont happen kot probably
}
void eval_lazy(int i, int l, int r) {
tree[i].lazy_app(r - l + 1);
if (l != r) {
tree[i*2+1].lazy = tree[i*2+1].lazy_comb(tree[i]);
tree[i*2+2].lazy = tree[i*2+2].lazy_comb(tree[i]);
}
tree[i].lazy = num_t::null_l;
}
};
const int mxN = 5e5+1;
ll n,m,a[mxN],k;
int opt,l,r;
segtree<sum_t> st;
int main() {
std::cin.tie(0); ios::sync_with_stdio(false);
cin >> n >> m;
for(int i=0; i<n; i++){
cin >> a[i];
}
st.init(n,a);
for(int i=0; i<m; i++){
cin >> opt;
if(opt == 2){
cin >> l >> k;
--l;
st.update(l,l,(sum_t){0LL,k});
}else{
cin >> l >> r;
--l, --r;
if(l>r)swap(l,r);
cout << st.query(l,r).val << "\n";
}
}
}
by stegatxins0 @ 2024-04-27 11:07:52
哦发现了错误,小白改变了对某个公园的打分有可能是0,所以null_l应该放INF而不是0