码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5316 Magician 线段树

时间:2015-07-28 20:58:58      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:


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

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316

题目大意:T组样例,每组给定一个长度为n的序列,m组操作。操作分为两种,0 a b表示查询a到b这个区间当中,满足下标为奇偶交替的,和的值最大值,1 a b表示把第a个数的值改为b。

思路:首先此题的重点在于如何查询。线段树维护的节点是四个值maxSum[2][2],其中,maxSum[i][j]代表当前所在区间开头为i状态结尾为j状态的最大值【数组下标0表示偶数,1表示奇数】。对于两个区间,如果不合并它们,则在maxSum[i][j] = max(左区间的maxSum[i][j],右区间的 maxSum[i][j]);如果合并,则maxSum[i][j] = (左maxSum[i][0] + 右maxSum[1][j],左maxSum[i][1] + 右maxSum[0][j])。

【ps:此题样例着实坑,此处给出两组组样例:

Input: 

2
4 5
1 2 3 5
0 1 4
1 4 -10
0 1 4
0 2 3
0 2 4
8 1
2 -3 6 -9 1 5 2 -8
0 1 8

Output: 11 6 5 5  ||  13

AC代码:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <cmath>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <queue>
  9 #include <set>
 10 #include <map>
 11 #include <stack>
 12 #include <limits.h>
 13 using namespace std;
 14 typedef long long LL;
 15 #define lson l, mid, rt << 1
 16 #define rson mid + 1, r, rt << 1 | 1
 17 #define MAXN 100005
 18 using namespace std;
 19 typedef long long LL;
 20 const LL inf = (1LL << 32);
 21 LL input[MAXN];
 22 int t, n, Q;
 23 struct Seg {
 24     LL maxSum[2][2];
 25 } tree[MAXN << 2];
 26 struct node {
 27     LL maxSum[2][2];
 28 };
 29 void Pushup(int rt) {
 30     for(int i = 0; i < 2; i++) {
 31         for(int j = 0; j < 2; j++) {
 32             tree[rt].maxSum[i][j] = max(max(tree[rt << 1].maxSum[i][j], tree[rt << 1 | 1].maxSum[i][j]),
 33                 max(tree[rt << 1].maxSum[i][1] + tree[rt << 1 | 1].maxSum[0][j], tree[rt << 1].maxSum[i][0] + tree[rt << 1 | 1].maxSum[1][j]));
 34         }
 35     }
 36 }
 37 void Build(int l, int r, int rt) {
 38     if(l == r) {
 39         if(l % 2) {
 40             tree[rt].maxSum[0][0] = -inf;
 41             tree[rt].maxSum[1][1] = input[l];
 42         } else {
 43             tree[rt].maxSum[0][0] = input[l];
 44             tree[rt].maxSum[1][1] = -inf;
 45         }
 46         tree[rt].maxSum[1][0] = -inf;
 47         tree[rt].maxSum[0][1] = -inf;
 48         return;
 49     }
 50     int mid = (l + r) >> 1;
 51     Build(lson);
 52     Build(rson);
 53     Pushup(rt);
 54 }
 55 void Update(LL v, int pos, int l, int r, int rt) {
 56     if(l == r) {
 57         if(l % 2 == 0) {
 58             tree[rt].maxSum[0][0] = v;
 59             tree[rt].maxSum[1][1] = -inf;
 60         } else {
 61             tree[rt].maxSum[0][0] = -inf;
 62             tree[rt].maxSum[1][1] = v;
 63         }
 64         tree[rt].maxSum[1][0] = -inf;
 65         tree[rt].maxSum[0][1] = -inf;
 66         return;
 67     }
 68     int mid = (l + r) >> 1;
 69     if(pos <= mid) Update(v, pos, lson);
 70     else Update(v, pos, rson);
 71     Pushup(rt);
 72 }
 73 node Query(int st, int ed, int l, int r, int rt) {
 74     node tmp;
 75     if(st == l && r == ed) {
 76         tmp.maxSum[0][0] = tree[rt].maxSum[0][0];
 77         tmp.maxSum[0][1] = tree[rt].maxSum[0][1];
 78         tmp.maxSum[1][0] = tree[rt].maxSum[1][0];
 79         tmp.maxSum[1][1] = tree[rt].maxSum[1][1];
 80         return tmp;
 81     }
 82     int mid = (l + r) >> 1;
 83     if(ed <= mid) return Query(st, ed, lson);
 84     else if(st > mid) return Query(st, ed, rson);
 85     else {
 86         node tmpL = Query(st, mid, lson);
 87         node tmpR = Query(mid + 1, ed, rson);
 88         for(int i = 0; i < 2; i ++) {
 89             for(int j = 0; j < 2; j++) {
 90                 tmp.maxSum[i][j] = max(max(tmpL.maxSum[i][j], tmpR.maxSum[i][j]),
 91                     max(tmpL.maxSum[i][1] + tmpR.maxSum[0][j], tmpL.maxSum[i][0] + tmpR.maxSum[1][j]));
 92             }
 93         }
 94         return tmp;
 95     }
 96 }
 97 int main() {
 98     scanf("%d", &t);
 99     while(t--) {
100         scanf("%d%d", &n, &Q);
101         for(int i = 1; i <= n; i++) {
102             scanf("%I64d", &input[i]);
103         }
104         Build(1, n, 1);
105         while(Q--) {
106             int select;
107             scanf("%d", &select);
108             if(select == 1) {
109                 int pos;
110                 LL v;
111                 scanf("%d%I64d", &pos, &v);
112                 Update(v, pos, 1, n, 1);
113             } else {
114                 int st, ed;
115                 scanf("%d%d", &st, &ed);
116                 node tmp = Query(st, ed, 1, n, 1);
117                 LL ans = -inf;
118                 for(int i = 0; i < 2; i++) {
119                     for(int j = 0; j < 2; j++) {
120                         ans = max(ans, tmp.maxSum[i][j]);
121                     }
122                 }
123                 printf("%I64d\n", ans);
124             }
125         }
126     }
127     return 0;

128 } 

hdu 5316 Magician 线段树

标签:

原文地址:http://www.cnblogs.com/gaoxiang36999/p/4684158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!