Magician
Time Limit: 18000/9000 MS (Java/Others) Memory
Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1700 Accepted Submission(s): 503
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
2015 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 long using 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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。