【线段树】HDU 5316 Magician

通道

题意:n个数,2种操作,1是单点更新,2是询问区间内序号为奇偶交错的和。

代码:

  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include <iostream>
  3 #include <fstream>
  4 #include <string>
  5 #include <time.h>
  6 #include <vector>
  7 #include <map>
  8 #include <queue>
  9 #include <algorithm>
 10 #include <stack>
 11 #include <cstring>
 12 #include <cmath>
 13 #include <set>
 14 #include <vector>
 15 using namespace std;
 16 template <class T>
 17 inline bool rd(T &ret) {
 18     char c; int sgn;
 19     if (c = getchar(), c == EOF) return 0;
 20     while (c != ‘-‘ && (c<‘0‘ || c>‘9‘)) c = getchar();
 21     sgn = (c == ‘-‘) ? -1 : 1;
 22     ret = (c == ‘-‘) ? 0 : (c - ‘0‘);
 23     while (c = getchar(), c >= ‘0‘&&c <= ‘9‘) ret = ret * 10 + (c - ‘0‘);
 24     ret *= sgn;
 25     return 1;
 26 }
 27 template <class T>
 28 inline void pt(T x) {
 29     if (x < 0) {
 30         putchar(‘-‘);
 31         x = -x;
 32     }
 33     if (x > 9) pt(x / 10);
 34     putchar(x % 10 + ‘0‘);
 35 }
 36 typedef long long ll;
 37 typedef pair<int, int> pii;
 38 const int N = 100000 + 10;
 39 const ll inf = 1e18;
 40 #define L(x) tree[x].l
 41 #define R(x) tree[x].r
 42 #define X(x) tree[x].node
 43 #define ls (id<<1)
 44 #define rs (id<<1|1)
 45 struct NODE {
 46     ll L[2][2];
 47     void clear(ll a) {
 48         for (int i = 0; i < 2; i++)for (int j = 0; j < 2; j++)L[i][j] = a;
 49     }
 50     NODE operator+(const NODE&x)const {
 51         NODE y;
 52         for (int i = 0; i < 2; i++)
 53             for (int j = 0; j < 2; j++)
 54             {
 55                 y.L[i][j] = max(x.L[i][j], L[i][j]);
 56                 for (int k = 0; k < 2; k++)
 57                 {
 58                     if (L[i][k] != -inf && x.L[k ^ 1][j] != -inf)
 59                     {
 60                         y.L[i][j] = max(y.L[i][j], L[i][k] + x.L[k ^ 1][j]);
 61                     }
 62                 }
 63             }
 64         return y;
 65     }
 66 };
 67 struct Node {
 68     int l, r;
 69     NODE node;
 70 }tree[N << 2];
 71 int n, m, a[N];
 72 void Up(int id) {
 73     X(id) = X(ls) + X(rs);
 74 }
 75 void build(int l, int r, int id) {
 76     L(id) = l; R(id) = r;
 77     X(id).clear(-inf);
 78     if (l == r) {
 79         X(id).L[l & 1][l & 1] = a[l];
 80         return;
 81     }
 82     int mid = (l + r) >> 1;
 83     build(l, mid, ls); build(mid + 1, r, rs);
 84     Up(id);
 85 }
 86 void update(int pos, int val, int id) {
 87     if (L(id) == R(id)) {
 88         X(id).clear(-inf);
 89         X(id).L[pos & 1][pos & 1] = val;
 90         return;
 91     }
 92     int mid = (L(id) + R(id)) >> 1;
 93     if (pos <= mid)
 94         update(pos, val, ls);
 95     else update(pos, val, rs);
 96     Up(id);
 97 }
 98 NODE query(int l, int r, int id) {
 99     if (l == L(id) && R(id) == r) {
100         return X(id);
101     }
102     int mid = (L(id) + R(id)) >> 1;
103     if (r <= mid)
104         return query(l, r, ls);
105     else if (mid < l)
106         return query(l, r, rs);
107     else {
108         return query(l, mid, ls) + query(mid + 1, r, rs);
109     }
110 }
111 int main() {
112     int T;rd(T);
113     while (T--) {
114         rd(n); rd(m);
115         for (int i = 1; i <= n; i++)rd(a[i]);
116         build(1, n, 1);
117         int op, l, r;
118         while (m--) {
119             rd(op); rd(l); rd(r);
120             if (op == 0) {
121                 ll ans = -inf;
122                 NODE E = query(l, r, 1);
123                 for (int i = 0; i < 2; i++)for (int j = 0; j < 2; j++)
124                     ans = max(ans, E.L[i][j]);
125                 pt(ans); puts("");
126             }
127             else
128             {
129                 update(l, r, 1);
130             }
131         }
132     }
133     return 0;
134 }

时间: 2024-10-20 15:35:31

【线段树】HDU 5316 Magician的相关文章

hdu 5316 Magician (线段树)

题目链接: hdu 5316 Magician 题目描述: 有n个精灵,每个精灵都有一个魔法值,现在有两个操作: (0, a, b)查询[a, b]序列中的一个完美序列的最大和,完美序列就是数组中相邻数字的下标奇偶性不同. (1, a, b)更新下标为a的精灵魔法值为b. 对的,没错就是这个意思,但是比赛的时候就是题意卡的死死的有没有啊,就是各种不懂有没有啊!!! 1 #include <cstdio> 2 #include <cstring> 3 #include <ios

HDU 5316 Magician(线段树区间合并, 子序列最值 多校2015啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 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 an

HDU 5316 Magician(线段树区间合并入门)

本文纯属原创,转载请注明出处谢谢.http://blog.csdn.net/zip_fan. 题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5316 Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Fantasy magicians usually gain their ability

HDU 5316——Magician——————【线段树区间合并区间最值】

Magician Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1613    Accepted Submission(s): 470 Problem Description Fantasy magicians usually gain their ability through one of three usual methods:

HDU 5316 Magician(线段树)

Magician Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 150    Accepted Submission(s): 42 Problem Description Fantasy magicians usually gain their ability through one of three usual methods:

HDU 5316 Magician (线段树区间最值,单点更新)

题目链接:Magician 题面: Magician Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1911    Accepted Submission(s): 549 Problem Description Fantasy magicians usually gain their ability through one of t

HDU 5316 Magician (线段树,单值修改,微变形)

题意:给一个初始序列A[1,n],第j个数字代表精灵j的power值,有两种操作:(1)查询区间[L,R] (2)修改某个精灵的power值. 但,查询的是区间[L,R]中一个美丽子序列sub[l,r]的和,美丽子序列是从A[L,R]中挑出的一些数字,这些数字按升序排好序,每两个相邻数字(sub[a],sub[b])在A[L,R]中的下标的的奇偶性不同.sub中至少有一个元素.power值可能为负. 思路: 单值修改的线段树,比较简单的模板.主要的问题在,奇偶性如何满足? 对于一个区间,要从中挑

HDU 5316 Magician (区结合并)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给出一个大小为n的区间,2种操作,更新某一个点的值,或者查询[l,r]区间的"最大值序列"(要求该序列的下标奇偶相间) 思路:线段树的单点更新和区间合并可以实现题目要求,我们维护4个数据就好,查询的比较特殊 (1).区间里最大的以奇数开头和以奇数结尾的序列 (2).区间里最大的以偶数开头和以偶数结尾的序列 (3).区间里最大的以奇数开头和以偶数结尾的序列 (4).区间里最大的以

线段树 [HDU 3308] LCIS

LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4437    Accepted Submission(s): 2006 Problem Description Given n integers.You have two operations:U A B: replace the Ath number by B. (index

线段树 [HDU 1166]

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 46775    Accepted Submission(s): 19828 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就