hdu1823:luck and love

Topic effect: Please write A two-dimensional data structures, support:

1) Modify a point

2) query within a maximum of one

solution:

This online solution some questions wrong 2333, the purpose of writing this is to give you a reference.

Since a two-dimensional array tree can not be resolved, then we will use a two-dimensional tree line strategy?

Of course, two-dimensional segment tree is very well written (I tune 20min)

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const double eps=1e-6;
int n,tree[205<<2][1005<<2],h,h1,h2;
double a,l,a1,a2;
char op[5];
int work(double p){return (int) (10.0*p+eps);}
void update1(int k,int x,int l,int r,int q,int val,bool flag){
	if (l==r){
		if (flag) tree[x][k]=max(tree[x][k],val);
		else tree[x][k]=max(tree[x<<1][k],tree[x<<1|1][k]);
		return ;
	}
	int mid=l+r>>1;
	if (q<=mid) update1(k<<1,x,l,mid,q,val,flag);
	else update1(k<<1|1,x,mid+1,r,q,val,flag);
	tree[x][k]=max(tree[x][k<<1],tree[x][k<<1|1]);
}
void update(int k,int l,int r,int v,int x,int y){
	if (l==r){update1(1,k,0,1000,y,v,1);return ;}
	int mid=l+r>>1;
	if (x<=mid) update(k<<1,l,mid,v,x,y);
	else update(k<<1|1,mid+1,r,v,x,y);
	update1(1,k,0,1000,y,v,0);
}
int query1(int k,int w,int l,int r,int x,int y){
	if (x<=l&&r<=y){return tree[w][k];}
	int mid=l+r>>1,ret=-1;
	if (x<=mid) ret=max(ret,query1(k<<1,w,l,mid,x,y));
	if (mid<y) ret=max(ret,query1(k<<1|1,w,mid+1,r,x,y));
	return ret;
}
int query(int k,int l,int r,int l1,int r1,int l2,int r2){
	if (l>=l1&&r<=r1){return query1(1,k,0,1000,l2,r2);}
	int mid=l+r>>1,ret=-1;
	if (l1<=mid) ret=max(ret,query(k<<1,l,mid,l1,r1,l2,r2));
	if (mid<r1) ret=max(ret,query(k<<1|1,mid+1,r,l1,r1,l2,r2));
	return ret;
}
int main(){
	memset (tree,-1,sizeof (tree));
	scanf ("%d",&n);
	while (n--){
		scanf ("%s",op);
		if (op[0]=='I'){
			scanf ("%d",&h);scanf ("%lf%lf",&a,&l);
			update(1,100,200,work(l),h,work(a));
		}
		else{
			scanf ("%d%d",&h1,&h2);
			scanf ("%lf%lf",&a1,&a2);
			if (h1>h2) swap(h1,h2);
			if (a1>a2) swap(a1,a2);
			int ans=query(1,100,200,h1,h2,work(a1),work(a2));
			if (ans<0) printf ("-1\n");
			else printf ("%.1lf\n",(double) ans*1.0/10.0);
		}
	}
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/DFTMR/p/11257319.html