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 ?
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.
![](https://i-blog.csdnimg.cn/blog_migrate/34fc7a49bdaa768d3412ebdbcbab3d77.jpeg)
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)
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
线段树
#include<cstdio> #include<vector> #include<cmath> #include<map> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const LL maxn = 400005; LL T, n, m, x, y, z, ans; struct tree { LL aa, ab, ba, bb; void clear(){ aa = ab = ba = bb = -0x7FFFFFFF; } LL maxn() { return max(max(aa, ab), max(ba, bb)); } }f[maxn], t; tree reset(tree a, tree b) { tree c; c.aa = max(a.aa + b.ba, a.ab + b.aa); c.aa = max(c.aa, max(a.aa, b.aa)); c.ab = max(a.aa + b.bb, a.ab + b.ab); c.ab = max(c.ab, max(a.ab, b.ab)); c.ba = max(a.ba + b.ba, a.bb + b.aa); c.ba = max(c.ba, max(a.ba, b.ba)); c.bb = max(a.ba + b.bb, a.bb + b.ab); c.bb = max(c.bb, max(a.bb, b.bb)); return c; } void build(LL now, LL l, LL r) { if (l == r) { f[now].clear(); if (l & 1) scanf("%lld", &f[now].aa); else scanf("%lld", &f[now].bb); } else { int mid = (l + r) >> 1; build(now + now, l, mid); build(now + now + 1, mid + 1, r); f[now] = reset(f[now + now], f[now + now + 1]); } } void change(LL now,LL l,LL r,LL one,LL v) { if (l == r) { f[now].clear(); if (l & 1) f[now].aa = v; else f[now].bb = v; } else { int mid = (l + r) >> 1; if (one <= mid) change(now + now, l, mid, one, v); else change(now + now + 1, mid + 1, r, one, v); f[now] = reset(f[now + now], f[now + now + 1]); } } void get(LL now, LL l, LL r, LL ll, LL rr) { if (l >= ll&&r <= rr) t = reset(t, f[now]); else { int mid = (l + r) >> 1; if (ll <= mid) get(now + now, l, mid, ll, rr); if (rr > mid) get(now + now + 1, mid + 1, r, ll, rr); } } int main() { scanf("%lld", &T); while (T--) { scanf("%lld%lld", &n, &m); build(1, 1, n); while (m--) { scanf("%lld%lld%lld", &x, &y, &z); if (x) change(1, 1, n, y, z); else { t.clear(); get(1, 1, n, y, z); printf("%lld\n", t.maxn()); } } } return 0; }