【hdu1823】【二維線段樹】Luck and Love

這道題是一道二維線段樹的裸題。

由於浮點數最多一位,所以我們可以先轉化成整數,輸出時除以10;另外當兩個人身高和活潑度都相等時,要取魅力值大的一個,所以要取最大而不能直接賦值。

代碼:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn1 = 100 + 10;
const int maxn2 = 1000 + 10;
const int c1 = 100,c2 = 1000;
int Max[maxn1<<2][maxn2<<2];
int n;
void init()
{
	freopen("hdu1823.in","r",stdin);
	freopen("hdu1823.out","w",stdout);
}

void pushup(int rt1,int rt2)
{
	Max[rt1][rt2] = max(Max[rt1][rt2<<1],Max[rt1][rt2<<1|1]);
}

void update2(int p,int c,int l,int r,int rt1,int rt2)
{
	if(l == p && r == p)
	{
		Max[rt1][rt2] = max(Max[rt1][rt2],c);
		return;
	}
	int m = (l + r) >> 1;
	if(p <= m)update2(p,c,l,m,rt1,rt2 << 1);
	else update2(p,c,m + 1,r,rt1,rt2 << 1 | 1);
	pushup(rt1,rt2);
}

void update1(int h,int p,int c,int l,int r,int rt)
{
	update2(p,c,0,c2,rt,1);
	if(l == h && r == h)return;
	int m = (l + r) >> 1;
	if(h <= m)update1(h,p,c,l,m,rt << 1);
	else update1(h,p,c,m + 1,r,rt << 1 | 1);
}

int query2(int p1,int p2,int l,int r,int rt1,int rt2)
{
	if(p1 <= l && r <= p2)return Max[rt1][rt2];
	int m = (l + r) >> 1;
	int res = -1;
	if(p1 <= m)res = max(res,query2(p1,p2,l,m,rt1,rt2 << 1));
	if(p2 > m)res = max(res,query2(p1,p2,m + 1,r,rt1,rt2 << 1 | 1));
	return res;
}

int query1(int h1,int h2,int p1,int p2,int l,int r,int rt)
{
	if(h1 <= l && r <= h2)return query2(p1,p2,0,c2,rt,1);
	int m = (l + r) >> 1;
	int res = -1;
	if(h1 <= m)res = max(res,query1(h1,h2,p1,p2,l,m,rt << 1));
	if(h2 > m)res = max(res,query1(h1,h2,p1,p2,m + 1,r,rt << 1 | 1));
	return res;
}

void readdata()
{
	while(~scanf("%d",&n))
	{
		memset(Max,-1,sizeof(Max));
		for(int i = 1;i <= n;i++)
		{
			char op[2];
			scanf("%s",op);
			if(op[0] == 'I')
			{
				int h;
				double p,num;
				scanf("%d%lf%lf",&h,&p,&num);
				update1(h,(int)(p * 10),(int)(num * 10),c1,c1 + 100,1);
			}
			else
			{
				int h1,h2;
				double p1,p2;
				scanf("%d%d%lf%lf",&h1,&h2,&p1,&p2);
				if(h1 > h2)swap(h1,h2);if(p1 > p2)swap(p1,p2);
				int res = query1(h1,h2,(int)(p1 * 10),(int)(p2 * 10),c1,c1 + 100,1);
				if(res < 0)printf("-1\n");
				else printf("%.1lf\n",(double)res / 10.0);
			}
		}
	}
}

int main()
{
	init();
	readdata();
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章