hdu3308 LCIS--区间更新 & 最长连续上升子序列

原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=3308


一:分析

lx表示以left开始的最长连续上升子序列的个数,rx表示以rx为结尾的最长连续上升子序列,mx表示在区间[ left , right ]最大的连续上升子序列。注意lx必须是以left开头,rx也必须是以right结尾。


二:AC代码

#define _CRT_SECURE_NO_DEPRECATE 

#include<iostream>
#include<cmath>
#include<algorithm>

using namespace std;

struct Node
{
	int left, right, mid;
	int lx, rx, mx;
	int dist() { return right - left + 1; }
};

Node node[100005 * 4];
int a[100005];

void pushUp(int root)
{
	int lChild = root * 2;
	int rChild = lChild + 1;

	node[root].lx = node[lChild].lx + 
		((node[lChild].lx == node[lChild].dist() && a[node[rChild].left] > a[node[lChild].right]) ? node[rChild].lx : 0);//注意后面的括号一定要加

	node[root].rx = node[rChild].rx + 
		((node[rChild].rx == node[rChild].dist() && a[node[rChild].left] > a[node[lChild].right]) ? node[lChild].rx : 0);

	node[root].mx = max(max(node[lChild].mx, node[rChild].mx), 
		a[node[rChild].left] > a[node[lChild].right] ? (node[lChild].rx + node[rChild].lx) : 0);
}

void build(int root, int l, int r)
{
	node[root].left = l;
	node[root].right = r;
	node[root].mid = (l + r) / 2;
	if (l == r)
	{
		node[root].lx = node[root].rx = node[root].mx = 1;
		return;
	}

	build(root * 2, l, (l + r) / 2);
	build(root * 2 + 1, (l + r) / 2 + 1, r);

	pushUp(root);
}

void update(int root, int pos)
{
	if (node[root].left == node[root].right)
	{
		node[root].lx = node[root].rx = node[root].mx = 1;
		return;
	}
	
	if (pos <= node[root].mid)
		update(root * 2, pos);
	else
		update(root * 2 + 1, pos);

	pushUp(root);
}

int query(int root, int l, int r)
{
	if (node[root].left == l&&node[root].right == r)
		return node[root].mx;

	if (r <= node[root].mid)
		return query(root * 2, l, r);
	else if (l > node[root].mid)
		return query(root * 2 + 1, l, r);
	else
	{
		int lSum = query(root * 2, l, node[root].mid);
		int rSum = query(root * 2 + 1, node[root].mid + 1, r);

		int mSum = 0;//中间
		if (a[node[root * 2 + 1].left] > a[node[root * 2].right])
			mSum = min(node[root * 2].rx, node[root].mid - l + 1) + min(node[root * 2 + 1].lx, r - node[root].mid);

		return max(max(lSum, rSum), mSum);
	}
}

int main()
{
	int t;
	int n, m;
	int l, r;
	char ch[10];
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; i++)
			scanf("%d", &a[i]);

		build(1, 0, n - 1);

		while (m--)
		{
			scanf("%s%d%d", ch, &l, &r);
			if (ch[0] == 'Q')
				printf("%d\n", query(1, l, r));
			else
			{
				a[l] = r;
				update(1, l);
			}
		}
	}
	
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值