原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=3308
一:分析
lx表示以left开始的最长连续上升子序列的个数,rx表示以rx为结尾的最长连续上升子序列,mx表示在区间[ left , right ]最大的连续上升子序列。注意lx必须是以left开头,rx也必须是以right结尾。
二:AC代码
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
struct Node
{
int left, right, mid;
int lx, rx, mx;
int dist() { return right - left + 1; }
};
Node node[100005 * 4];
int a[100005];
void pushUp(int root)
{
int lChild = root * 2;
int rChild = lChild + 1;
node[root].lx = node[lChild].lx +
((node[lChild].lx == node[lChild].dist() && a[node[rChild].left] > a[node[lChild].right]) ? node[rChild].lx : 0);//注意后面的括号一定要加
node[root].rx = node[rChild].rx +
((node[rChild].rx == node[rChild].dist() && a[node[rChild].left] > a[node[lChild].right]) ? node[lChild].rx : 0);
node[root].mx = max(max(node[lChild].mx, node[rChild].mx),
a[node[rChild].left] > a[node[lChild].right] ? (node[lChild].rx + node[rChild].lx) : 0);
}
void build(int root, int l, int r)
{
node[root].left = l;
node[root].right = r;
node[root].mid = (l + r) / 2;
if (l == r)
{
node[root].lx = node[root].rx = node[root].mx = 1;
return;
}
build(root * 2, l, (l + r) / 2);
build(root * 2 + 1, (l + r) / 2 + 1, r);
pushUp(root);
}
void update(int root, int pos)
{
if (node[root].left == node[root].right)
{
node[root].lx = node[root].rx = node[root].mx = 1;
return;
}
if (pos <= node[root].mid)
update(root * 2, pos);
else
update(root * 2 + 1, pos);
pushUp(root);
}
int query(int root, int l, int r)
{
if (node[root].left == l&&node[root].right == r)
return node[root].mx;
if (r <= node[root].mid)
return query(root * 2, l, r);
else if (l > node[root].mid)
return query(root * 2 + 1, l, r);
else
{
int lSum = query(root * 2, l, node[root].mid);
int rSum = query(root * 2 + 1, node[root].mid + 1, r);
int mSum = 0;//中间
if (a[node[root * 2 + 1].left] > a[node[root * 2].right])
mSum = min(node[root * 2].rx, node[root].mid - l + 1) + min(node[root * 2 + 1].lx, r - node[root].mid);
return max(max(lSum, rSum), mSum);
}
}
int main()
{
int t;
int n, m;
int l, r;
char ch[10];
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
build(1, 0, n - 1);
while (m--)
{
scanf("%s%d%d", ch, &l, &r);
if (ch[0] == 'Q')
printf("%d\n", query(1, l, r));
else
{
a[l] = r;
update(1, l);
}
}
}
return 0;
}