uva12299 RMQ with Shifts

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3720

采用notonlysuccess的风格 博客链接:http://www.notonlysuccess.com/index.php/segment-tree-complete/

单点更新

刚开始我全部移位后更新整个树 就超时了 后来考虑的只用更新那一点就够了(自下而上)

但是  a[root]=MIN(a[root<<1],a[root<<1|1]);  我直接写成 if(a[rt]<a[root]) a[root]=a[rt];  wa无数 考

#include<cstdio>
#include<cstring>
#include<cstdlib>

#define MIN(a,b) (a>b?b:a)
#define maxn 100005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define INF 99999999
int a[maxn<<2];
int arr[maxn];
int d;
int num[35];
char op[35];

void Push_Up(int rt)
{
	a[rt]=MIN(a[rt<<1],a[rt<<1|1]);
}

void build(int l,int r,int rt)
{
	if(l==r)
	{
		scanf("%d",&a[rt]);
		arr[++d]=rt;
		return;
	}
	int m=(l+r)>>1;
	build(lson);
	build(rson);
	Push_Up(rt);
}

int query(int L,int R,int l,int r,int rt)
{
	if(L<=l&&r<=R)
		return a[rt];
	int m=(l+r)>>1;
	int t1,t2;
	t1=t2=INF;
	if(L<=m)
		t1=query(L,R,lson);
	if(R>m)
		t2=query(L,R,rson);
	return MIN(t1,t2);
}

void updata(int rt)
{
	if(rt==1)
		return;
	int root=rt>>1;
	a[root]=MIN(a[root<<1],a[root<<1|1]);
	updata(root);
}

int main()
{
	int n,q;
	d=0;
	scanf("%d%d",&n,&q);
	build(1,n,1);
	while(q--)
	{
		memset(num,0,sizeof(num));
		scanf("%s",op);
		if(op[0]=='q')
		{
			int len=strlen(op);
			int cn=0;
			for(int i=6;i<len-1;i++)
			{
				if(op[i]==',')
					cn++;
				else
					num[cn]=num[cn]*10+op[i]-'0';
			}
			printf("%d\n",query(num[0],num[1],1,n,1));
		}
		else
		{
			int len=strlen(op);
			int cn=1;
			for(int i=6;i<len-1;i++)
			{
				if(op[i]==',')
					cn++;
				else
					num[cn]=num[cn]*10+op[i]-'0';
			}
			int ct=a[arr[num[1]]];
			for(int i=1;i<cn;i++)
			{
				a[arr[num[i]]]=a[arr[num[i+1]]];
				updata(arr[num[i]]);
			}
			a[arr[num[cn]]]=ct;
			updata(arr[num[cn]]);
		}
	}
//	system("pause");
	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值