Farming
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 633 Accepted Submission(s): 168
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.
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.
//呵呵,把p当成z坐标,然后就相当于求体积的并了,
//有了上道题的经验、这题就so easy 1Y,
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 60003
#define lson l,m,k<<1
#define rson m,r,k<<1|1
using namespace std;
struct node
{
int x,y1,y2,z1,z2;
int flag;
bool operator<(const node&a)const
{
return x<a.x;
}
};
struct tree
{
int cover,len;
};
node In[N];
tree st[N<<2];
int p[3];
int rcy[N];
void build(int l,int r,int k)
{
st[k].cover=st[k].len=0;
if(l+1==r)
return;
int m=(l+r)>>1;
build(lson);
build(rson);
}
void up(int &k,int &l,int &r)
{
if(st[k].cover)
{
st[k].len=rcy[r]-rcy[l];return;
}
if(l+1==r){st[k].len=0;return;}
st[k].len=st[k<<1].len+st[k<<1|1].len;
}
int flag;
void update(int &y1,int &y2,int l,int r,int k)
{
if(y1<=rcy[l]&&y2>=rcy[r])
{
st[k].cover+=flag;
up(k,l,r);
return;
}
int m=(l+r)>>1;
if(y1<rcy[m]) update(y1,y2,lson);
if(y2>rcy[m]) update(y1,y2,rson);
up(k,l,r);
}
int main()
{
int x1,y1,x2,y2,z;
int i,j,k,l;
int T,t=1,m,n;
double v,s;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
p[0]=0;
for(i=1;i<=m;i++)
scanf("%d",&p[i]);
for(j=i=0;i<n;i++)
{
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&z);
In[j].x=x1;In[j].y1=y1;In[j].y2=y2;
In[j].z1=0;In[j].z2=p[z];
rcy[j]=y1;In[j++].flag=1;
In[j].x=x2;In[j].y1=y1;In[j].y2=y2;
In[j].z1=0;In[j].z2=p[z];
rcy[j]=y2;In[j++].flag=-1;
}
sort(p,p+m+1);
sort(In,In+j);
sort(rcy,rcy+j);
for(i=1,k=0;i<j;i++)
if(rcy[i]!=rcy[k])
rcy[++k]=rcy[i];
n=--j;
v=0;
for(i=0;i<m;i++)
{
build(0,k,1);
s=0;
for(j=0;j<n;j++)
{
if(In[j].z1<=p[i]&&p[i]<In[j].z2)
{
flag=In[j].flag;
update(In[j].y1,In[j].y2,0,k,1);
for(l=j+1;l<=n;l++)
if(In[l].z1<=p[i]&&p[i]<In[l].z2)
break;
s+=1.0*st[1].len*(In[l].x-In[j].x);
}
}
v+=s*(p[i+1]-p[i]);
}
printf("Case %d: %.lf\n",t++,v);
}
return 0;
}