
1 1 1 1 0 1 1
1
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#define LL long long
using namespace std;
const int MAXN = 100000 + 10;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
LL a[MAXN], ans;
struct Tree
{
int l, r;
LL ee, eo, oe, oo;
}tree[MAXN<<2];
LL max4(LL A, LL B, LL C, LL D)
{
return max(max(A, B), max(C, D));
}
void push_up(int rt)
{
tree[rt].ee = max(-INF, max(tree[rt<<1].ee + tree[rt<<1|1].oe, tree[rt<<1].eo + tree[rt<<1|1].ee));
tree[rt].ee = max(tree[rt].ee, tree[rt<<1].ee);
tree[rt].ee = max(tree[rt].ee, tree[rt<<1|1].ee);
tree[rt].eo = max(-INF, max(tree[rt<<1].ee + tree[rt<<1|1].oo, tree[rt<<1].eo + tree[rt<<1|1].eo));
tree[rt].eo = max(tree[rt].eo, tree[rt<<1].eo);
tree[rt].eo = max(tree[rt].eo, tree[rt<<1|1].eo);
tree[rt].oe = max(-INF, max(tree[rt<<1].oo + tree[rt<<1|1].ee, tree[rt<<1].oe + tree[rt<<1|1].oe));
tree[rt].oe = max(tree[rt].oe, tree[rt<<1].oe);
tree[rt].oe = max(tree[rt].oe, tree[rt<<1|1].oe);
tree[rt].oo = max(-INF, max(tree[rt<<1].oe + tree[rt<<1|1].oo, tree[rt<<1].oo + tree[rt<<1|1].eo));
tree[rt].oo = max(tree[rt].oo, tree[rt<<1].oo);
tree[rt].oo = max(tree[rt].oo, tree[rt<<1|1].oo);
}
void build(int l, int r, int rt)
{
tree[rt].l = l; tree[rt].r = r;
if(l == r)
{
if(l & 1)
{
tree[rt].oo = a[l];
tree[rt].eo = tree[rt].ee = tree[rt].oe = -INF;
}
else
{
tree[rt].ee = a[l];
tree[rt].eo = tree[rt].oo = tree[rt].oe = -INF;
}
return ;
}
int m = (l + r) >> 1;
build(l, m, rt<<1);
build(m + 1, r, rt<<1|1);
push_up(rt);
}
void update(int l, int x, int rt)
{
if(tree[rt].l == l && tree[rt].r == l)
{
if(tree[rt].l & 1)
{
tree[rt].oo = x;
tree[rt].oe = tree[rt].ee = tree[rt].eo = -INF;
}
else
{
tree[rt].ee = x;
tree[rt].oe = tree[rt].oo = tree[rt].eo = -INF;
}
return ;
}
if(l <= tree[rt<<1].r) update(l, x, rt<<1);
else update(l, x, rt<<1|1);
push_up(rt);
}
Tree query(int l, int r, int rt)
{
Tree res;
if(tree[rt].l == l && tree[rt].r == r)
{
res.ee = tree[rt].ee;
res.eo = tree[rt].eo;
res.oo = tree[rt].oo;
res.oe = tree[rt].oe;
return res;
}
int m = (tree[rt].l + tree[rt].r) >> 1;
if(r <= m) return query(l, r, rt<<1);
else if(l > m) return query(l, r, rt<<1|1);
else
{
Tree t1 = query(l, m, rt<<1);
Tree t2 = query(m + 1, r, rt<<1|1);
res.ee = max(max4(-INF, t1.ee + t2.oe, t1.eo + t2.ee, t1.ee), t2.ee);
res.oo = max(max4(-INF, t1.oe + t2.oo, t1.oo + t2.eo, t1.oo), t2.oo);
res.eo = max(max4(-INF, t1.ee + t2.oo, t1.eo + t2.eo, t1.eo), t2.eo);
res.oe = max(max4(-INF, t1.oo + t2.ee, t1.oe + t2.oe, t1.oe), t2.oe);
return res;
}
}
int main()
{
int T, n, m;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i=1;i<=n;i++) scanf("%I64d", &a[i]);
build(1, n, 1);
int op, l, r;
while(m--)
{
scanf("%d%d%d", &op, &l, &r);
if(op == 0)
{
ans = -INF;
Tree res = query(l, r, 1);
ans = max(ans, max4(res.ee, res.oo, res.eo, res.oe));
printf("%I64d\n", ans);
}
else update(l, r, 1);
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 5316 Magician(2015多校第三场 线段树)
原文:http://blog.csdn.net/moguxiaozhe/article/details/47145077