您的位置:首页 > 其它

HDU5316 Magician 线段树区间合并

2015-07-29 09:50 369 查看
传送门:HDU5316

Magician

Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

[align=left]Problem Description[/align]
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 ?

[align=left]Input[/align]
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)

[align=left]Output[/align]
For each 0 type query, output the corresponding answer.

[align=left]Sample Input[/align]

1
1 1
1
0 1 1


[align=left]Sample Output[/align]

1


题意:给定一个数组,长度小于10w,数组元素的大小的绝对值小于10^9。有一个操作:单点修改。有一个询问:在给定的[L,R]区间内,选出一个子序列,要求子序列的相邻的两个数的下标奇偶性不同,求子序列的最大和。

思路:用线段树维护每个区间,以奇数下标开头、奇数下标结尾的子序列的最大和jj;奇数下标开头、偶数下标结尾的子序列的最大和jo;偶数数下标开头、奇数下标结尾的子序列的最大和oj;偶数下标开头、偶数下标结尾的子序列的最大和oo;

当然,在叶子节点的时候,只有jj或者oo才有效,其他3个值全部赋值为-INF,合并的时候注意溢出。区间合并时,以oj为例,oj值可能来自于左区间或者右区间的oj,还可能来自左区间的oj+右区间的oj,左区间的oo+右区间的jj。

代码:

#include<cstdio>
#include<cstring>
#define LL __int64
#define INF 0x7FFFFFFFFFFFFFFF
#define maxn 100005
struct node
{
int l,r;
LL jj,jo,oj,oo;
} tree[maxn<<2];
inline LL max(LL a,LL b)
{
return a>b?a:b;
}
inline LL add(LL a,LL b)
{
if(a==-INF||b==-INF) return -INF;
else return a+b;
}
inline void pushup(node &f,const node &l,const node &r)
{
f.jj=max(l.jj,r.jj);
f.jj=max(f.jj,add(l.jj,r.oj));
f.jj=max(f.jj,add(l.jo,r.jj));

f.jo=max(l.jo,r.jo);
f.jo=max(f.jo,add(l.jo,r.jo));
f.jo=max(f.jo,add(l.jj,r.oo));

f.oj=max(l.oj,r.oj);
f.oj=max(f.oj,add(l.oj,r.oj));
f.oj=max(f.oj,add(l.oo,r.jj));

f.oo=max(l.oo,r.oo);
f.oo=max(f.oo,add(l.oo,r.jo));
f.oo=max(f.oo,add(l.oj,r.oo));
}
int val[maxn];
void build(int id,int l,int r)
{
tree[id].l=l;
tree[id].r=r;
if(l==r)
{
if(l&1)
{
tree[id].jj=val[tree[id].l];
tree[id].jo=tree[id].oj=tree[id].oo=-INF;
}
else
{
tree[id].oo=val[tree[id].l];
tree[id].jo=tree[id].oj=tree[id].jj=-INF;
}
}
else
{
int mid=(l+r)>>1;
build(id<<1,l,mid);
build(id<<1|1,mid+1,r);
pushup(tree[id],tree[id<<1],tree[id<<1|1]);
}
}
void update(int id,int pos,int v)
{
if(tree[id].l==tree[id].r)
{
val[tree[id].l]=v;
if(pos&1)
{
tree[id].jj=val[tree[id].l];
tree[id].jo=tree[id].oj=tree[id].oo=-INF;
}
else
{
tree[id].oo=val[tree[id].l];
tree[id].jo=tree[id].oj=tree[id].jj=-INF;
}
}
else
{
int mid=(tree[id].l+tree[id].r)>>1;
if(pos<=mid) update(id<<1,pos,v);
else update(id<<1|1,pos,v);
pushup(tree[id],tree[id<<1],tree[id<<1|1]);
}
}
node que(int id,int l,int r)
{
if(l<=tree[id].l&&tree[id].r<=r) return tree[id];
else
{
int mid=(tree[id].l+tree[id].r)>>1;
if(r<=mid) return que(id<<1,l,r);
if(l>mid) return que(id<<1|1,l,r);
node F,L,R;
L=que(id<<1,l,r);
R=que(id<<1|1,l,r);
pushup(F,L,R);
return F;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++) scanf("%d",&val[i]);
build(1,1,n);
while(m--)
{
int q;
scanf("%d",&q);
if(q==0)
{
node x;
int l,r;
scanf("%d %d",&l,&r);
x=que(1,l,r);
LL ans=max(max(x.jj,x.oo),max(x.jo,x.oj));
printf("%I64d\n",ans);
}
else
{
int pos,v;
scanf("%d %d",&pos,&v);
update(1,pos,v);
}
}
}
return 0;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: