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].
U表示把第A个数改成B,下标从0开始。
Q表示求区间[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).
多组输入数据
第一行两个整数n和m (n,m<= 100000)
第二行n个不超过100000的非负数
接下里m个询问,如题目描述。
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
一道线段树区间合并的题目
求上升区间
可利用区间左右端点的大小比较进行解决
#include<iostream>
const int maxn = 1e5 + 10;
int leaf[maxn];
int a[maxn];
int n, m;
int t;
using namespace std;
struct node{
int l, r;
int lsum, rsum;
int lazy, sum;
}tree[maxn * 3];
void pushup(int x){
int mid = (tree[x].l + tree[x].r) >> 1;
int a1 = a[mid];
int a2 = a[mid + 1];
if(a1 < a2){
tree[x].lsum = tree[x << 1].lsum;
if(tree[x << 1].lsum == tree[x << 1].r - tree[x << 1].l + 1) tree[x].lsum += tree[x << 1 | 1].lsum;
tree[x].rsum = tree[x << 1 | 1].rsum;
if(tree[x << 1 | 1].rsum == tree[x << 1 | 1].r - tree[x << 1 | 1].l + 1) tree[x].rsum += tree[x << 1].rsum;
tree[x].sum = max(tree[x << 1].rsum + tree[x << 1 | 1].lsum, max(tree[x << 1].sum, tree[x << 1 | 1].sum));
}
else{
tree[x].lsum = tree[x << 1].lsum;
tree[x].rsum = tree[x << 1 | 1].rsum;
tree[x].sum = max(tree[x << 1].sum, tree[x << 1 | 1].sum);
}
}
void build(int i, int l, int r){
int mid = (l + r) >> 1;
tree[i].l = l;
tree[i].r = r;
tree[i].lsum = 0;
tree[i].rsum = 0;
tree[i].sum = 0;
tree[i].lazy = -1;
if(l == r){
tree[i].lsum = 1;
tree[i].rsum = 1;
tree[i].sum = 1;
leaf[l] = i;
return ;
}
build(i << 1, l, mid);
build(i << 1 | 1, mid + 1, r);
pushup(i);
}
void update(int i, int pos, int val){
if(tree[i].l == tree[i].r){
a[pos] = val;
return ;
}
int mid = (tree[i].l + tree[i].r) >> 1;
if(pos <= mid) update(i << 1, pos, val);
else update(i << 1 | 1, pos, val);
pushup(i);
}
int query(int i, int l, int r){
if(tree[i].l >= l && tree[i].r <= r){
return tree[i].sum;
}
else{
int mid = (tree[i].l + tree[i].r) >> 1;
if(r <= mid)
return query(i << 1, l, r);
else if(l > mid)
return query(i << 1 | 1, l, r);
else{
int a1 = a[mid];
int a2 = a[mid + 1];
if(a1 < a2){
node t;
t.lsum = query(i << 1, l, mid);
t.rsum = query(i << 1 | 1, mid + 1, r);
return max(max(t.lsum, t.rsum), min(mid-l+1, tree[i<<1].rsum)+min(r-mid, tree[i<<1|1].lsum));//注意大小
}
else{
return max(query(i << 1, l, mid), query(i << 1 | 1, mid + 1, r));
}
}
}
}
int main(){
ios::sync_with_stdio(0);
cin >> t;
while(t--){
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> a[i];
build(1, 1, n);
char b;
int c, d;
for(int i = 1;i <= m; i++){
cin >> b >> c >> d;
if(b == 'Q'){
cout << query(1, c + 1, d + 1) << endl;
}
else{
update(1, c + 1, d);
}
}
}
}