HDU 3308 LCIS

LCIS

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3810    Accepted Submission(s): 1724


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

Author
shǎ崽
 

Source
解题思路:线段树单点修改区间合并
#include <iostream>
#include <cstdio>
#include <cstring>
#define Maxn 100005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
struct
{
	int lnum;
	int rnum;
	int lsum;
	int rsum;
	int msum;
}tree[Maxn<<2];
void push_up(int rt,int d)
{
	tree[rt].lnum=tree[rt<<1].lnum;
	tree[rt].rnum=tree[rt<<1|1].rnum;
	tree[rt].lsum=tree[rt<<1].lsum;
	tree[rt].rsum=tree[rt<<1|1].rsum;
	if(tree[rt<<1].rnum<tree[rt<<1|1].lnum&&tree[rt].lsum==d-(d>>1))
		tree[rt].lsum+=tree[rt<<1|1].lsum;
	if(tree[rt<<1].rnum<tree[rt<<1|1].lnum&&tree[rt].rsum==(d>>1))
		tree[rt].rsum+=tree[rt<<1].rsum;
	if(tree[rt<<1].rnum<tree[rt<<1|1].lnum)
		tree[rt].msum=max(tree[rt<<1].rsum+tree[rt<<1|1].lsum,max(tree[rt<<1].msum,tree[rt<<1|1].msum));
	else
		tree[rt].msum=max(tree[rt<<1].msum,tree[rt<<1|1].msum);
}
void build(int l,int r,int rt)
{
	if(l==r)
	{
		scanf("%d",&tree[rt].lnum);
		tree[rt].rnum=tree[rt].lnum;
		tree[rt].lsum=tree[rt].rsum=tree[rt].msum=1;
		return ;
	}
	int m=(l+r)>>1;
	build(lson);
	build(rson);
	push_up(rt,r-l+1);
}
void update(int pos,int x,int l,int r,int rt)
{
	if(l==r)
	{
		tree[rt].lnum=tree[rt].rnum=x;
		return ;
	}
	int m=(l+r)>>1;
	if(pos<=m)
		update(pos,x,lson);
	else
		update(pos,x,rson);
	push_up(rt,r-l+1);
}
int query(int L,int R,int l,int r,int rt)
{
	int ans=0;
	if(L<=l&&R>=r)
		return ans=tree[rt].msum;
	int m=(l+r)>>1;
	if(L<=m)
		ans=max(ans,query(L,R,lson));
	if(R>m)
		ans=max(ans,query(L,R,rson));
	if(tree[rt<<1].rnum<tree[rt<<1|1].lnum&&L<=m&&R>m)
		ans=max(ans,min(R,m+tree[rt<<1|1].lsum)-max(L,m-tree[rt<<1].rsum+1)+1);
	return ans;	
}
int main()
{
	int t,n,m,a,b;
	char str[3];
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		build(1,n,1);
		while(m--)
		{
			scanf("%s%d%d",str,&a,&b);
			if(str[0]=='Q')
				printf("%d\n",query(a+1,b+1,1,n,1));
			else
				update(a+1,b,1,n,1);
		}
	}
	return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值