码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5316Magician (线段树)

时间:2015-07-29 12:10:58      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:

Magician

Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1067    Accepted Submission(s): 283


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
 

Source
 

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<math.h>
#include<string>
#include<set>
#include<queue>
#include<map>
using namespace std;  
#define INF -0x7fffffff
#define MAXN 100100
struct  node{//奇奇,奇偶,偶奇,偶偶4种情况
long long jj;
long long jo;
long long oj;
long long oo;

}num[4*MAXN];

int temp[MAXN];

void build(int l,int r,int root){
    if(l==r){
        if(l%2==1){
            num[root].jj=temp[l];
            num[root].jo=INF;
            num[root].oj=INF;
            num[root].oo=INF;
        }
        else{
            num[root].jj=INF;
            num[root].jo=INF;
            num[root].oj=INF;
            num[root].oo=temp[l];
        
        
        }
        return;
    }//if
    int mid=(l+r)/2;
    build(l,mid,root<<1);
    build(mid+1,r,root<<1|1);
    num[root].jj=max(num[root<<1].jj,max(num[root<<1|1].jj,max(num[root<<1].jj+num[root<<1|1].oj,num[root<<1].jo+num[root<<1|1].jj)));//合并
    num[root].jo=max(num[root<<1].jo,max(num[root<<1|1].jo,max(num[root<<1].jo+num[root<<1|1].jo,num[root<<1].jj+num[root<<1|1].oo)));
    num[root].oj=max(num[root<<1].oj,max(num[root<<1|1].oj,max(num[root<<1].oj+num[root<<1|1].oj,num[root<<1].oo+num[root<<1|1].jj)));
    num[root].oo=max(num[root<<1].oo,max(num[root<<1|1].oo,max(num[root<<1].oo+num[root<<1|1].jo,num[root<<1].oj+num[root<<1|1].oo)));
}

void update(int l,int r,int root,int goal,int val){
    if(l==r&&goal==l){
        if(l%2==1){
            num[root].jj=val;
            num[root].jo=INF;
            num[root].oj=INF;
            num[root].oo=INF;
        }
        else{
            num[root].jj=INF;
            num[root].jo=INF;
            num[root].oj=INF;
            num[root].oo=val;
        
        
        }
        return;
    }//if
    int mid=(l+r)/2;
    if(goal<=mid)
    update(l,mid,root<<1,goal,val);
    else
    update(mid+1,r,root<<1|1,goal,val);
    num[root].jj=max(num[root<<1].jj,max(num[root<<1|1].jj,max(num[root<<1].jj+num[root<<1|1].oj,num[root<<1].jo+num[root<<1|1].jj)));
    num[root].jo=max(num[root<<1].jo,max(num[root<<1|1].jo,max(num[root<<1].jo+num[root<<1|1].jo,num[root<<1].jj+num[root<<1|1].oo)));
    num[root].oj=max(num[root<<1].oj,max(num[root<<1|1].oj,max(num[root<<1].oj+num[root<<1|1].oj,num[root<<1].oo+num[root<<1|1].jj)));
    num[root].oo=max(num[root<<1].oo,max(num[root<<1|1].oo,max(num[root<<1].oo+num[root<<1|1].jo,num[root<<1].oj+num[root<<1|1].oo)));
}


node qurey(int l,int r,int root,int L,int R){
    if(L<=l&&R>=r){
        return num[root];
    }
    int mid=(l+r)/2;
    if(R<=mid)
    return qurey(l,mid,root<<1,L,R);
    else if(mid<L)
    return qurey(mid+1,r,root<<1|1,L,R);
    else{
    node u=qurey(l,mid,root<<1,L,R);
    node v=qurey(mid+1,r,root<<1|1,L,R);
    node tans;
    tans.jj=max(u.jj,max(v.jj,max(u.jj+v.oj,u.jo+v.jj)));
    tans.jo=max(u.jo,max(v.jo,max(u.jo+v.jo,u.jj+v.oo)));
    tans.oj=max(u.oj,max(v.oj,max(u.oj+v.oj,u.oo+v.jj)));
    tans.oo=max(u.oo,max(v.oo,max(u.oo+v.jo,u.oj+v.oo)));
    return tans;
    }
}
int main(){
    //freopen("G://test.txt","r",stdin);
        int t,n,m;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;++i)
            scanf("%d",&temp[i]);
        build(1,n,1);
        int ml,x,y;
        for(int i=1;i<=m;++i){
            scanf("%d%d%d",&ml,&x,&y);
            if(ml==1){
                update(1,n,1,x,y);
            }
            else{
                node ans=qurey(1,n,1,x,y);
                long long aa=ans.jj;
                if(ans.jo>aa)
                    aa=ans.jo;
                if(ans.oj>aa)
                    aa=ans.oj;
                if(ans.oo>aa)
                    aa=ans.oo;
                printf("%lld\n",aa);
            }
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 5316Magician (线段树)

标签:

原文地址:http://blog.csdn.net/classicshao/article/details/47124999

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!