HDU 3308——LCIS【线段树 & 单点更新 & 区间和并 & 维护区间LIS】

题目传送门


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].


Input

T in the first line, indicating the case number.
Each case starts with two integers n , m(0<n,m<=105).
The next line has n integers(0<=val<=105).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=105)
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:用B替换第A个数。(编号从0开始)
  • Q A B:输出[a,b]中最长连续严格递增子序列(LCIS)的长度。

题解

  • 首先序列严格递增,7,7这样的序列不算递增

  • 其次本题是单点更新,所以不用PushDown,只用PushUp就行.

  • 线段树维护信息:
    mx:最长上升子序列的长度)
    lm:包含区间左端点的LIS长度
    rm:包含区间右端点的LIS长度

  • PushUp的时候

    • lm、rm直接从子节点获得即可
    • mx显然可以从左右两个子区间的最大值得到。
      还有一种可能是左右区间各取一部分,此时必须满足左区间的右端点值严格小于右区间的左端点值。
  • 疑惑:为何维护的lm、rm是左端点、右端点的LIS,为何不是左右孩子节点的LIS?


AC-Code

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <fstream>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int INF = 0x7fffffff;
const int MOD = 1e4;
const int MAXN = 1e5 + 2;

struct node {
	int left;
	int right;
	int mx;
	int lm;
	int rm;
}tree[MAXN << 2];
int n, m;
int a[MAXN];
void PushUp(int rt) {
	tree[rt].lm = tree[rt << 1].lm;
	tree[rt].rm = tree[rt << 1 | 1].rm;
	tree[rt].mx = max(tree[rt << 1].mx, tree[rt << 1 | 1].mx);
	int mid = (tree[rt].left + tree[rt].right) >> 1;
	if (a[mid] < a[mid + 1])
	{
		if (tree[rt << 1].lm == mid - tree[rt].left + 1) tree[rt].lm += tree[rt << 1 | 1].lm;
		if (tree[rt << 1 | 1].rm == tree[rt].right - mid) tree[rt].rm += tree[rt << 1].rm;
		tree[rt].mx = max(tree[rt].mx, tree[rt << 1].rm + tree[rt << 1 | 1].lm);
	}
}

void build(int rt, int l, int r) {
	tree[rt].left = l;
	tree[rt].right = r;
	if (l == r) {
		tree[rt].mx = tree[rt].lm = tree[rt].rm = 1;
		return;
	}
	int mid = (l + r) >> 1;
	build(rt << 1, l, mid);
	build(rt << 1 | 1, mid + 1, r);
	PushUp(rt);
}
void updata(int rt, int val, int p) {
	if (tree[rt].left == tree[rt].right) {
		a[p] = val;
		return;
	}
	int mid = (tree[rt].left + tree[rt].right) >> 1;
	if (p <= mid)	updata(rt << 1, val, p);
	else	updata(rt << 1 | 1, val, p);
	PushUp(rt);
}
int Query(int rt, int l, int r) {
	if (l <= tree[rt].left && tree[rt].right <= r) {
		return tree[rt].mx;
	}
	int mid = (tree[rt].left + tree[rt].right) >> 1;
	int s = 0;
	if (l <= mid)	s = max(s, Query(rt << 1, l, r));
	if (r > mid)	s = max(s, Query(rt << 1 | 1, l, r));
	if (a[mid] < a[mid + 1])
		s = max(s, min(tree[rt << 1].rm, mid - l + 1) + min(tree[rt << 1 | 1].lm, r - mid));
	return s;
}

int main() {
	ios;
	int T;
	cin >> T;
	while (T--) {
		cin >> n >> m;
		for (int i = 1; i <= n; i++)
			cin >> a[i];
		build(1, 1, n);
		while (m--) {
			char str[3];
			int x, y;
			cin >> str >> x >> y;
			if (str[0] == 'Q')
				cout << Query(1, x + 1, y + 1) << endl;
			else
				updata(1, y, x + 1);
		}

	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值