【线段树】 HDOJ 3308 LCIS

要保存很多信息的线段树,我写的线段树保存了超多的信息,而且pushup写了两遍。。。。有一种比较简单的方法是直接放弃结构体,用数组保存区间的一个端点和区间长度,因为区间长度需要用到很多次,如果选择保存区间的两个端点,那么代码会写的很长很难受。。。。我就是用结构题写的,保存的是区间两个端点,后来看了网上别人发的题解,发现用数组保存比较方便。。。我的代码比较挫。。。


#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <climits>
#define maxn 400005
#define eps 1e-6
#define mod 1000000007
#define INF 99999999
#define lowbit(x) (x&(-x))
typedef long long LL;
using namespace std;

struct node
{
	int ll, rr, l1, r1, l2, r2;
	int ansl, ansr;
}t[maxn];
char s[10];
int num[maxn];
int n, m;
int qr, ql, u, p;

void pushup(int o)
{
	int tmp1, tmp2, tmp3;
	tmp2=t[2*o].rr-t[2*o].ll+1;
	tmp3=t[2*o+1].rr-t[2*o+1].ll+1;
	if(num[t[2*o].r2]<num[t[2*o+1].l1]) tmp1=t[2*o+1].r1-t[2*o].l2+1;
	else tmp1=0;
	if(tmp1>=tmp2 && tmp1>=tmp3) t[o].ll=t[2*o].l2, t[o].rr=t[2*o+1].r1;
	else if(tmp2>tmp3) t[o].ll=t[2*o].ll, t[o].rr=t[2*o].rr;
	else t[o].ll=t[2*o+1].ll, t[o].rr=t[2*o+1].rr;
	if(t[2*o].l1==t[o].ll) t[o].l1=t[o].ll, t[o].r1=t[o].rr;
	else t[o].l1=t[2*o].l1, t[o].r1=t[2*o].r1;
	if(t[2*o+1].r2==t[o].rr) t[o].l2=t[o].ll, t[o].r2=t[o].rr;
	else t[o].l2=t[2*o+1].l2, t[o].r2=t[2*o+1].r2;
}
void build(int o, int L, int R)
{
	if(L==R){
		t[o].ansl=t[o].ansr=0;
		t[o].ll=t[o].rr=t[o].l1=t[o].l2=t[o].r1=t[o].r2=L;
		return;
	}
	int mid=(L+R)/2;
	build(2*o, L, mid);
	build(2*o+1, mid+1, R);
	pushup(o);
}
void _pushup(int o)
{
	int tmp1;
	if(num[t[2*o+1].l1]>num[t[2*o].r2]) tmp1=(t[2*o+1].r1<=qr ? t[2*o+1].r1 : qr)-(t[2*o].l2>=ql ? t[2*o].l2 : ql)+1;
	else tmp1=0;
	int tmp2=t[2*o].ansr-t[2*o].ansl+1;
	int tmp3=t[2*o+1].ansr-t[2*o+1].ansl+1;
	if(tmp1>=tmp2 && tmp1>=tmp3){
		t[o].ansr=t[2*o+1].r1<=qr ? t[2*o+1].r1 : qr;
		t[o].ansl=t[2*o].l2>=ql ? t[2*o].l2 : ql;
	}
	else if(tmp2>tmp3) t[o].ansr=t[2*o].ansr, t[o].ansl=t[2*o].ansl;
	else t[o].ansr=t[2*o+1].ansr, t[o].ansl=t[2*o+1].ansl;
	t[2*o].ansl=t[2*o].ansr=0;
	t[2*o+1].ansl=t[2*o+1].ansr=0;
}
void query(int o, int L, int R)
{
	if(ql<=L && qr>=R){
		t[o].ansl=t[o].ll;
		t[o].ansr=t[o].rr;
		return;
	}
	int mid=(L+R)/2;
	if(ql<=mid) query(2*o, L, mid);
	if(qr>mid) query(2*o+1, mid+1, R);
	_pushup(o);
}
void updata(int o, int L, int R)
{
	if(L==R) return;
	int mid=(L+R)/2;
	if(mid>=u) updata(2*o, L, mid);
	else updata(2*o+1, mid+1, R);
	pushup(o);
}
void solve(void)
{
	while(m--){
		scanf("%s",s);
		if(s[0]=='Q'){
			scanf("%d%d",&ql,&qr);
			ql++, qr++;
			query(1, 1, n);
			printf("%d\n", t[1].ansr-t[1].ansl+1);
		}
		else{
			scanf("%d%d",&u,&p);
			u++;
			num[u]=p;
			updata(1, 1, n);
		}
	}
}
int main(void)
{
	int _;
	while(scanf("%d",&_)!=EOF){
		while(_--){
			scanf("%d%d",&n,&m);
			for(int i=1;i<=n;i++) scanf("%d",&num[i]);
			build(1, 1, n);
			solve();
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值