残阳如血 @ 2024-05-09 20:33:19
// 单点修改,区间查询
#include <iostream>
#include <algorithm>
// #define xinyoudui
const int N = 5e5 + 10;
const int INF = -2e9;
struct TreeNode {
int l, r;
int sum, ans, lmax, rmax;
// sum:区间总和,ans:区间最大子段和,lmax:最大前缀,rmax:最大后缀
TreeNode *lson, *rson;
TreeNode(int _sum = INF, int _ans = INF, int _lamx = INF, int _rmax = INF, int _l = -1, int _r = -1, TreeNode *_lson = nullptr, TreeNode *_rson = nullptr)
: l(_l), r(_r), sum(_sum), ans(_ans), lmax(_lamx), rmax(_rmax), lson(_lson), rson(_rson) {}
~TreeNode() {
if (lson) delete lson;
if (rson) delete rson;
lson = rson = nullptr;
}
};
int n, m, a[N];
bool inRange(TreeNode *u, int L, int R) {
return u->l >= L && u->r <= R;
}
bool outRange(TreeNode *u, int L, int R) {
return u->l > R || u->r < L;
}
void pushup(TreeNode *u, TreeNode *ls, TreeNode *rs) {
u->sum = ls->sum + rs->sum;
u->lmax = std::max(ls->lmax, ls->sum + rs->lmax);
u->rmax = std::max(rs->rmax, rs->sum + ls->rmax);
u->ans = std::max({ls->ans, rs->ans, ls->rmax + rs->lmax});
}
TreeNode *build(int l, int r) {
TreeNode *u = new TreeNode;
u->l = l, u->r = r;
if (l == r) {
u->sum = u->ans = u->lmax = u->rmax = a[l];
u->lson = u->rson = nullptr;
return u;
}
int mid = l + r >> 1;
pushup(u, u->lson = build(l, mid), u->rson = build(mid + 1, r));
return u;
}
TreeNode *query(TreeNode *u, int L, int R) {
if (outRange(u, L, R)) return new TreeNode;
if (inRange(u, L, R)) return u;
TreeNode *res = new TreeNode;
pushup(res, query(u->lson, L, R), query(u->rson, L, R));
return res;
}
void update(TreeNode *u, int p, int x) {
if (u->l == u->r) {
u->sum = u->ans = u->lmax = u->rmax = x;
return ;
}
int mid = u->l + u->r >> 1;
if (p <= mid) update(u->lson, p, x);
else update(u->rson, p, x);
pushup(u, u->lson, u->rson);
}
int main() {
std::cin.tie(0)->sync_with_stdio(0);
std::cin >> n >> m;
for (int i = 1; i <= n; ++i) std::cin >> a[i];
TreeNode *root = build(1, n);
for (int k, a, b; m; --m) {
#ifndef xinyoudui
std::cin >> k >> a >> b;
int s = std::min(a, b), t = std::max(a, b);
if (k == 1) std::cout << query(root, s, t)->ans << '\n';
else update(root, a, b);
#else
std::cin >> k >> a;
update(root, k, a);
std::cout << root->ans << '\n';
#endif
}
std::cout.flush();
return 0;
}
提交记录
by 残阳如血 @ 2024-05-09 20:37:15
现在 45 pts 了,后面几个点还是 MLE,同时求助以下为什么要开 long long?
by Special_Tony @ 2024-05-09 21:00:10
@残阳如血 我不用llAC了,你写法问题吧。
by 残阳如血 @ 2024-05-09 21:00:47
@Special_Tony 那为什么会 MLE 啊/kk
by Special_Tony @ 2024-05-09 21:03:25
@残阳如血 我不到啊,我不是这么些的