线段树,每个节点维护某一段开头和结尾为 奇奇 奇偶 偶奇 偶偶 的四种值......
1 1 1 1 0 1 1
1
/* *********************************************** Author :CKboss Created Time :2015年07月29日 星期三 07时47分37秒 File Name :HDOJ5316.cpp ************************************************ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> using namespace std; typedef long long int LL; const int maxn=100100; const LL INF=1LL<<62; int n,m; int a[maxn]; struct Node { LL jj,jo,oj,oo; Node() { this->clear(); } LL getMax() { return max(max(jj,jo),max(oj,oo)); } void clear() { jj=oo=oj=jo=-INF; } void toString() { printf("jj: %lld jo: %lld oj: %lld oo: %lld\n",jj,jo,oj,oo); } }T[maxn<<2]; #define lrt rt<<1 #define rrt rt<<1|1 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 void push_up(int rt) { /// jo T[rt].jo=max(T[lrt].jo,T[rrt].jo); T[rt].jo=max(T[rt].jo,T[lrt].jj+T[rrt].oo); T[rt].jo=max(T[rt].jo,T[lrt].jo+T[rrt].jo); /// jj T[rt].jj=max(T[lrt].jj,T[rrt].jj); T[rt].jj=max(T[rt].jj,T[lrt].jj+T[rrt].oj); T[rt].jj=max(T[rt].jj,T[lrt].jo+T[rrt].jj); /// oj T[rt].oj=max(T[lrt].oj,T[rrt].oj); T[rt].oj=max(T[rt].oj,T[lrt].oo+T[rrt].jj); T[rt].oj=max(T[rt].oj,T[lrt].oj+T[rrt].oj); /// oo T[rt].oo=max(T[lrt].oo,T[rrt].oo); T[rt].oo=max(T[rt].oo,T[lrt].oj+T[rrt].oo); T[rt].oo=max(T[rt].oo,T[lrt].oo+T[rrt].jo); } Node Union(const Node a,const Node b) { Node ret; /// oj ret.jo=max(a.jo,b.jo); ret.jo=max(ret.jo,a.jj+b.oo); ret.jo=max(ret.jo,a.jo+b.jo); /// jj ret.jj=max(a.jj,b.jj); ret.jj=max(ret.jj,a.jj+b.oj); ret.jj=max(ret.jj,a.jo+b.jj); /// oj ret.oj=max(a.oj,b.oj); ret.oj=max(ret.oj,a.oo+b.jj); ret.oj=max(ret.oj,a.oj+b.oj); /// oo ret.oo=max(a.oo,b.oo); ret.oo=max(ret.oo,a.oj+b.oo); ret.oo=max(ret.oo,a.oo+b.jo); return ret; } void build(int l,int r,int rt) { T[rt].clear(); if(l==r) { if(l%2==0) T[rt].oo=a[l]; else T[rt].jj=a[l]; return ; } int m=(l+r)/2; build(lson); build(rson); push_up(rt); } void update(int P,int V,int l,int r,int rt) { if(l==r&&l==P) { T[rt].clear(); if(l%2==0) T[rt].oo=V; else T[rt].jj=V; return ; } int m=(l+r)/2; if(P<=m) update(P,V,lson); else if(P>m) update(P,V,rson); push_up(rt); } Node query(int L,int R,int l,int r,int rt) { //cout<<l<<" <----> "<<r<<endl; if(L<=l&&r<=R) { return T[rt]; } int m=(l+r)/2; Node ret; ret.clear(); if(L<=m) { Node temp = query(L,R,lson); ret = Union(ret,temp); } if(R>m) { Node temp = query(L,R,rson); ret = Union(ret,temp); } return ret; } void show(int l,int r,int rt) { printf("%d <---> %d\n",l,r); T[rt].toString(); if(l==r) return ; int m=(l+r)/2; show(lson); show(rson); } int K,A,B; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T_T; scanf("%d\n",&T_T); while(T_T--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",a+i); build(1,n,1); //show(1,n,1); while(m--) { scanf("%d%d%d",&K,&A,&B); if(K==0) { Node as = query(A,B,1,n,1); cout<<as.getMax()<<endl; } else if(K==1) { update(A,B,1,n,1); } } } return 0; }