HDU 5316 Magician (segment tree interval merging, sub-series maximum of 2015)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5316


Problem Descriptionfantasy magicians usually gain their ability through one of the three usual methods:possessing it as an in Nate Talent, gaining it through study and practice, or receiving it from another being, often a god, spirit, or demon of s ome sort. Some Wizards is depicted as have a special gift which sets them apart from the vast majority of characters in fantasy W Orlds who is unable to learn magic.

Magicians, sorcerers, wizards, Magi, and practitioners of magic by other titles has appeared in myths, folktales, and lit Erature throughout recorded, 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 had many elves like Dobby, and each of the 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 both kinds of magic, change magic an D Query Magic, the first is to change an elf's power, the second is get the maximum sum of beautiful subsequence of a give N interval. A beautiful subsequence is a subsequence then all the adjacent pairs of elves in the sequence has a different parity of P Osition. Can do the same thing as Mr Zstu?


Inputthe first line was an integer T represent the number of test cases.
Each of the test case begins with a integers n, m represent the number of elves and the number of the time that Mr. Zstu use D his magic.
(N,m <= 100000)
The next line have n integers represent elves ' magic power, magic Power is between-1000000000 and 1000000000.
followed M lines, each of the line have 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 the magic power of the elf in position A to B. (1 <= a <= n, 1 <= b <= 1e9)

Outputfor each 0 type query, output the corresponding answer.
Sample Input
11 110 1 1

Sample Output
1

Source2015 multi-university Training Contest 3

Test instructions

Gives n points and M operations;

1:1 A B change the value of point A to B

2:0 A B The largest subsequence between the sub-range A to B and (the original subscript parity of the adjacent elements in this subsequence (which can be discontinuous) is different)

Ps:

Official:


for each node of the segment tree, we need to use a value of four:I start even finally,I start the odd end,Odd start, odd, odd, and even endto maintain the maximum values for four different cases. Finally, the interval is merged into two intervals:even the end and the odd start orboth odd and even beginnings can be combined. The code is as follows:
#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define LL Long long# Define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 |    1//lson and Rson distinguish the left son and right son of the node//rt represents the root of the current subtree (root), that is, the current node const int MAXN = 100000+7;struct node{int has[2][2];//Whether this is the case    LL val[2][2];//The maximum value of the condition} tre[maxn<<2];int max (int x,int y) {if (x > Y) return x; return y;}    Merge node UNION (node A,node b) {node C;            The four initiation and termination conditions can be directly inherited from the corresponding case of the left son or right son 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.val[i][j] = max (A.val[i][j], b.val[i][j]);            else if (A.has[i][j]) c.val[i][j] = A.val[i][j];        else if (B.has[i][j]) c.val[i][j] = B.val[i][j];    }}//Four cases are merged by the situation of the left son and the right son. If the odd beginning and the end can be the left son of the strange beginning and the right son of the odd final merger//can also be from the left son of the odd beginning and the right son of the odd beginning of the end of the merger,And so for (int i = 0; I <= 1, i++) {for (int j = 0; J <= 1; j + +) {for (int k = 0; K &lt ; = 1; k++) {if (A.has[i][j] && b.has[!j][k]) {if (c.has[i][k                    ] {C.val[i][k] = max (C.val[i][k], a.val[i][j]+b.val[!j][k]);                        } else {c.has[i][k] = 1;                    C.VAL[I][K]=A.VAL[I][J]+B.VAL[!J][K]; }}}}} return C;} void pushup (int rt) {Tre[rt] = UNION (tre[rt<<1],tre[rt<<1|1]);}    void build (int l,int R,int RT) {memset (tre[rt].has,0,sizeof (Tre[rt].has));        if (L = = r) {int tt;        scanf ("%d", &AMP;TT);        TRE[RT].HAS[L%2][L%2] = 1;        TRE[RT].VAL[L%2][L%2] = TT;    return;    } int mid = (L + r) >> 1;    Build (Lson);    Build (Rson); Pushup (RT);} void update(int p,int sc,int l,int r,int RT)        {if (L = = r)//leaf node {memset (tre[rt].has,0,sizeof (Tre[rt].has));        TRE[RT].HAS[L%2][L%2] = 1;        TRE[RT].VAL[L%2][L%2] = SC;    return;    } int mid = (L + r) >> 1;    if (P <= mid)//recursive update left subtree or right subtree update (p, SC, Lson);    else Update (P, SC, Rson); Pushup (RT);}    node query (int l,int r,int l,int r,int RT) {if (L <= l && R <= R) {return TRE[RT];    } int flag1 = 0, Flag2 = 0;    Node ans1, ans2;    int mid = (L + r) >> 1;        if (l <= mid)//go left {ans1 = query (L, R, Lson);    Flag1 = 1;        if (Mid < R)//go right {ans2 = query (L, R, Rson);    Flag2 = 1;    } if (!FLAG1) {return ans2;    } if (!flag2) {return ans1; } return UNION (ANS1,ANS2);}    int main () {int t;    int N, M;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m); Build (1, N, 1); Achievements while (M--) {int OP, a, B;            scanf ("%d%d%d", &op,&a,&b);            if (op = = 1) {update (A, B, 1, N, 1);                } else if (op = = 0) {Node T = query (A, B, 1, N, 1);                LL 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.val[i][j];                            flag=1;                        } else ans = max (ans, t.val[i][j]);            }}} printf ("%lld\n", ans); }}} return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5316 Magician (segment tree interval merging, sub-series maximum of 2015)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.