给你n个数 m次询问 :
1.将a位置的书换成b
2.询问一段区间最大子序列的和 这里最大子序列不能出现奇数下标和奇数下标相邻 偶数下标和偶数下标相邻;
很明显的线段树,结构体里存4个变量:
__int64 odd_even; 最大子序列是奇数下标开头 偶数下标结尾
__int64 even_odd;偶数开头 奇数结尾
__int64 odd_odd;奇数开头 奇数结尾
__int64 even_even; 偶数开头 偶数结尾
在更新是 取值情况为 odd_even=max(L( odd_even),R ( odd_even),L( odd_even)+R ( odd_even),L(odd_odd)+R(even_even))
其他三个依次类推。。。。
在查找的时候 返回的是结构体变量 输出是比较返回的结构体里面的四个值就行;
#include#include using namespace std; #define LL(x) (x<<1) #define RR(x) ((x<<1)|1) #define INF -0x3f3f3f3f struct node { __int64 odd_even; __int64 even_odd; __int64 odd_odd; __int64 even_even; }tree[4*100000]; __int64 num[100010]; __int64 max(__int64 a,__int64 b) { return a>b?a:b; } int made(int point) { tree[point].odd_odd=max(tree[LL(point)].odd_odd,tree[RR(point)].odd_odd); tree[point].odd_odd=max(tree[point].odd_odd,tree[LL(point)].odd_odd+tree[RR(point)].even_odd); tree[point].odd_odd=max(tree[point].odd_odd,tree[LL(point)].odd_even+tree[RR(point)].odd_odd); tree[point].even_even=max(tree[LL(point)].even_even,tree[RR(point)].even_even); tree[point].even_even=max(tree[point].even_even,tree[LL(point)].even_even+tree[RR(point)].odd_even); tree[point].even_even=max(tree[point].even_even,tree[LL(point)].even_odd+tree[RR(point)].even_even); tree[point].odd_even=max(tree[LL(point)].odd_even,tree[RR(point)].odd_even); tree[point].odd_even=max(tree[point].odd_even,tree[LL(point)].odd_even+tree[RR(point)].odd_even); tree[point].odd_even=max(tree[point].odd_even,tree[LL(point)].odd_odd+tree[RR(point)].even_even); tree[point].even_odd=max(tree[LL(point)].even_odd,tree[RR(point)].even_odd); tree[point].even_odd=max(tree[point].even_odd,tree[LL(point)].even_odd+tree[RR(point)].even_odd); tree[point].even_odd=max(tree[point].even_odd,tree[LL(point)].even_even+tree[RR(point)].odd_odd); return 0; } int deal(int L,int R,int point) { if(L==R) { tree[point].odd_even=tree[point].even_odd=INF; tree[point].odd_odd=tree[point].even_even=INF; if(L%2==0) tree[point].even_even=num[L]; else tree[point].odd_odd=num[L]; return 0; } int mid=(L+R)/2; deal(L,mid,LL(point)); deal(mid+1,R,RR(point)); made(point); return 0; } int update(int L,int R,int pos,int k,int point) { if(L==R&&L==pos) { tree[point].odd_even=tree[point].even_odd=INF; tree[point].odd_odd=tree[point].even_even=INF; if(L%2==0) tree[point].even_even=k; else tree[point].odd_odd=k; return 0; } int mid=(L+R)/2; if(pos<=mid) update(L,mid,pos,k,LL(point)); else update(mid+1,R,pos,k,RR(point)); made(point); return 0; } struct node find(int L,int R,int left,int right,int point) { if(L==left&&R==right) { return tree[point]; } int mid=(L+R)/2; if(right<=mid) { return find(L,mid,left,right,LL(point)); } else if(left>mid) { return find(mid+1,R,left,right,RR(point)); } else { node a=find(L,mid,left,mid,LL(point)); node b=find(mid+1,R,mid+1,right,RR(point)); node c; c.odd_odd=max(a.odd_odd,b.odd_odd); c.odd_odd=max(c.odd_odd,a.odd_odd+b.even_odd); c.odd_odd=max(c.odd_odd,a.odd_even+b.odd_odd); c.even_even=max(a.even_even,b.even_even); c.even_even=max(c.even_even,a.even_even+b.odd_even); c.even_even=max(c.even_even,a.even_odd+b.even_even); c.odd_even=max(a.odd_even,b.odd_even); c.odd_even=max(c.odd_even,a.odd_even+b.odd_even); c.odd_even=max(c.odd_even,a.odd_odd+b.even_even); c.even_odd=max(a.even_odd,b.even_odd); c.even_odd=max(c.even_odd,a.even_odd+b.even_odd); c.even_odd=max(c.even_odd,a.even_even+b.odd_odd); return c; } } int main() { int n,m,T,i,j; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); deal(1,n,1); for(i=1;i<=n;i++) { scanf("%I64d",&num[i]); } deal(1,n,1); while(m--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); if(a==0) { node p=find(1,n,b,c,1); __int64 Max; Max=max(p.odd_even,p.even_odd); Max=max(Max,p.odd_odd); Max=max(Max,p.even_even); printf("%I64d\n",Max); } else { update(1,n,b,c,1); } } } return 0; }#include