hdu 5316 Magician(2015 Multi-University Training Contest 3)
来源:互联网 发布:网络机房图片 编辑:程序博客网 时间:2024/05/22 06:22
Magician
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 329 Accepted Submission(s): 93
Problem Description
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 ?
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)
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
11 110 1 1
Sample Output
1
题目大意:
给出一段序列,求位置奇偶相间的子序列的最大和,有更改和查询。
解题思路:
线段树,每个节点记录4个值,分别(起始点,终点)为(奇,奇),(奇,偶),(偶,奇),(偶,偶),于是两个节点的合并变为分情况讨论,例如:奇偶=max(奇奇+偶偶,奇偶+奇偶)
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int inf=0x7fffffff;const int maxn=100000+100;struct node{ long long max1,max2,max3,max4;//分别为奇奇,奇偶,偶奇,偶偶} a[maxn*4];int num[maxn];void build(int rt,int l,int r)//建树{ if(l==r) { if(l%2) { a[rt].max1=num[l]; a[rt].max2=-inf; a[rt].max3=-inf; a[rt].max4=-inf; } else { a[rt].max1=-inf; a[rt].max2=-inf; a[rt].max3=-inf; a[rt].max4=num[l]; } return; } int mid=l+(r-l)/2; build(rt<<1,l,mid); build(rt<<1|1,mid+1,r); a[rt].max1=max(a[rt<<1|1].max1,max(a[rt<<1].max1,max(a[rt<<1].max1+a[rt<<1|1].max3,a[rt<<1].max2+a[rt<<1|1].max1))); a[rt].max2=max(a[rt<<1|1].max2,max(a[rt<<1].max2,max(a[rt<<1].max2+a[rt<<1|1].max2,a[rt<<1].max1+a[rt<<1|1].max4))); a[rt].max3=max(a[rt<<1|1].max3,max(a[rt<<1].max3,max(a[rt<<1].max3+a[rt<<1|1].max3,a[rt<<1].max4+a[rt<<1|1].max1))); a[rt].max4=max(a[rt<<1|1].max4,max(a[rt<<1].max4,max(a[rt<<1].max4+a[rt<<1|1].max2,a[rt<<1].max3+a[rt<<1|1].max4)));}void update(int rt,int left,int right,int x,int val)//更新{ if(left==right&&x==left) { if(x%2) { a[rt].max1=val; a[rt].max2=-inf; a[rt].max3=-inf; a[rt].max4=-inf; } else { a[rt].max1=-inf; a[rt].max2=-inf; a[rt].max3=-inf; a[rt].max4=val; } return; } int mid=(left+right)/2;; if(x<=mid) update(rt<<1,left,mid,x,val); else update(rt<<1|1,mid+1,right,x,val); a[rt].max1=max(a[rt<<1|1].max1,max(a[rt<<1].max1,max(a[rt<<1].max1+a[rt<<1|1].max3,a[rt<<1].max2+a[rt<<1|1].max1))); a[rt].max2=max(a[rt<<1|1].max2,max(a[rt<<1].max2,max(a[rt<<1].max2+a[rt<<1|1].max2,a[rt<<1].max1+a[rt<<1|1].max4))); a[rt].max3=max(a[rt<<1|1].max3,max(a[rt<<1].max3,max(a[rt<<1].max3+a[rt<<1|1].max3,a[rt<<1].max4+a[rt<<1|1].max1))); a[rt].max4=max(a[rt<<1|1].max4,max(a[rt<<1].max4,max(a[rt<<1].max4+a[rt<<1|1].max2,a[rt<<1].max3+a[rt<<1|1].max4)));}node query(int rt,int left,int right,int l,int r)//查询{ if(l<=left&&r>=right) { return a[rt]; } int mid=(left+right)/2; if(r<=mid) { return query(rt<<1,left,mid,l,r); } else if(l>mid) { return query(rt<<1|1,mid+1,right,l,r); } else { node u,v,q; u=query(rt<<1,left,mid,l,r); v=query(rt<<1|1,mid+1,right,l,r); q.max1=max(v.max1,max(u.max1,max(u.max1+v.max3,u.max2+v.max1))); q.max2=max(v.max2,max(u.max2,max(u.max2+v.max2,u.max1+v.max4))); q.max3=max(v.max3,max(u.max3,max(u.max3+v.max3,u.max4+v.max1))); q.max4=max(v.max4,max(u.max4,max(u.max4+v.max2,u.max3+v.max4))); return q; }}int main(){ int n,t,m; //freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) { scanf("%d",&num[i]); } build(1,1,n); int q,x,y; for(int i=0; i<m; i++) { scanf("%d%d%d",&q,&x,&y); if(q==1) update(1,1,n,x,y); else { node p=query(1,1,n,x,y); long long ans=p.max1; ans=max(ans,p.max2); ans=max(ans,p.max3); ans=max(ans,p.max4); cout<<ans<<endl; } } } return 0;}
0 0
- hdu 5316 Magician(2015 Multi-University Training Contest 3)
- HDU 5316 Magician(2015 Multi-University Training Contest 3 1001)
- hdu-5316 Magician (2015 Multi-University Training Contest 3)
- hdu 5316 Magician 2015 Multi-University Training Contest 3
- hdu 5316 Magician 2015 Multi-University Training Contest 3
- HDU 5316 Magician 2015 Multi-University Training Contest 3 1001 线段树
- 2015 Multi-University Training Contest 3 1001 Magician
- hdu 5317 RGCDQ 2015 Multi-University Training Contest 3
- hdu 5319 Painter 2015 Multi-University Training Contest 3
- hdu-5325 Crazy Bobo (2015 Multi-University Training Contest 3)
- hdu 5326 Work 2015 Multi-University Training Contest 3
- hdu 5319 Painter 2015 Multi-University Training Contest 3
- hdu 5317 RGCDQ 2015 Multi-University Training Contest 3
- 2015 Multi-University Training Contest 3
- 2015 Multi-University Training Contest 3
- 2015 Multi-University Training Contest 3 RGCDQ
- 2015 Multi-University Training Contest 3
- 2015 Multi-University Training Contest 3
- Inspect_gasket_local_deformable.hdev (网上也有相关的)相关例程学习
- android优化bmob聊天组件
- 使用PostThreadMessage向发送线程消息失败的解决方法
- USB驱动开发之mass storage的枚举识别过程
- 让设计深入人心
- hdu 5316 Magician(2015 Multi-University Training Contest 3)
- spring mongodb Criteria中"and"与"andOperator"方法的区别及"$and"如何工作
- iOS开发实现页面间的数据传递
- NYOJ-水池数目
- Java线程同步:synchronized锁住的是代码还是对象
- nyoj 757 (扣分最少)(队列问题)
- poj 3083 Children of the Candy Corn(广度搜索)
- Android 镜像文件生成流程
- WinCE中C#WinForm利用Web Service查询数据库