HDU 5316 Magician (segment tree)

Source: Internet
Author: User

Label:

MagicianTime limit:18000/9000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): Accepted submission (s): 42


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
Recommendwange2014 | We have carefully selected several similar problems for you:5326 5325 5324 5323 5322


/* Test instructions: In an interval to take the number, take the number of numbers sorted by the size of the label, if the label is odd and even alternating to meet the condition of the sequence, to satisfy the number of the condition sequence and the maximum idea: line segment tree, A[0][0] for this even start, even end, 0, 1, representing the odd end of the beginning of Parity 1 0 odd-even transformation equation in the Pushup function, it is very unfortunate to look at the function note that the game did not do it, because my query function returned is ___int64 has been timed out, later looked at others blog, found others Return to structure, */#pragma comment (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <cstring># include<algorithm> #include <vector> #include <string> #include <iostream> #include <queue > #include <cmath> #include <map> #include <stack> #include <bitset>using namespace std;# Define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (y x) ((x+y) >>1) typedef long long ll; #define BUG Prin TF ("hi\n") #define Inf-1000000005#define n 100005inline ll Max (ll X,ll y) {return x>y?x:y;}  struct stud{int le,ri;  ll A[2][2];      Stud operator = (stud b) {for (Int. i=0;i<2;i++) for (int j=0;j<2;j++) B.A[I][J]=A[I][J];  return *this; }}f[n*4];ll a[n];int n,m;void pushup (int pos) {F[pos].a[0][0]=max (F[l (POS)].a[0][0],f[r (POS)].a[0][0]);//left point, right endpoint take larger F[pos].a[0][0]=max (F[pos].a[0][0], F[l (POS)].a[0][0]+f[r (POS)].a[1][0]);//should be very good understanding F[pos].a[0][0]=max (F[pos].a[0][0],f[l (POS)].a[0][1]+f[r (POS)].a[0   ][0]);   F[pos].a[0][1]=max (F[l (POS)].a[0][1],f[r (POS)].a[0][1]);   F[pos].a[0][1]=max (F[pos].a[0][1],f[l (POS)].a[0][0]+f[r (POS)].a[1][1]);   F[pos].a[0][1]=max (F[pos].a[0][1],f[l (POS)].a[0][1]+f[r (POS)].a[0][1]);   F[pos].a[1][0]=max (F[l (POS)].a[1][0],f[r (POS)].a[1][0]);   F[pos].a[1][0]=max (F[pos].a[1][0],f[l (POS)].a[1][0]+f[r (POS)].a[1][0]);   F[pos].a[1][0]=max (F[pos].a[1][0],f[l (POS)].a[1][1]+f[r (POS)].a[0][0]);   F[pos].a[1][1]=max (F[l (POS)].a[1][1],f[r (POS)].a[1][1]);   F[pos].a[1][1]=max (F[pos].a[1][1],f[l (POS)].a[1][0]+f[r (POS)].a[1][1]); F[pos].a[1][1]=max (F[pos].a[1][1],f[l (POS)].a[1][1]+f[r (POS)].a[0][1]);}            void update (int pos,int le,int ri) {if (F[pos].le==le&&f[pos].ri==le) {if (le&1) { F[pos].a[1][1]=rI            F[pos].a[0][0]=inf;        F[pos].a[0][1]=f[pos].a[1][0]=inf;            } else {F[pos].a[0][0]=ri;        F[pos].a[0][1]=f[pos].a[1][0]=f[pos].a[1][1]=inf;    } return;    } int Mid=mid (F[POS].LE,F[POS].RI);    if (mid>=le) Update (L (POS), Le,ri);   else Update (R (POS), Le,ri); Pushup (POS);}    Stud query (int pos,int le,int ri) {if (F[pos].le==le&&f[pos].ri==ri) {return f[pos];    } int Mid=mid (F[POS].LE,F[POS].RI);    if (Mid>=ri) return query (L (POS), Le,ri);    else if (mid<le) return query (R (POS), Le,ri);        else {stud te=query (L (POS), le,mid);        Stud He=query (R (POS), Mid+1,ri);        Stud ans;        Ans.a[0][0]=max (Max (Max (te.a[0][0],he.a[0][0]), te.a[0][1]+he.a[0][0]), te.a[0][0]+he.a[1][0]);        Ans.a[0][1]=max (Max (Max (te.a[0][1],he.a[0][1]), te.a[0][1]+he.a[0][1]), te.a[0][0]+he.a[1][1]); Ans.a[1][0]=max (Max (te.a[1][0],he.a[1][0), te.a[1][1]+he.a[0][0]), te.a[1][0]+he.a[1][0]);        Ans.a[1][1]=max (Max (Max (te.a[1][1],he.a[1][1]), te.a[1][1]+he.a[0][1]), te.a[1][0]+he.a[1][1]);    return ans;    }}void Build (int pos,int le,int ri) {f[pos].le=le;    F[pos].ri=ri;            if (Le==ri) {if (le&1) {F[pos].a[1][1]=a[le];            F[pos].a[0][0]=inf;        F[pos].a[0][1]=f[pos].a[1][0]=inf;            } else {F[pos].a[0][0]=a[le];        F[pos].a[0][1]=f[pos].a[1][0]=f[pos].a[1][1]=inf;    } return;    } int Mid=mid (LE,RI);    Build (L (POS), le,mid);    Build (R (POS), Mid+1,ri); Pushup (POS);}    int main () {int i,j,t;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        for (i=1;i<=n;i++) scanf ("%lld", &a[i]);        Build (1,1,n);        int Op,le,ri;            while (m--) {scanf ("%d%d%d", &op,&le,&ri); if (op==0) {stud temp=query (1,le,ri);                ll Ans=temp.a[0][0];                for (i=0;i<2;i++) for (j=0;j<2;j++) Ans=max (Ans,temp.a[i][j]);            printf ("%lld\n", ans);            } else {update (1,LE,RI); }}} return 0;} /*55 3-6-8 3 4 50 1 51 4-20 1 5*/

/* Timeout code */#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <cstring># include<algorithm> #include <vector> #include <string> #include <iostream> #include <queue > #include <cmath> #include <map> #include <stack> #include <bitset>using namespace std;# Define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (y x) ((x+y) >>1) typedef long long ll; #define BUG Prin TF ("hi\n") #define Inf-1000000005#define n 100005inline ll Max (ll X,ll y) {return x>y?x:y;}  struct stud{int le,ri; ll a[2][2];}   F[n*4];ll a[n];int n,m;void pushup (int pos) {F[pos].a[0][0]=max (F[l (POS)].a[0][0],f[r (POS)].a[0][0]);   F[pos].a[0][0]=max (F[pos].a[0][0],f[l (POS)].a[0][0]+f[r (POS)].a[1][0]);   F[pos].a[0][0]=max (F[pos].a[0][0],f[l (POS)].a[0][1]+f[r (POS)].a[0][0]);   F[pos].a[0][1]=max (F[l (POS)].a[0][1],f[r (POS)].a[0][1]);   F[pos].a[0][1]=max (F[pos].a[0][1],f[l (POS)].a[0][0]+f[r (POS)].a[1][1]); F[pos].a[0][1]=max (f[pos].a[0][1],f[l (POS)].a[0][1]+f[r (POS)].a[0][1]);   F[pos].a[1][0]=max (F[l (POS)].a[1][0],f[r (POS)].a[1][0]);   F[pos].a[1][0]=max (F[pos].a[1][0],f[l (POS)].a[1][0]+f[r (POS)].a[1][0]);   F[pos].a[1][0]=max (F[pos].a[1][0],f[l (POS)].a[1][1]+f[r (POS)].a[0][0]);   F[pos].a[1][1]=max (F[l (POS)].a[1][1],f[r (POS)].a[1][1]);   F[pos].a[1][1]=max (F[pos].a[1][1],f[l (POS)].a[1][0]+f[r (POS)].a[1][1]); F[pos].a[1][1]=max (F[pos].a[1][1],f[l (POS)].a[1][1]+f[r (POS)].a[0][1]);}            void update (int pos,int le,int ri) {if (F[pos].le==le&&f[pos].ri==le) {if (le&1) {            F[pos].a[1][1]=ri;            F[pos].a[0][0]=inf;        F[pos].a[0][1]=f[pos].a[1][0]=inf;            } else {F[pos].a[0][0]=ri;        F[pos].a[0][1]=f[pos].a[1][0]=f[pos].a[1][1]=inf;    } return;    } int Mid=mid (F[POS].LE,F[POS].RI);    if (mid>=le) Update (L (POS), Le,ri);   else Update (R (POS), Le,ri); Pushup (POS);} ll query (int pos,int le,int ri,int s,int e) {if (F[pos].le==le&&f[pos].ri==ri) {return f[pos].a[s][e];    } int Mid=mid (F[POS].LE,F[POS].RI);    if (Mid>=ri) return query (L (POS), le,ri,s,e);    else if (mid<le) return query (R (POS), le,ri,s,e);        else {ll temp=query (L (POS), le,mid,s,s) +query (R (POS), mid+1,ri,s^1,e);        ll Hh=query (L (POS), le,mid,s,s^1) +query (R (POS), mid+1,ri,s,e);    Return Max (TEMP,HH);    }}void Build (int pos,int le,int ri) {f[pos].le=le;    F[pos].ri=ri;            if (Le==ri) {if (le&1) {F[pos].a[1][1]=a[le];            F[pos].a[0][0]=inf;        F[pos].a[0][1]=f[pos].a[1][0]=inf;            } else {F[pos].a[0][0]=a[le];        F[pos].a[0][1]=f[pos].a[1][0]=f[pos].a[1][1]=inf;    } return;    } int Mid=mid (LE,RI);    Build (L (POS), le,mid);    Build (R (POS), Mid+1,ri); Pushup (POS);}  ll dp[105][2];ll solve (int le,int ri) {int i,j; for (i=0;i+le<=ri;i++) dp[i][0]=dp[i][1]=inf;      if ((le%2) ==0) {Dp[0][0]=a[le];  Dp[0][1]=inf;     } else {dp[0][0]=inf;  Dp[0][1]=a[le];         } for (i=le+1;i<=ri;i++) {if (i&1) {dp[i-le][0]=dp[i-1-le][0];     Dp[i-le][1]=max (Max (dp[i-le-1][0]+a[i],dp[i-le-1][1]), a[i]);         } else {dp[i-le][1]=dp[i-le-1][1];     Dp[i-le][0]=max (Max (dp[i-le-1][1]+a[i],dp[i-le-1][0]), a[i]); }} return Max (Dp[ri-le][0],dp[ri-le][1]);}    int main () {int i,j,t;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        for (i=1;i<=n;i++) scanf ("%lld", &a[i]);        Build (1,1,n);        int Op,le,ri;            while (m--) {scanf ("%d%d%d", &op,&le,&ri);            Ri=min (N,ri);                if (op==0) {int len=ri-le+1;                if (len<=100) {printf ("%lld\n", Solve (Le,ri));               Continue } ll Temp=max (query (1,le,ri,0,0), query (1,le,ri,0,1));                ll Hh=max (Query (1,le,ri,1,0), query (1,le,ri,1,1));            printf ("%i64d\n", Max (TEMP,HH));                } else {update (1,LE,RI);            A[le]=ri; }}} return 0;} /*55 3-6-8 3 4 50 1 51 4-20 1 5*/




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

HDU 5316 Magician (segment tree)

Related Article

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.

Tags Index: