MagicianTime
limit:18000/9000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1700 Accepted Submission (s): 503
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
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath > #include <vector> #include <queue> #include <set> #include <algorithm> #define LL Long longusing namespace Std;const int maxn = 100000 + 10;const long long INF = 0X3F3F3F3F3F3F3F3FLL; LL A[MAXN], ans;struct tree{int L, R; LL ee, eo, oe, oo;} tree[maxn<<2]; ll Max4 (ll A, LL B, LL C, ll D) {return Max (max (A, B), Max (c, D));} void push_up (int rt) {tree[rt].ee = max (-inf, Max (tree[rt<<1].ee + tree[rt<<1|1].oe, Tree[rt<<1].eo + tree[rt<<1|1].ee)); tree[rt].ee = Max (tree[rt].ee, tree[rt<<1].ee); tree[rt].ee = Max (tree[rt].ee, tree[rt<<1|1].ee); Tree[rt].eo = Max (-inf, Max (tree[rt<<1].ee + tree[rt<<1|1].oo, Tree[rt<<1].eo + tree[rt<<1|1]. EO)); Tree[rt].eo = Max (Tree[rt].eo, Tree[rt<<1].eo); Tree[rt].eo = Max (Tree[rt].eo, Tree[rt<<1|1].eo); Tree[rt].oe =Max (-inf, Max (tree[rt<<1].oo + tree[rt<<1|1].ee, Tree[rt<<1].oe + Tree[rt<<1|1].oe)); Tree[rt].oe = Max (Tree[rt].oe, Tree[rt<<1].oe); Tree[rt].oe = Max (Tree[rt].oe, Tree[rt<<1|1].oe); tree[rt].oo = Max (-inf, Max (Tree[rt<<1].oe + tree[rt<<1|1].oo, tree[rt<<1].oo + tree[rt<<1|1]. EO)); tree[rt].oo = Max (tree[rt].oo, tree[rt<<1].oo); tree[rt].oo = Max (tree[rt].oo, tree[rt<<1|1].oo);} void build (int l, int r, int rt) {tree[rt].l = l; tree[rt].r = R; if (L = = r) {if (L & 1) {tree[rt].oo = a[l]; Tree[rt].eo = tree[rt].ee = Tree[rt].oe =-inf; } else {tree[rt].ee = a[l]; Tree[rt].eo = tree[rt].oo = Tree[rt].oe =-inf; } return; } int m = (L + r) >> 1; Build (L, M, rt<<1); Build (M + 1, R, rt<<1|1); PUSH_UP (RT);} void Update (int l, int x, int rt) {if (TREE[RT].L = = L && tree[rt].r = = L) {if (TREE[RT].L & 1) {tree[rt].oo = x; Tree[rt].oe = tree[rt].ee = Tree[rt].eo =-inf; } else {tree[rt].ee = x; Tree[rt].oe = tree[rt].oo = Tree[rt].eo =-inf; } return; } if (l <= TREE[RT<<1].R) update (l, x, rt<<1); else update (l, x, rt<<1|1); PUSH_UP (RT);} Tree query (int l, int r, int rt) {Tree res; if (TREE[RT].L = = L && TREE[RT].R = = r) {res.ee = tree[rt].ee; Res.eo = Tree[rt].eo; res.oo = tree[rt].oo; Res.oe = Tree[rt].oe; return res; } int m = (tree[rt].l + tree[rt].r) >> 1; if (r <= m) return query (L, R, Rt<<1); else if (L > m) return query (L, R, Rt<<1|1); else {Tree T1 = query (L, M, rt<<1); Tree t2 = Query (M + 1, R, rt<<1|1); res.ee = Max (max4 (-inf, t1.ee + t2.oe, T1.eo + t2.ee, t1.ee), t2.ee); res.oo = Max (max4 (-inf, T1.oe + t2.oo, t1.oo + T2.eo, t1.oo), t2.oo); Res.eo = Max (max4 (-inf, t1.ee + t2.oo, T1.eo + T2.eo, T1.eo), T2.eo); Res.oe = Max (max4 (-inf, t1.oo + t2.ee, T1.oe + T2.oe, T1.oe), T2.oe); return res; }}int Main () {int T, n, M; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n, &m); for (int i=1;i<=n;i++) scanf ("%i64d", &a[i]); Build (1, N, 1); int op, l, R; while (m--) {scanf ("%d%d%d", &op, &l, &r); if (op = = 0) {ans =-inf; Tree res = query (l, r, 1); ans = max (ans, max4 (res.ee, res.oo, Res.eo, Res.oe)); printf ("%i64d\n", ans); } else Update (L, r, 1); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5316 Magician (third segment tree of more than 2015 schools)