1.线段树(最大值)线段树

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

const int MAXNODE = 1 << 19;

const int MAXN = 2e6 + 10;


struct NODE {
int value;
int left, right;
}node[MAXNODE];

int father[MAXN];

inline void BuildTree(int i, int left, int right) {
node[i].left = left;
node[i].right = right;
node[i].value = 0;
if (left == right) {
father[left] = i;
return;
}
BuildTree(i << 1, left, (int)(float(left + right) / 2.0));
BuildTree((i << 1) + 1, (int)(floor(left + right) / 2.0) + 1,right);
}

inline void UpdateTree(int ri) {
if (ri == 1) { return; }
int fi = ri / 2;
int a = node[fi << 1].value;
int b = node[(fi << 1) + 1].value;
node[fi].value = max(a, b);
UpdateTree(ri / 2);
}

int Max;
inline void Query(int i, int l,int r) {
if (node[i].left == l && node[i].right == r) {
Max = max(Max, node[i].value);
return;
}
i = i << 1;
if (l <= node[i].right) {
if (r <= node[i].right) {
Query(i, l, r);
}
else {
Query(i, l, node[i].right);
}
}
i++;
if (r >= node[i].left) {
if (l >= node[i].left) { Query(i, l, r); }
else {
Query(i, node[i].left, r);
}
}
}

int main() {
int n, m, g;
ios::sync_with_stdio(false);
while (cin >> n >> m) {
BuildTree(1, 1, n);
for (int i = 1; i <= n; i++) {
cin >> g;
node[father[i]].value = g;
UpdateTree(father[i]);
}
string op;
int a, b;
while (m--) {
cin >> op >> a >> b;
if (op[0] == 'Q') {
Max = 0;
Query(1, a, b);
cout << Max << endl;
}
else {
node[father[a]].value = b;
UpdateTree(father[a]);
}
}
}
return 0;
}

2.线段树(加和)模板 

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

const int MAXNODE = 1 << 19;

const int MAXN = 2e6 + 10;


struct NODE {
int value;
int left, right;
}node[MAXNODE];

int father[MAXN];

inline void BuildTree(int i, int left, int right) {
node[i].left = left;
node[i].right = right;
node[i].value = 0;
if (left == right) {
father[left] = i;
return;
}
BuildTree(i << 1, left, (int)(float(left + right) / 2.0));
BuildTree((i << 1) + 1, (int)(floor(left + right) / 2.0) + 1, right);
}

inline void UpdateTree(int ri, int vl) {
if (ri == 1) {
node[ri].value += vl;
return;
}
node[ri].value += vl;
UpdateTree(ri / 2, vl);
}
int res;
inline void Query(int i, int l, int r) {
if (node[i].left == l && node[i].right == r) {
//Max = max(Max, node[i].value);
res += node[i].value;
return;
}
i = i << 1;
if (l <= node[i].right) {
if (r <= node[i].right) {
Query(i, l, r);
}
else {
Query(i, l, node[i].right);
}
}
i++;
if (r >= node[i].left) {
if (l >= node[i].left) { Query(i, l, r); }
else {
Query(i, node[i].left, r);
}
}
}

int main() {
int n, m, g, a, b;
ios::sync_with_stdio(false);
while (cin >> n >> m) {
BuildTree(1, 1, n);
for (int i = 1; i <= n; i++) {
cin >> g;
//node[father[i]].value = g;
UpdateTree(father[i],g);
}
string op;
while (m--) {
cin >> op;
if (op[0] == 'E') { break; }
else if (op[0] == 'Q') {
cin >> a >> b;
res = 0;
Query(1, a, b);
cout << res << endl;
}
else if(op[0] == 'S')
{
cin >> a >> b;
UpdateTree(father[a],-b);
}
else {
cin >> a >> b;
UpdateTree(father[a], b);
}
}
}
return 0;
}