HDU 3308 - LCIS(线段树+区间合并)

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<=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


题意:求某区间内的最长连续上升子序列。

WA了一个下午。。。做出来了真开心


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 1e5 + 10;
int maxv[maxn << 2], lv[maxn << 2], rv[maxn << 2];
int a[maxn];
void PushUp(int rt, int l, int r)
{
	maxv[rt] = max(maxv[rt << 1], maxv[rt << 1 | 1]);
	lv[rt] = lv[rt << 1];
	rv[rt] = rv[rt << 1 | 1];
	int m = (l + r) >> 1;

	if (a[m + 1] > a[m])
	if (maxv[rt] <= rv[rt << 1] + lv[rt << 1 | 1])
	{
		maxv[rt] = rv[rt << 1] + lv[rt << 1 | 1];
		if (lv[rt << 1] == m - l + 1)
			lv[rt] = lv[rt << 1] + lv[rt << 1 | 1];
		if (rv[rt << 1 | 1] == r - m)
			rv[rt] = rv[rt << 1] + rv[rt << 1 | 1];
	}
}

void build(int l, int r, int rt)
{
	if (l == r)
	{
		maxv[rt] = rv[rt] = lv[rt] = 1;
		return;
	}
	int m = (l + r) >> 1;
	build(lson);
	build(rson);
	PushUp(rt, l, r);
}

int query(int L, int R, int l, int r, int rt)
{
	if (L <= l&&r <= R)
	{
		return maxv[rt];
	}
	int m = (l + r) >> 1;
	int ret = 0;
	if (R <= m) ret = max(ret, query(L, R, lson));
	else if (L > m) ret = max(ret, query(L, R, rson));
	else
	{
		int lx = query(L, m, lson);
		int rx = query(m + 1, R, rson);
		int tmp = 0;
		if (a[m]<a[m + 1])
			tmp = min(m - L + 1, rv[rt << 1]) + min(R - m, lv[rt << 1 | 1]);
		ret = max(max(lx, rx), tmp);
	}
	return ret;
}

void update(int o, int l, int r, int rt)
{
	if (l == r)
		return;
	int m = (l + r) >> 1;
	if (o <= m) update(o, lson);
	else update(o, rson);
	PushUp(rt, l, r);
}

int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int n, m;
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; i++)
			scanf("%d", &a[i]);
		build(0, n - 1, 1);
		while (m--)
		{
			char op[2];
			int u, v;
			scanf("%s%d%d", op, &u, &v);
			if (op[0] == 'Q')
			{
				int maxx = query(u, v, 0, n - 1, 1);
				printf("%d\n", maxx);
			}
			else if (op[0] == 'U')
			{
				a[u] = v;
				update(u, 0, n - 1, 1);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值