做题记录 Luogu P4513
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了做题记录 Luogu P4513相关的知识,希望对你有一定的参考价值。
线段树维护区间最大子列模板(只用单点修改,还要弱一点)
今天终于理解了 root << 1
和 tree[root].l
的区别了:前者是 root 号节点的左子节点,后者是 root 号节点所代表的区间左下标
#include<bits/stdc++.h>
using namespace std;
#define maxn 500005
struct node
{
int l, r, val, mxqz, mxhz, mxzd;
};
node tree[maxn * 4];
int num[maxn], n, m;
void pushup(int root)
{
tree[root].val = tree[root << 1].val + tree[root << 1 | 1].val;
tree[root].mxqz = max(tree[root << 1].mxqz, tree[root << 1].val + tree[root << 1 | 1].mxqz);
tree[root].mxhz = max(tree[root << 1 | 1].mxhz, tree[root << 1 | 1].val + tree[root << 1].mxhz);
tree[root].mxzd = max(tree[root << 1].mxzd, tree[root << 1 | 1].mxzd);
tree[root].mxzd = max(tree[root].mxzd, tree[root << 1].mxhz + tree[root << 1 | 1].mxqz);
return;
}
void build(int root, int l, int r)
{
tree[root].l = l;
tree[root].r = r;
if(l == r)
{
tree[root].val = tree[root].mxqz = tree[root].mxhz = tree[root].mxzd = num[l];
return;
}
int mid = (l + r) >> 1;
build(root << 1, l, mid);
build(root << 1 | 1, mid + 1, r);
pushup(root);
return;
}
void update(int root, int pos, int x)
{
if(tree[root].l == tree[root].r)
{
tree[root].val = tree[root].mxqz = tree[root].mxhz = tree[root].mxzd = x;
return;
}
int mid = (tree[root].l + tree[root].r) >> 1;
if(pos <= mid)
{
update(root << 1, pos, x);
}
else
{
update(root << 1 | 1, pos, x);
}
pushup(root);
return;
}
node query(int root, int L, int R)
{
if(L <= tree[root].l && R >= tree[root].r)
{
return tree[root];
}
int mid = (tree[root].l + tree[root].r) >> 1;
if(R <= mid)
{
return query(root << 1, L, R);
}
if(L > mid)
{
return query(root << 1 | 1, L, R);
}
node lch = query(root << 1, L, R), rch= query(root << 1 | 1, L, R), ret;
ret.val = lch.val + rch.val;
ret.mxqz = max(lch.mxqz, lch.val + rch.mxqz);
ret.mxhz = max(rch.mxhz, rch.val + lch.mxhz);
ret.mxzd = max(lch.mxzd, rch.mxzd);
ret.mxzd = max(ret.mxzd, lch.mxhz + rch.mxqz);
return ret;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
cin >> num[i];
}
build(1, 1, n);
int opt, x, y;
for(int i = 1; i <= m; i++)
{
cin >> opt;
if(opt == 1)
{
cin >> x >> y;
if(x > y)
{
swap(x, y);
}
node ans = query(1, x, y);
cout << ans.mxzd << endl;
}
else if(opt == 2)
{
cin >> x >> y;
update(1, x, y);
}
}
return 0;
}
以上是关于做题记录 Luogu P4513的主要内容,如果未能解决你的问题,请参考以下文章