题目:
Description
Given n integers.
You have two operations:
U A B: replace the Ath number by B. (index counting from 0)
Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b].
You have two operations:
U A B: replace the Ath number by B. (index counting from 0)
Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b].
Input
T in the first line, indicating the case number.
Each case starts with two integers n , m(0<n,m<=10 5).
The next line has n integers(0<=val<=10 5).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=10 5)
OR
Q A B(0<=A<=B< n).
Each case starts with two integers n , m(0<n,m<=10 5).
The next line has n integers(0<=val<=10 5).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=10 5)
OR
Q A B(0<=A<=B< n).
Output
For each Q, output the answer.
Sample Input
1 10 10 7 7 3 3 5 9 9 8 1 8 Q 6 6 U 3 4 Q 0 1 Q 0 5 Q 4 7 Q 3 5 Q 0 2 Q 4 6 U 6 10 Q 0 9
Sample Output
1 1 4 2 3 1 2 5
题意:
给你n个数,每次有两种操作
U A B 把第A个数变成B(注意是从0开始)
Q A B 求区间【A,B】最长连续递增区间
思路:线段树,既然我们要求最长的递增序列,那我们就要把每个区间的递增序列的长度保留下来。定义一个max来存。
然后考虑到两个区间组成一个新的区间的时候可能还可以进行合并,那么我们就要把每个区间边缘的数字(lnum,rnum)记录下来,并且区间边缘递增的长度(lmax,rmax)记录下来
当合并的时候新的区间的max 就是 左孩子区间的max,右孩子区间的max,如何左右孩子区间的可以合并(就是左孩子区间的右端和右孩子区间的左端是连续递增的),就还要把他们两端的长度加起来,三者中最大的就是新的区间的max。
每次同理也要更新 (lnum,rnum) lnum就是左孩子区间的lnum,rnum就是右孩子区间的rnum
更新(lmax,rmax ) 如果一个区间不是完全递增的,那么lmax就是左孩子区间的lmax ,rmax就是右孩子区间的rmax, 如果区间完全递增就要加上另外一个区间的lmax或rmax
查询的时候,如果刚好就是线段数的一个区间 就是该区间的max
否则 就是分成的左区间,右区间,以及可能合并的,三者的最大值。
代码(代码巨丑,就不要吐槽了):
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
#define mido int mid=(tree[index].l+tree[index].r)/2;
struct node {
int l;
int r;
int max;
int lnum;
int rnum;
int lmax;
int rmax;
};
int max(int a, int b) {
if (a > b) return a;
return b;
}
node tree[4 * 100005];
void build(int index, int l, int r) {
tree[index].l = l;
tree[index].r = r;
mido;
if (l == r) {
int num;
scanf("%d", &num);
tree[index].max = 1;
tree[index].lnum = num;
tree[index].rnum = num;
tree[index].lmax = 1;
tree[index].rmax = 1;
return;
}
build(index * 2, l, mid);
build(index * 2 + 1, mid + 1, r);
if (tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
tree[index].max = max(tree[index * 2].max, max(tree[index * 2 + 1].max, tree[index * 2].rmax + tree[index * 2 + 1].lmax));
}
else {
tree[index].max = max(tree[index * 2].max, tree[index * 2 + 1].max);
}
tree[index].lnum = tree[index * 2].lnum;
tree[index].rnum = tree[index * 2 + 1].rnum;
if (tree[index * 2].lmax == tree[index * 2].r - tree[index * 2].l + 1 && tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
tree[index].lmax = tree[index * 2].lmax + tree[index * 2 + 1].lmax;
}
else tree[index].lmax = tree[index * 2].lmax;
if (tree[index * 2 + 1].rmax == tree[index * 2 + 1].r - tree[index * 2 + 1].l + 1 && tree[index * 2 + 1].lnum>tree[index * 2].rnum) {
tree[index].rmax = tree[index * 2 + 1].rmax + tree[index * 2].rmax;
}
else tree[index].rmax = tree[index * 2 + 1].rmax;
}
void update(int index,int l, int r, int q) {
if (tree[index].l == l&&tree[index].r == r) {
tree[index].lnum = q;
tree[index].rnum = q;
return;
}
mido;
if (r <= mid) {
update(index * 2, l, r, q);
}
else if (l>mid) {
update(index * 2 + 1, l, r, q);
}
else {
update(index * 2, l, mid, q);
update(index * 2 + 1, mid + 1, r, q);
}
if (tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
tree[index].max = max(tree[index * 2].max, max(tree[index * 2 + 1].max, tree[index * 2].rmax + tree[index * 2 + 1].lmax));
}
else {
tree[index].max = max(tree[index * 2].max, tree[index * 2 + 1].max);
}
tree[index].lnum = tree[index * 2].lnum;
tree[index].rnum = tree[index * 2 + 1].rnum;
if (tree[index * 2].lmax == tree[index * 2].r - tree[index * 2].l + 1 && tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
tree[index].lmax = tree[index * 2].lmax + tree[index * 2 + 1].lmax;
}
else tree[index].lmax = tree[index * 2].lmax;
if (tree[index * 2 + 1].rmax == tree[index * 2 + 1].r - tree[index * 2 + 1].l + 1 && tree[index * 2 + 1].lnum>tree[index * 2].rnum) {
tree[index].rmax = tree[index * 2 + 1].rmax + tree[index * 2].rmax;
}
else tree[index].rmax = tree[index * 2 + 1].rmax;
}
int query(int index, int a, int b) {
if (tree[index].l == a&&tree[index].r == b) {
return tree[index].max;
}
mido;
if (b <= mid) {
query(index * 2, a, b);
}
else if (a>mid) {
query(index * 2 + 1, a, b);
}
else {
int t1,t2,t3;
if (tree[index * 2].rnum < tree[index * 2 + 1].lnum) {
if (tree[index * 2].r - a + 1 >= tree[index * 2].rmax&&b - tree[index * 2 + 1].l+1 >= tree[index * 2 + 1].lmax) {
t1 = tree[index * 2].rmax + tree[index * 2 + 1].lmax;
}
else if (tree[index * 2].r - a + 1 < tree[index * 2].rmax&&b - tree[index * 2 + 1].l+1 >= tree[index * 2 + 1].lmax) {
t1= tree[index * 2].r - a + 1+ tree[index * 2 + 1].lmax;
}
else if (tree[index * 2].r - a + 1 >= tree[index * 2].rmax&&b - tree[index * 2 + 1].l+1 < tree[index * 2 + 1].lmax) {
t1 = tree[index * 2].rmax + b - tree[index * 2 + 1].l + 1;
}
else {
t1= tree[index * 2].r - a + 1 + b - tree[index * 2 + 1].l + 1;
}
t2 = query(index*2, a, mid);
t3 = query(index * 2 + 1, mid + 1, b);
return max(t1, max(t2, t3));
}
else {
int t1, t2;
t1= query(index * 2, a, mid);
t2 = query(index * 2 + 1, mid + 1, b);
return max(t1, t2);
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n,m;
scanf("%d%d", &n, &m);
build(1, 1, n);
while (m--) {
char oper[2];
int a, b;
scanf("%s%d%d", oper, &a, &b);
if (strcmp(oper, "U") == 0) {
update(1, a+1, a+1, b);
}
else {
printf("%d\n", query(1, a + 1, b + 1));
}
}
}
return 0;
}