# 线段树的核心是区间 与query(The core of segment tree is interval and query)-其他

## 线段树的核心是区间 与query(The core of segment tree is interval and query)

### https://www.acwing.com/activity/content/code/content/167568/

l,r,lmax,rmax,ans

``````#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 500010;

int n, m;
int w[N];
struct Node
{
int l, r;
int sum, lmax, rmax, tmax;
}tr[N * 4];

void pushup(Node &u, Node &l, Node &r)//直接传入本次要处理的节点和他的左右子节点 通过这样获得本次的节点信息
{
u.sum = l.sum + r.sum;
u.lmax = max(l.lmax, l.sum + r.lmax);
u.rmax = max(r.rmax, r.sum + l.rmax);
u.tmax = max(max(l.tmax, r.tmax), l.rmax + r.lmax);
}

void pushup(int u)
{
pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
}

void build(int u, int l, int r)
{
tr[u] = {l, r};
if (l == r)
{
tr[u] = {l, r, w[r], w[r], w[r], w[r]};
return ;
}
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);

}

void modify(int u, int x, int v)
{
if (tr[u].l == x && tr[u].r == x){

tr[u] = {x, x, v, v, v, v};
return ;
}

int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) modify(u << 1, x, v);
else modify(u << 1 | 1, x, v);
pushup(u);

}

Node query(int u, int l, int r)
{
if (tr[u].l >= l && tr[u].r <= r) return tr[u];

int mid = tr[u].l + tr[u].r >> 1;
if (r <= mid) return query(u << 1, l, r);
else if (l > mid) return query(u << 1 | 1, l, r);
else
{
auto left = query(u << 1, l, r);
auto right = query(u << 1 | 1, l, r);
Node res;
pushup(res, left, right);
return res;
}

}

int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
build(1, 1, n);

int k, x, y;
while (m -- )
{
scanf("%d%d%d", &k, &x, &y);
if (k == 1)
{
if (x > y) swap(x, y);
printf("%d\n", query(1, x, y).tmax);//直接返回节点 然后输出节点的信息
}
else modify(1, x, y);
}

return 0;
}

``````
————————

### https://www.acwing.com/activity/content/code/content/167568/

The information to be queried is the maximum continuous sum in the L, R interval
The maximum continuous sum is obtained by the left and right sub nodes L, r = max (L maximum field sum, R maximum sub segment sum, the maximum field sum across the middle (= the maximum field sum at the right end of the left node + the maximum field sum at the left end of the right node))
So a node needs to store
l,r,lmax,rmax,ans
Because the right Lmax needs l.sum + r.left corresponding to the left and right son nodes
So you have to maintain a sum

``````#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 500010;

int n, m;
int w[N];
struct Node
{
int l, r;
int sum, lmax, rmax, tmax;
}tr[N * 4];

void pushup(Node &u, Node &l, Node &r)//直接传入本次要处理的节点和他的左右子节点 通过这样获得本次的节点信息
{
u.sum = l.sum + r.sum;
u.lmax = max(l.lmax, l.sum + r.lmax);
u.rmax = max(r.rmax, r.sum + l.rmax);
u.tmax = max(max(l.tmax, r.tmax), l.rmax + r.lmax);
}

void pushup(int u)
{
pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
}

void build(int u, int l, int r)
{
tr[u] = {l, r};
if (l == r)
{
tr[u] = {l, r, w[r], w[r], w[r], w[r]};
return ;
}
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);

}

void modify(int u, int x, int v)
{
if (tr[u].l == x && tr[u].r == x){

tr[u] = {x, x, v, v, v, v};
return ;
}

int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) modify(u << 1, x, v);
else modify(u << 1 | 1, x, v);
pushup(u);

}

Node query(int u, int l, int r)
{
if (tr[u].l >= l && tr[u].r <= r) return tr[u];

int mid = tr[u].l + tr[u].r >> 1;
if (r <= mid) return query(u << 1, l, r);
else if (l > mid) return query(u << 1 | 1, l, r);
else
{
auto left = query(u << 1, l, r);
auto right = query(u << 1 | 1, l, r);
Node res;
pushup(res, left, right);
return res;
}

}

int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
build(1, 1, n);

int k, x, y;
while (m -- )
{
scanf("%d%d%d", &k, &x, &y);
if (k == 1)
{
if (x > y) swap(x, y);
printf("%d\n", query(1, x, y).tmax);//直接返回节点 然后输出节点的信息
}
else modify(1, x, y);
}

return 0;
}

``````