HDU 5316 Magician(线段树区间合并入门)

本文纯属原创,转载请注明出处谢谢。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;
}

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

时间: 2024-10-24 02:55:51

HDU 5316 Magician(线段树区间合并入门)的相关文章

HDU 5316 Magician(线段树区间合并, 子序列最值 多校2015啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 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 an

hdu5316 Magician(线段树区间合并)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:有n个精灵,每个精灵有一个能力值,有两种操作①改变某一个精灵的能力值②查询区间[L,R]里面位置奇偶相间的能力值的最大和. 分析:这题线段树区间合并可以做.每个节点保存4个信息:①以奇位置开始偶位置结束的奇偶序列最大和②以奇位置开始奇位置结束的奇偶序列最大和③以偶位置开始偶位置结束的奇偶序列最大和④以偶位置开始奇位置结束的奇偶序列最大和 合并的时候,以奇位置开始偶位置结束的奇偶序列最大和=m

HDU 3308 LCIS (线段树区间合并)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[mid + 1] > a[mid],写的细心点就好了. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int MAXN = 1

hdu 5316 Magician (线段树)

题目链接: hdu 5316 Magician 题目描述: 有n个精灵,每个精灵都有一个魔法值,现在有两个操作: (0, a, b)查询[a, b]序列中的一个完美序列的最大和,完美序列就是数组中相邻数字的下标奇偶性不同. (1, a, b)更新下标为a的精灵魔法值为b. 对的,没错就是这个意思,但是比赛的时候就是题意卡的死死的有没有啊,就是各种不懂有没有啊!!! 1 #include <cstdio> 2 #include <cstring> 3 #include <ios

POJ-3667 线段树区间合并入门题

题意:长度为n的区间,m个操作,一开始都是0 1 x表示求出长度为x的0的连续区间的最左端,并把这个区间变成1 2 x y表示将区间[x,y]变成0 线段树的区间合并第一题: 每次维护左端连续区间长度ls.右端连续区间长度rs,最大连续长度ms 区间合并的注意点主要在push up操作: 每次更新了一段区间之后向上更新,首先,父区间的ls继承左子树的ls,父区间的rs继承右子树的rs 然后就是重点:左右区间合并之后中间部分可能是连续的!!! 所以:如果整个左.右子区间都是“满的”,父区间的ls和

HDU 1540(线段树+区间合并)学习记录

学习了线段树的新姿势,记录一下 参考blog:https://blog.csdn.net/sunyutian1998/article/details/79618316 query的时候m-ql+1和qr-m写成了m-l+1.r-m,wa了几发之后才找到bug 错误样例: 10 1Q 5 wrong answer:5 顺带一提,这个题目可以在set上面二分直接过,不过最主要还是为了练习区间合并 #include<bits/stdc++.h> using namespace std; const

hdu 1540(线段树区间合并)

题目链接:传送门 参考文章:传送门 题意:n个数字初始连在一条线上,有三种操作, D x表示x号被摧毁: R 表示恢复剩下的通路 Q表示查询标号为x所在的串的最长长度. 思路:线段树的区间合并. #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 50500; struct Node{ int l,r; int ls,rs,ms; }cur

HDU 3308 LCIS(线段树区间合并)

Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting from 0) Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b]. Input T in the first line, indicat

HDU 4339 线段树区间合并

Query Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2573    Accepted Submission(s): 851 Problem Description You are given two strings s1[0..l1], s2[0..l2] and Q - number of queries.Your task