# kuangbin線段樹專題解析

### 1. HDU 1166 敵兵佈陣

``````#include <stdio.h>
using namespace std;
const int MAXN=1e5 + 10;
int origin[MAXN], tree[MAXN<<2];

void pushup(int p) {
tree[p] = tree[p << 1] + tree[p << 1 | 1];
}

void build(int p, int l, int r) {
if (l == r) {
tree[p] = origin[l];
return;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
pushup(p);
}

void update_node(int p, int l,int r, int q,int v) {
if (l == r) {   //查詢到點
tree[p] += v;
return;
}
int mid = (l + r) >> 1;
if (q > mid) update_node(p << 1 | 1, mid + 1, r, q, v);
else update_node(p << 1, l, mid, q, v);
pushup(p);
}

int query(int p, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr)  return tree[p];    //被包含在詢問區域內的區間(有效的部分)
int mid = (l + r) >> 1;
int temp = 0;
if (qr > mid) temp += query(p << 1 | 1, mid + 1, r, ql, qr);    //分塊切割出有效的部分(已忽略無效部分)
if (ql <= mid) temp += query(p << 1, l, mid, ql, qr);
return temp;
}

int main () {
int T;
scanf("%d", &T);
for (int k = 1; k <= T; k++) {
int n, i, j;
char s[10];
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", origin + i);
build(1, 1, n);
printf("Case %d:\n", k);
while(scanf("%s", s) && s[0] != 'E') {
scanf("%d%d", &i, &j);
if (s[0] == 'A')
update_node(1, 1, n, i, j);
else if (s[0] == 'S')
update_node(1, 1, n, i, -j);
else
printf("%d\n", query(1, 1, n, i, j));
}
}
}``````

### 2. HDU 1754 I Hate It

``````#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAXN = 2e5 + 10;

int origin[MAXN], tree[MAXN<<2], lazy[MAXN<<2];

void pushup(int p) {
tree[p] = max(tree[p << 1], tree[p << 1 | 1]);
}

void build(int p, int l, int r) {
if (l == r) {
tree[p] = origin[l];
return;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
pushup(p);
}

void update_node(int p, int l,int r, int q,int v) {
if (l == r) {   //查詢到點
tree[p] = v;
return;
}
int mid = (l + r) >> 1;
if (q > mid) update_node(p << 1 | 1, mid + 1, r, q, v);
else update_node(p << 1, l, mid, q, v);
pushup(p);
}

int query(int p, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr)  return tree[p];    //被包含在詢問區域內的區間(有效的部分)
int mid = (l + r) >> 1;
int temp = -1;
if (qr > mid) temp = max(temp, query(p << 1 | 1, mid + 1, r, ql, qr));    //分塊切割出有效的部分(已忽略無效部分)
if (ql <= mid) temp = max(temp, query(p << 1, l, mid, ql, qr));
return temp;
}

int main() {
int n, m;
while(scanf("%d%d", &n, &m) != EOF) {
for (int i = 1; i <= n; i++)
scanf("%d", origin + i);
build(1, 1, n);
while(m--) {
char c[3];
int i, j;
scanf("%s%d%d", c, &i, &j);
if (c[0] == 'Q')
printf("%d\n", query(1, 1, n, i, j));
else
update_node(1, 1, n, i, j);
}
}
}``````

### 3. POJ 3468 A Simple Problem with Integers

``````#include <stdio.h>
using namespace std;
typedef long long ll;
const int MAXN=1e5 + 10;
ll origin[MAXN], tree[MAXN<<2], lazy[MAXN<<2];

void pushup(int p) {
tree[p] = tree[p << 1] + tree[p << 1 | 1];
}

void pushdown(int p, int l, int r) {
if (lazy[p]) {
lazy[p << 1] += lazy[p];
lazy[p << 1 | 1] += lazy[p];
int mid = (l + r) >> 1;
tree[p << 1] += lazy[p] * (mid - l + 1);  //左右兒子結點均按照需要加的值總和更新結點信息
tree[p << 1 | 1] += lazy[p] * (r - mid);
lazy[p] = 0;
}
}

void build(int p, int l, int r) {
if (l == r) {
tree[p] = origin[l];
return;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
pushup(p);
}

void update(int p, int l, int r, int ql, int qr, ll v) {   //區間更新
if (ql <= l && r <= qr) {       //當前區間在更新區間內
tree[p] += v * (r - l + 1);
lazy[p] += v;
//   pushdown(p, l, r);
return;
}
int mid = (l + r) >> 1;
pushdown(p, l, r);
if (ql <= mid)  update(p << 1, l, mid, ql, qr, v);
if (qr > mid)  update(p << 1 | 1, mid + 1, r, ql, qr, v);
pushup(p);
}

ll query(int p, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr)  return tree[p];    //被包含在詢問區域內的區間(有效的部分)
int mid = (l + r) >> 1;
pushdown(p, l, r);        //有區間更新時才需要
ll temp = 0;
if (qr > mid) temp += query(p << 1 | 1, mid + 1, r, ql, qr);    //分塊切割出有效的部分(已忽略無效部分)
if (ql <= mid) temp += query(p << 1, l, mid, ql, qr);
return temp;
}

int main () {

int n, m;
while(scanf("%d%d", &n, &m)!= EOF) {
int i, j;
ll c;
char s[5];
for (int i = 1; i <= n; i++)
scanf("%lld", origin + i);
build(1, 1, n);
while(m--) {
scanf("%s", s);
if (s[0] == 'Q') {
scanf("%d%d", &i, &j);
printf("%lld\n", query(1, 1, n, i, j));
}
else {
scanf("%d%d%lld", &i, &j, &c);
update(1, 1, n, i, j, c);
}
}
}
}
``````

### 5. HDU 1698 Just a Hook

``````#include <stdio.h>
#include <memory.h>
using namespace std;
const int MAXN=1e5 + 10;
int origin[MAXN], tree[MAXN<<2], lazy[MAXN<<2];

void pushup(int p) {
tree[p] = tree[p << 1] + tree[p << 1 | 1];
}

void pushdown(int p, int l, int r) {
if (lazy[p]) {
lazy[p << 1] = lazy[p];
lazy[p << 1 | 1] = lazy[p];
int mid = (l + r) >> 1;
tree[p << 1] = lazy[p] * (mid - l + 1);  //左右兒子結點均按照需要加的值總和更新結點信息
tree[p << 1 | 1] = lazy[p] * (r - mid);
lazy[p] = 0;
}
}

void build(int p, int l, int r) {
if (l == r) {
tree[p] = origin[l];
return;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
pushup(p);
}

void update(int p, int l, int r, int ql, int qr, int v) {   //區間更新
if (ql <= l && r <= qr) {       //當前區間在更新區間內
tree[p] = v * (r - l + 1);
lazy[p] = v;
return;
}
int mid = (l + r) >> 1;
pushdown(p, l, r);
if (ql <= mid)  update(p << 1, l, mid, ql, qr, v);
if (qr > mid)  update(p << 1 | 1, mid + 1, r, ql, qr, v);
pushup(p);
}

int query(int p, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr)  return tree[p];    //被包含在詢問區域內的區間(有效的部分)
int mid = (l + r) >> 1;
pushdown(p, l, r);        //有區間更新時才需要
int temp = 0;
if (qr > mid) temp += query(p << 1 | 1, mid + 1, r, ql, qr);    //分塊切割出有效的部分(已忽略無效部分)
if (ql <= mid) temp += query(p << 1, l, mid, ql, qr);
return temp;
}

int main () {

int T;
scanf("%d", &T);
for (int k = 1; k <= T; k++) {
int n, m;
scanf("%d%d", &n, &m);
int i, j, c;
for (int i = 1; i <= n; i++)
origin[i] = 1;
memset(lazy, 0, sizeof(lazy));
build(1, 1, n);
while(m--) {
scanf("%d%d%d", &i, &j, &c);
update(1, 1, n, i, j, c);
}
printf("Case %d: The total value of the hook is %d.\n", k, query(1,1,n,1, n));
}
}``````

### 7. POJ 3264 Balanced Lineup

``````#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAXN = 5e4 + 10;
const int INF = 0x3f3f3f3f;

int origin[MAXN];
struct node {
int max;
int min;
}tree[MAXN<<2];

void pushup(int p) {
tree[p].max = max(tree[p << 1].max, tree[p << 1 | 1].max);
tree[p].min = min(tree[p << 1].min, tree[p << 1 | 1].min);
}

void build(int p, int l, int r) {
if (l == r) {
tree[p].max = tree[p].min = origin[l];
return;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
pushup(p);
}

node query(int p, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr)  return tree[p];    //被包含在詢問區域內的區間(有效的部分)
int mid = (l + r) >> 1;
node temp, temp2;
temp.max = -INF;
temp.min = INF;
if (qr > mid) {
//一定要注意：這裏應該先存下temp2,而不是每次都query出來，可以省很多時間，否則會TLE
temp2 = query(p << 1 | 1, mid + 1, r, ql, qr);
temp.max = max(temp.max, temp2.max);
temp.min = min(temp.min, temp2.min);
}
if (ql <= mid) {
temp2 = query(p << 1, l, mid, ql, qr);
temp.max = max(temp.max, temp2.max);
temp.min = min(temp.min, temp2.min);
}
return temp;
}

int main () {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)   //origin數組的下標從1開始
scanf("%d", origin + i);
build(1, 1, n);     //拿區間 1, n 的數據建樹
while (q--) {
int i, j;
scanf("%d%d", &i, &j);
node temp = query(1, 1, n, i, j);
printf("%d\n", temp.max - temp.min);
}
}``````