
1 1 1 1 0 1 1
1
2015 Multi-University Training Contest 3
简单的线段树区间叠加,维护区间内以奇偶开头/结尾的最大值,注意初始化时全部初始化为-INF, 在更新的时候就会选择最大的进行更新,自动的决定选取或者不选取。
#include<cstdio>
#include<cmath>
#include<stdlib.h>
#include<map>
#include<set>
#include<time.h>
#include<vector>
#include<queue>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define INF 0x3f3f3f3f
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define lson L, mid, rt<<1
#define rson mid+1, R, rt<<1|1
#define maxn 100000 + 10
long long a[maxn];
int n, m;
struct Node
{
long long M[2][2];
}T[maxn<<2];
void pushup(int rt)
{
int l = rt<<1, r = rt<<1|1;
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
{
T[rt].M[i][j] = max(max(T[l].M[i][j], T[r].M[i][j]), max(T[l].M[i][0]+T[r].M[1][j], T[l].M[i][1]+T[r].M[0][j]));
}
}
void build(int L, int R, int rt)
{
if(L == R)
{
scanf("%I64d", &a[L]);
if(L & 1)
{
T[rt].M[1][1] = a[L];
T[rt].M[0][0] = -INF;
}
else
{
T[rt].M[0][0] = a[L];
T[rt].M[1][1] = -INF;
}
T[rt].M[1][0] = -INF;
T[rt].M[0][1] = -INF;
return ;
}
int mid = (L + R) >> 1;
build(lson);
build(rson);
pushup(rt);
}
void update(int t, int v, int L, int R, int rt)
{
if(L == R)
{
if(L & 1)
{
T[rt].M[1][1] = v;
}
else
{
T[rt].M[0][0] = v;
}
return ;
}
int mid = (L + R) >> 1;
if(t <= mid) update(t, v, lson);
else update(t, v, rson);
pushup(rt);
}
Node query(int l, int r, int L, int R, int rt)
{
Node tmp;
if(l == L && r == R)
{
tmp = T[rt];
return tmp;
}
int mid = (L + R) >> 1;
if(r <= mid) return query(l, r, lson);
else if(l > mid) return query(l, r, rson);
else
{
Node ltmp = query(l, mid, lson);
Node rtmp = query(mid+1, r, rson);
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
{
tmp.M[i][j] = max(max(ltmp.M[i][j], rtmp.M[i][j]), max(ltmp.M[i][0]+rtmp.M[1][j], ltmp.M[i][1]+rtmp.M[0][j]));
}
return tmp;
}
}
int main()
{
int kase;
scanf("%d", &kase);
while(kase--)
{
scanf("%d%d", &n, &m);
build(1, n, 1);
for(int i=0; i<m; i++)
{
int t, a, b;
scanf("%d%d%d", &t, &a, &b);
if(t == 0)
{
long long ans = -INF;
Node tmp = query(a, b, 1, n, 1);
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
{
ans = max(ans, tmp.M[i][j]);
}
printf("%I64d\n", ans);
}
else
{
update(a, b, 1, n, 1);
}
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/dojintian/article/details/47125859