Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an innate talent, gaining it through study and practice, or receiving it from another being, often a god, spirit, or demon of some sort. Some wizards are depicted as having a special gift which sets them apart from the vast majority of characters in fantasy worlds who are unable to learn magic.
Magicians, sorcerers, wizards, magi, and practitioners of magic by other titles have appeared in myths, folktales, and literature throughout recorded history, with fantasy works drawing from this background.
In medieval chivalric romance, the wizard often appears as a wise old man and acts as a mentor, with Merlin from the King Arthur stories representing a prime example. Other magicians can appear as villains, hostile to the hero.
Mr. Zstu is a magician, he has many elves like dobby, each of which has a magic power (maybe negative). One day, Mr. Zstu want to test his ability of doing some magic. He made the elves stand in a straight line, from position 1 to position n, and he used two kinds of magic, Change magic and Query Magic, the first is to change an elf’s power, the second is get the maximum sum of beautiful subsequence of a given interval. A beautiful subsequence is a subsequence that all the adjacent pairs of elves in the sequence have a different parity of position. Can you do the same thing as Mr. Zstu ?
Input
The first line is an integer T represent the number of test cases.
Each of the test case begins with two integers n, m represent the number of elves and the number of time that Mr. Zstu used his magic.
(n,m <= 100000)
The next line has n integers represent elves’ magic power, magic power is between -1000000000 and 1000000000.
Followed m lines, each line has three integers like
type a b describe a magic.
If type equals 0, you should output the maximum sum of beautiful subsequence of interval [a,b].(1 <= a <= b <= n)
If type equals 1, you should change the magic power of the elf at position a to b.(1 <= a <= n, 1 <= b <= 1e9)
Output
For each 0 type query, output the corresponding answer.
Sample Input
1
1 1
1
0 1 1
Sample Output
1
给你一组序列,和两个操作,一个是单点修改一个值,另一个是求l~r区间内完美序列的最大值。
所谓的完美序列是这个序列里的数字在原来序列里的位置是奇偶交替的,问这样的值最大是多少。
思路:所谓的奇偶交替,无非就是四种情况:奇偶,偶奇,偶偶,奇奇。
在向上更新的时候,对于这四种情况,都要分开去求最值。
奇偶=max(max(左.奇偶,右.奇偶),max(左.奇偶+右.奇偶,左.奇奇+右.偶偶));
偶奇=max(max(左.偶奇,右.偶奇),max(左.偶奇+右.偶奇,左.偶偶+右.奇奇));
偶偶=max(max(左.偶偶,右.偶偶),max(左.偶偶+右.奇偶,左.偶奇+右.偶偶));
奇奇=max(max(左.奇奇,右.奇奇),max(左.奇偶+右.奇奇,左.奇奇+右.偶奇));
就这样去更新。在求和的时候,用这四个值求最大值。类似于线段树求区间最大子段和。
代码如下:
#include<bits/stdc++.h>
#define ll long long
#define inf 0x7f7f7f7f
using namespace std;
const int maxx=1e5+100;
struct node{
int l;
int r;
ll jj,jo,oj,oo;
}p[maxx<<2];
ll a[maxx];
int n,m;
inline void pushup(int cur)
{
p[cur].oo=max(max(p[cur<<1].oo,p[cur<<1|1].oo),max(p[cur<<1].oj+p[cur<<1|1].oo,p[cur<<1].oo+p[cur<<1|1].jo));
p[cur].jj=max(max(p[cur<<1].jj,p[cur<<1|1].jj),max(p[cur<<1].jo+p[cur<<1|1].jj,p[cur<<1].jj+p[cur<<1|1].oj));
p[cur].oj=max(max(p[cur<<1].oj,p[cur<<1|1].oj),max(p[cur<<1].oo+p[cur<<1|1].jj,p[cur<<1].oj+p[cur<<1|1].oj));
p[cur].jo=max(max(p[cur<<1].jo,p[cur<<1|1].jo),max(p[cur<<1].jj+p[cur<<1|1].oo,p[cur<<1].jo+p[cur<<1|1].jo));
}
inline void build(int l,int r,int cur)
{
p[cur].l=l;
p[cur].r=r;
if(l==r)
{
if(l%2)
{
p[cur].jj=a[l];
p[cur].oj=-inf;
p[cur].jo=-inf;
p[cur].oo=-inf;
}
else
{
p[cur].oo=a[l];
p[cur].oj=-inf;
p[cur].jo=-inf;
p[cur].jj=-inf;
}
return ;
}
int mid=l+r>>1;
build(l,mid,cur<<1);
build(mid+1,r,cur<<1|1);
pushup(cur);
}
inline void update(int pos,ll v,int cur)
{
int L=p[cur].l;
int R=p[cur].r;
if(L==R)
{
if(L&1) p[cur].jj=v;
else p[cur].oo=v;
return ;
}
int mid=L+R>>1;
if(pos<=mid) update(pos,v,cur<<1);
else update(pos,v,cur<<1|1);
pushup(cur);
}
inline node query(int l,int r,int cur)
{
int L=p[cur].l;
int R=p[cur].r;
if(l<=L&&R<=r) return p[cur];
int mid=L+R>>1;
if(r<=mid) return query(l,r,cur<<1);
else if(l>mid) return query(l,r,cur<<1|1);
else
{
node x=query(l,mid,cur<<1);
node y=query(mid+1,r,cur<<1|1);
node ans;
ans.oo=max(max(x.oo,y.oo),max(x.oj+y.oo,x.oo+y.jo));
ans.jj=max(max(x.jj,y.jj),max(x.jo+y.jj,x.jj+y.oj));
ans.oj=max(max(x.oj,y.oj),max(x.oo+y.jj,x.oj+y.oj));
ans.jo=max(max(x.jo,y.jo),max(x.jj+y.oo,x.jo+y.jo));
return ans;
}
}
int main()
{
int t,op,x,y;ll z;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
build(1,n,1);
while(m--)
{
scanf("%d",&op);
if(op==0)
{
scanf("%d%d",&x,&y);
node s=query(x,y,1);
printf("%lld\n",max(max(s.jj,s.oo),max(s.oj,s.jo)));
}
else if(op==1)
{
scanf("%d%lld",&x,&z);
update(x,z,1);
}
}
}
return 0;
}
努力加油a啊,(o)/~