HDU 3255 Farming(線段樹求體積並)

http://acm.hdu.edu.cn/showproblem.php?pid=3255

Farming
Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1226    Accepted Submission(s): 354


Problem Description
You have a big farm, and you want to grow vegetables in it. You're too lazy to seed the seeds yourself, so you've hired n people to do the job for you.
Each person works in a rectangular piece of land, seeding one seed in one unit square. The working areas of different people may overlap, so one unit square can be seeded several times. However, due to limited space, different seeds in one square fight each other -- finally, the most powerful seed wins. If there are several "most powerful" seeds, one of them win (it does not matter which one wins).

There are m kinds of seeds. Different seeds grow up into different vegetables and sells for different prices. 
As a rule, more powerful seeds always grow up into more expensive vegetables.
Your task is to calculate how much money will you get, by selling all the vegetables in the whole farm.
 

Input
The first line contains a single integer T (T <= 10), the number of test cases. 
Each case begins with two integers n, m (1 <= n <= 30000, 1 <= m <= 3).
The next line contains m distinct positive integers pi (1 <= pi <= 100), the prices of each kind of vegetable. 
The vegetables (and their corresponding seeds) are numbered 1 to m in the order they appear in the input. 
Each of the following n lines contains five integers x1, y1, x2, y2, s, indicating a working seeded a rectangular area with lower-left corner (x1,y1), upper-right corner (x2,y2), with the s-th kind of seed.
All of x1, y1, x2, y2 will be no larger than 106 in their absolute values.
 

Output
For each test case, print the case number and your final income.
 

Sample Input
2 1 1 25 0 0 10 10 1 2 2 5 2 0 0 2 1 1 1 0 3 2 2
 

Sample Output
Case 1: 2500 Case 2: 16
 

Source
 


你家種一年地的收成暴int!你當是《日人民報》啊!

題意:

僱傭貧農種地(所以是地主嘍),每個農民帶一種種子,播種在他管理矩形區域,每個單位面積種一粒,區域可能會重合,那麼收成高的那粒會將其他的覆蓋掉,求最終收成。

分析:

一開始想的是二維線段樹set,但是看到面積有sqr(10^6),即使離散化也有sqr(3*10^4),後來想到加權的線段樹掃描線求面積並,但是發現覆蓋種子不好維護,後來看到別人題解標題:線段樹求體積並。

把價格看成高度,那麼每粒種子的高度區間就可以看成[0,price),想像一下,這些"長方體"的形狀一定是"類金字塔"形的,也就是空間中每一個微元一定是"接地的",那麼我們只要對價格排序,然後用類似微積分的思想(不會的下學期自覺重修去)求出各部分的體積(最多3次求面積並,面積×高度/價格差)即可。


/*
 *
 *	Author	:	fcbruce
 *
 *	Date	:	2014-08-27 14:14:21 
 *
 */
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10

#ifdef _WIN32
	#define lld "%I64d"
#else
	#define lld "%lld"
#endif

#define maxm 
#define maxn 30303

using namespace std;

struct __seed
{
	int x1,y1,x2,y2,price;
}seed[maxn];

struct __edge
{
	int x1,x2,y,type;
	bool operator < (const __edge &e)const
	{
		return y<e.y;
	}
}edge[maxn<<1];

int price[4];

int X[maxn<<1];

int add[maxn<<3],sum[maxn<<3];

inline void pushup(int k,int l,int r)
{
	if (add[k]>0)
		sum[k]=X[r]-X[l];
	else if (r-l==1)
		sum[k]=0;
	else
		sum[k]=sum[k*2+1]+sum[k*2+2];
}

void update(int a,int b,int v,itn k,int l,int r)
{
	if (b<=l || r<=a)	return ;
	if (a<=l && r<=b)
	{
		add[k]+=v;
	}
	else
	{
		update(a,b,v,k*2+1,l,l+r>>1);
		update(a,b,v,k*2+2,l+r>>1,r);
	}
	
	pushup(k,l,r);
}


int main()
{
	#ifndef ONLINE_JUDGE
		freopen("/home/fcbruce/文檔/code/t","r",stdin);
	#endif // ONLINE_JUDGE
	
	int n,m,T_T;
	scanf( "%d",&T_T);
	
	for (int __=1;__<=T_T;__++)
	{
		scanf( "%d%d",&n,&m);
		price[0]=0;
		for (int i=1;i<=m;i++)
			scanf( "%d",price+i);
			
		for (int i=0,x1,y1,x2,y2,type;i<n;i++)
		{
			scanf( "%d%d%d%d%d",&x1,&y1,&x2,&y2,&type);
			seed[i]=(__seed){x1,y1,x2,y2,price[type]};
		}
		
		sort(price+1,price+m+1);

		LL income=0;
		
		for (int i=1;i<=m;i++)
		{
			int cnt=0;
			
			for (int j=0;j<n;j++)
			{
				if (seed[j].price>=price[i])
				{
					edge[cnt]=(__edge){seed[j].x1,seed[j].x2,seed[j].y1,1};
					X[cnt++]=seed[j].x1;
					edge[cnt]=(__edge){seed[j].x1,seed[j].x2,seed[j].y2,-1};
					X[cnt++]=seed[j].x2;
				}
			}
			
			sort(edge,edge+cnt);
			sort(X,X+cnt);
			int xn=unique(X,X+cnt)-X;
			
			memset(sum,0,sizeof sum);
			memset(add,0,sizeof add);
			LL area=0;
			
			for (int j=0;j<cnt;j++)
			{
				if (j) area+=(LL)(edge[j].y-edge[j-1].y)*sum[0];
				int a=lower_bound(X,X+xn,edge[j].x1)-X;
				int b=lower_bound(X,X+xn,edge[j].x2)-X;
				update(a,b,edge[j].type,0,0,xn+1);
			}
			
			income+=(LL)(price[i]-price[i-1])*area;
		}
		
		printf( "Case %d: ",__);
		printf( lld "\n",income);
		
	}
	
	return 0;
}



發佈了119 篇原創文章 · 獲贊 10 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章