Magician
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1671 Accepted Submission(s): 496
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.
![](https://i-blog.csdnimg.cn/blog_migrate/0c30511e05e1931473b3231713235680.jpeg)
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
1 1 1 1 0 1 1
Sample Output
1
Source
Recommend
wange2014
线段树区间合并
两种操作:1.单点更新,2.区间查询,问题是问一个区间的子序列最大元素和,其中子序列满足条件,自序列中任意两个相邻元素所在位置的奇偶性不同。
对于每一个区间,都有4个状态:奇数开始奇数结尾的子序列最大和,偶数开始偶数结尾的子序列最大和,奇数开始偶数结尾子序列最大和,偶数开始奇数结尾子序列最大和。
合并方式: 区间a,区间b->区间c:
c[奇][奇]=max( a[奇][奇],b[偶][偶],a[奇][奇]+b[偶][奇],a[奇][偶]+b[奇][奇] );
a[奇][奇]表示以奇数开头,奇数结尾的子序列最大和,若其中某一项没有,可以设为足够小的值。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson num<<1,le,mid
#define rson num<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
using namespace std;
const int maxn= 100000 ;
//const int maxm= ;
//const int INF= ;
typedef long long ll;
const ll INF =100000000000000+20;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
struct Node
{
ll sum[2][2];
} node[(maxn+20)<<2];
Node union_(Node &a,Node &b)
{
ll k1,k2,maxi;
Node c;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
c.sum[i][j]=max( a.sum[i][j],b.sum[i][j] );
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
{
c.sum[i][j]=max(c.sum[i][j],a.sum[i][k]+b.sum[!k][j]);
}
}
}
return c;
}
void build(int num,int le,int ri)
{
if(le==ri)
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
node[num].sum[i][j]=-INF;
}
}
scanf("%I64d",&node[num].sum[le%2][le%2]);
return ;
}
MID;
build(lson);
build(rson);
node[num]=union_(node[num<<1],node[num<<1|1] );
}
Node query(int x,int y,int num,int le,int ri ) //
{
if(x<=le&&ri<=y) return node[num] ;
MID;
bool tmp1=0,tmp2=0;
Node a,b,c;
if( x<=mid ) tmp1=true,a=query(x,y,num<<1,le,mid);
if( y>mid ) tmp2=true,b=query(x,y,num<<1|1,mid+1,ri );
if(tmp1 && !tmp2) return a;
else if(! tmp1 && tmp2) return b;
return c=union_( a,b );
}
void update(int pos,ll change, int num,int le,int ri)
{
if( le==ri )
{
node[num].sum[le%2][le%2]=change;
return ;
}
MID;
if( pos<=mid ) update(pos,change,lson);
else update(pos,change,rson);
node[num]=union_(node[num<<1],node[num<<1|1] );
}
int main()
{
//freopen("1001in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int i,j,x,y,op; ll b;
int T,m,n;scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--)
{
scanf("%d",&op);
if( !op )
{
scanf("%d%d",&x,&y);
Node c= query(x,y,1,1,n);
ll maxi=-INF;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
maxi=max( maxi, c.sum[i][j] );
}
}
printf("%I64d\n",maxi);
}
else scanf("%d%I64d",&x,&b),update(x,b,1,1,n);
}
}
return 0;
}