这个是一个很简单很明显的线段树的区间合并,不过区间合并的题目都还是有点难写,建议存个板子。
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + ;
struct node
{
int l, r, rp, lp, len;
int max_pre, max_sub, max_last;
}tree[maxn*];
int a[maxn]; void push_up(int id)
{
tree[id].rp = tree[id<<|].rp;
tree[id].lp = tree[id << ].lp;
tree[id].max_pre = tree[id << ].max_pre;
tree[id].max_last = tree[id << | ].max_last;
tree[id].max_sub = max(tree[id << ].max_sub, tree[id << | ].max_sub);
if(tree[id<<].rp<tree[id<<|].lp)
{
if (tree[id << ].max_pre == tree[id << ].len) tree[id].max_pre += tree[id << | ].max_pre;
if (tree[id << | ].max_last == tree[id << | ].len) tree[id].max_last += tree[id << ].max_last;
tree[id].max_sub = max(tree[id].max_sub, tree[id << ].max_last + tree[id<<|].max_pre);
}
} void build(int id,int l,int r)
{
tree[id].l = l;
tree[id].r = r;
tree[id].len = r - l + ;
if(l==r)
{
tree[id].rp = tree[id].lp = a[l];
tree[id].max_pre = tree[id].max_last = tree[id].max_sub = ;
return;
}
int mid = (l+r) >> ;
build(id << , l, mid);
build(id << | , mid + , r);
push_up(id);
} int query(int id,int x,int y)
{
int ans = ;
if(x<=tree[id].l&&tree[id].r<=y)
{
return tree[id].max_sub;
}
int mid = (tree[id].l + tree[id].r) >> ;
if (x <= mid) ans = max(ans, query(id << , x, y));
if (y > mid) ans = max(ans, query(id << | , x, y));
if(tree[id<<].rp<tree[id<<|].lp)
{
ans = max(ans, min(mid - x + , tree[id << ].max_last) + min(y - mid, tree[id << | ].max_pre));
}
return ans;
} void update(int id,int x,int z)
{
if(tree[id].l==tree[id].r&&tree[id].l==x)
{
tree[id].rp = z;
tree[id].lp = z;
return;
}
int mid = (tree[id].l + tree[id].r) >> ;
if (x <= mid) update(id << , x, z);
if (x > mid) update(id << | , x, z);
push_up(id);
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m;
scanf("%d%d", &n, &m);
for(int i=;i<=n;i++)
{
scanf("%d", &a[i]);
}
build(, , n);
while(m--)
{
char s[];
int x, y;
scanf("%s %d %d", s, &x, &y);
if(s[]=='Q')
{
int ans = query(, x+, y+);
printf("%d\n", ans);
}
else update(, x+, y);
}
}
}