Problem 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).
The next line has n integers(0<=val<=10).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=10)
OR
Q A B(0<=A<=B< n).
Each case starts with two integers n , m(0<n,m<=10).
The next line has n integers(0<=val<=10).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=10)
OR
Q A B(0<=A<=B< n).
Output
For each Q, output the answer.
题目大意:给n个数,动态地修改某个数的值,或者查询一段区间的LCIS(最长连续上升子序列,坑爹的标题……)。
思路:线段树,每个点维护一个区间的从左边开始的最长的LCIS,从右边开始的最长的LCIS,这个区间最大的LCIS。然后随便搞搞就能过了……
代码(593MS):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = << ; int lmax[MAXN], rmax[MAXN], mmax[MAXN];
int a[MAXN], n, m, T; void update(int x, int l, int r, int pos, int val) {
if(pos <= l && r <= pos) {
a[pos] = val;
} else {
int ll = x << , rr = ll | , mid = (l + r) >> ;
if(pos <= mid) update(ll, l, mid, pos, val);
if(mid < pos) update(rr, mid + , r, pos, val);
if(a[mid] < a[mid + ]) {
lmax[x] = lmax[ll] + (lmax[ll] == mid - l + ) * lmax[rr];
rmax[x] = rmax[rr] + (rmax[rr] == r - mid) * rmax[ll];
mmax[x] = max(rmax[ll] + lmax[rr], max(mmax[ll], mmax[rr]));
} else {
lmax[x] = lmax[ll];
rmax[x] = rmax[rr];
mmax[x] = max(mmax[ll], mmax[rr]);
}
}
} int query(int x, int l, int r, int aa, int bb) {
if(aa <= l && r <= bb) {
return mmax[x];
} else {
int ll = x << , rr = ll | , mid = (l + r) >> ;
int ans = ;
if(aa <= mid) ans = max(ans, query(ll, l, mid, aa, bb));
if(mid < bb) ans = max(ans, query(rr, mid + , r, aa, bb));
if(a[mid] < a[mid + ]) ans = max(ans, min(rmax[ll], mid - aa + ) + min(lmax[rr], bb - mid));
return ans;
}
} void build(int x, int l, int r) {
if(l == r) {
lmax[x] = rmax[x] = mmax[x] = ;
} else {
int ll = x << , rr = ll | , mid = (l + r) >> ;
build(ll, l, mid);
build(rr, mid + , r);
if(a[mid] < a[mid + ]) {
lmax[x] = lmax[ll] + (lmax[ll] == mid - l + ) * lmax[rr];
rmax[x] = rmax[rr] + (rmax[rr] == r - mid) * rmax[ll];
mmax[x] = max(rmax[ll] + lmax[rr], max(mmax[ll], mmax[rr]));
} else {
lmax[x] = lmax[ll];
rmax[x] = rmax[rr];
mmax[x] = max(mmax[ll], mmax[rr]);
}
}
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
for(int i = ; i < n; ++i) scanf("%d", &a[i]);
build(, , n - );
while(m--) {
char c;
int a, b;
scanf(" %c%d%d", &c, &a, &b);
if(c == 'Q') printf("%d\n", query(, , n - , a, b));
else update(, , n - , a, b);
}
}
}