本文纯属原创,转载请注明出处谢谢。http://blog.csdn.net/zip_fan。
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5316
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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 ?
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
如果你能看懂题意并且对线段树有所了解的话,你会觉得:
一个不能更简单的线段树区间合并问题。
恩,前提是看懂了题意= =、、
A beautiful subsequence is a subsequence that all the adjacent pairs of elves in the sequence have a different parity of position.
这句话,来自最后一段,是这个题的核心。
是这么翻译的:一个漂亮的子序列指的是:一个子序列,它的每一对相邻元素在原数组中的下标的奇偶性不同。
请把这句话念10遍。
OK,这个题就是求给定的LR区间内的若干个漂亮的子序列他们各自的和最大是多少。
好吧这个其实也得念10遍。
以上2句话念明白了,这题就水题了。(比赛的时候看错题哭死)
对于线段树每个节点,用四个值 奇始奇终、奇始偶终、偶始奇终、偶始偶终
去分别存四种不同情况的最大值。
然后连接到一起就好了,两个区间合到一起的时候
奇终和偶始 可以合到一起 ,偶终和奇始 可以合到一起。
然后注意有负值所以要注意初始化和判断该情况在这段区间内是否存在。
然后就水过了。
#include <stdio.h> #include <string.h> #include <iostream> #include <math.h> #include <algorithm> #include <vector> #include <map> #define PI acos(-1.0) #define M 1000005 //10^6 #define eps 1e-8 #define LL long long #define moo 1000000007 #define INF -999999999 using namespace std; struct Node { int has[2][2];//存是否有这种情况,0代表无1代表有 long long ma[2][2];//存该情况的最大值 //[i][j]为i始j终的情况,i、j为0则是偶,i、j为1则是奇 }tre[100000*4]; //合并操作 Node uni(Node a,Node b) { Node c; //首先 四种起始和终止情况可以直接继承于左儿子或右儿子的对应情况,取最大 for(int i=0;i<=1;i++) for(int j=0;j<=1;j++) { c.has[i][j]=a.has[i][j]|b.has[i][j]; if(a.has[i][j]&&b.has[i][j]) c.ma[i][j]=max(a.ma[i][j],b.ma[i][j]); else if(a.has[i][j]) c.ma[i][j]=a.ma[i][j]; else if(b.has[i][j]) c.ma[i][j]=b.ma[i][j]; } //其次 四种情况也可以由左儿子和右儿子的情况合并起来。 //例如奇始奇终可以由左儿子的奇始偶终和右儿子的奇始奇终合并,也可以由左儿子的奇始奇终和右儿子的偶始奇终合并,以此类推 for(int i=0;i<=1;i++) for(int j=0;j<=1;j++) for(int k=0;k<=1;k++) if(a.has[i][j]&&b.has[!j][k]) if(c.has[i][k]) c.ma[i][k]=max(c.ma[i][k],a.ma[i][j]+b.ma[!j][k]); else c.has[i][k]=1,c.ma[i][k]=a.ma[i][j]+b.ma[!j][k]; return c; } //初始化线段树,父节点的值是由子节点的合并而来 void build(int num,int le,int ri) { memset(tre[num].has,0,sizeof(tre[num].has)); if(le==ri) { int a; scanf("%d",&a); tre[num].has[le%2][le%2]=1; tre[num].ma[le%2][le%2]=a; return ; } int mid=(le+ri)/2; build(num*2,le,mid); build(num*2+1,mid+1,ri); tre[num]=uni(tre[num*2],tre[num*2+1]); } //修改操作,跟build基本没任何区别 void update(int num,int le,int ri,int x,int y) { if(le==ri) { memset(tre[num].has,0,sizeof(tre[num].has)); tre[num].has[le%2][le%2]=1; tre[num].ma[le%2][le%2]=y; return ; } int mid=(le+ri)/2; if(x<=mid) update(num*2,le,mid,x,y); else update(num*2+1,mid+1,ri,x,y); tre[num]=uni(tre[num*2],tre[num*2+1]); } //查询操作,按照区间查询,然后把左右查的结果合并起来(如果有的话) Node query(int num,int le,int ri,int x,int y) { if(x<=le&&y>=ri) return tre[num]; int flag1=0,flag2=0; Node x1,x2; int mid=(le+ri)/2; if(x<=mid) x1=query(num*2,le,mid,x,y),flag1=1; if(y>mid) x2=query(num*2+1,mid+1,ri,x,y),flag2=1; if(flag1==0) return x2; if(flag2==0) return x1; return uni(x1,x2); } int main() { int T; cin>>T; while(T--) { int n,m; cin>>n>>m; build(1,1,n); for(int i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); if(x==0) { Node t=query(1,1,n,y,z); long long ans; int flag=0; //查询出来的判断四种情况哪种最大。 for(int i=0;i<=1;i++) for(int j=0;j<=1;j++) if(t.has[i][j]) if(flag==0) ans=t.ma[i][j],flag=1; else ans=max(ans,t.ma[i][j]); cout<<ans<<endl; } else update(1,1,n,y,z); } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。