Uva 12299 线段树求区间最小值(RMQ) 区间查询单点更新

九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/12317079

n个点 m个操作

shift(a,b,c···) 表示把a位置的数字放在最后,其他位置的数字向前移动

 

这里shift直接视为修改单点数据

没有太多trap, 注意向上更新时 只有非叶子节点才允许此操作

线段树裸题:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#define N  101000
#define ll int
#define L(x) x<<1
#define R(x) x<<1|1
#define Mid(x,y) (x+y)>>1
using namespace std;
inline ll Min(ll a,ll b){return a>b?b:a;}
inline ll Max(ll a,ll b){return a>b?a:b;}

ll len,maxdeep,Query;
struct node{
	int l,r;
	ll sum;
}tree[N*4];
ll a[N];
void build(int l,int r,int id){
	tree[id].l = l;	tree[id].r = r;

	if(l==r) { tree[id].sum = a[l] ; return ;}
	int mid = Mid(l,r);
	build(l,mid,L(id));	 build(mid+1,r,R(id));
	tree[id].sum = Min(tree[L(id)].sum, tree[R(id)].sum);
}

void updata_point(int pos,int id, ll data){//单点更新
	if(tree[id].l == tree[id].r)
	{	tree[id].sum = data; return ; }

	int mid = Mid(tree[id].l, tree[id].r);

	if(pos <= mid) 		updata_point(pos, L(id), data);
	else				updata_point(pos , R(id), data);

	if(tree[id].l != tree[id].r)
	tree[id].sum = Min( tree[L(id)].sum , tree[R(id)].sum );

}


ll query_inter(int l,int r,int id){
	if( l == tree[id].l && tree[id].r == r)
		return tree[id].sum;

	int mid = Mid(tree[id].l , tree[id].r );
	if(r<=mid)return query_inter(l,r,L(id));
	if(mid<l)return query_inter(l,r,R(id));

	return Min( query_inter( l, mid, L(id)) , query_inter( mid+1, r, R(id)));
}
int temp[N],top,temp2[N];
void zou(){
	while(1)
	{
		int x;scanf("%d",&temp[top]);
		top++;
		char c=getchar();
		if(c==')')break;
	}
	for(int i=0;i<top;i++){
		updata_point(temp[i],1,a[temp[(i+1)%top]]);
		temp2[ temp[i] ] = a[ temp[(i+1)%top] ];
	}
	for(int i=0;i<top;i++){
		a[ temp[i] ] = temp2[ temp[i] ];
	}
}
int main(){
	int i;
	while(~scanf("%d %d",&len,&Query)){
		for(i=1;i<=len;i++)scanf("%d",&a[i]);

		build(1,len,1);
		memcpy(temp2,a,sizeof(a));

		while(Query--)
		{
			char c='0';
			while(c!='q' && c!='s')c=getchar();
			i=4;
			while(i--)c=getchar();

			if(c=='y')
			{
				int u,v; scanf("(%d,%d)",&u,&v);
				printf("%d\n",query_inter(u,v,1));
			}
			if(c=='t')
			{
				top=0;
				getchar();
				zou();
			}
		}



	}
	return 0;
}
/*
{6, 2, 4, 8, 5, 1, 4}

{6, 8, 4, 5, 4, 1, 2}

{8, 6, 4, 5, 4, 1, 2}



9 99
10000 1000 100 45 12 450 46 78 965

query(1,1)
query(1,2)
query(1,3)
shift(1,6,9)
query(1,1)
query(1,2)
query(1,3)
shift(2,5)
query(2,3)
query(1,2)
shift(4,5,7,8)
query(2,5)
shift(1,4,6,8)
shift(1,3,7,8)
query(1,9)
query(3,6)
query(7,8)
query(2,4)
query(5,6)
query(9,9)

ans:
10000
1000
100
450
450
100
12
12
12
12
45
450
12
45
10000

*/


 

你可能感兴趣的:(Uva 12299 线段树求区间最小值(RMQ) 区间查询单点更新)