题意:n个数,2种操作,1是单点更新,2是询问区间内序号为奇偶交错的和。
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 }