Farming
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1929 Accepted Submission(s): 599
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.
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 p i (1 <= p i <= 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 10 6 in their absolute values.
Each case begins with two integers n, m (1 <= n <= 30000, 1 <= m <= 3).
The next line contains m distinct positive integers p i (1 <= p i <= 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 10 6 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
题解:
若m=3,则分三次计算,首先price按升序排列,
第一次(S1+S2+S3)*price[1];
第二次(S2+S3)*(price[2]-price[1]);(price[1]的部分前面已经计算过)
第三次S3*(price[3]-price[2])(同理)
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson rt<<1
#define rson rt<<1|1
const int maxn=30007;
ll y[maxn*2],price1[5],price2[5];
ll x1[maxn],y11[maxn],x2[maxn],y2[maxn];
int s_th[maxn],t,n,m;
struct LINE
{
ll x,y_down,y_up;
int flag;
}line[maxn*2];
bool cmp(const LINE& a,const LINE& b)
{
return a.x<b.x;
}
struct TREE
{
int l,r,cover;
ll len;
}tree[(maxn*2)<<2];
void add(ll x1,ll x2,ll y1,ll y2)
{
t++;
y[t]=y1;
line[t].x=x1;
line[t].y_down=y1;
line[t].y_up=y2;
line[t].flag=1;
t++;
y[t]=y2;
line[t].x=x2;
line[t].y_down=y1;
line[t].y_up=y2;
line[t].flag=-1;
}
void build(int l,int r,int rt)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].cover=0;
tree[rt].len=0;
if(l==r)return;
int m=(l+r)>>1;
build(l,m,lson);
build(m+1,r,rson);
}
void cal(int rt)
{
int l=tree[rt].l,r=tree[rt].r;
if(tree[rt].cover)
tree[rt].len=y[r+1]-y[l];
else if(l==r)
tree[rt].len=0;
else
tree[rt].len=tree[lson].len+tree[rson].len;
}
void update(int l,int r,int rt,int flag)
{
if(l<=tree[rt].l&&r>=tree[rt].r)
{
tree[rt].cover+=flag;
cal(rt);
return;
}
int m=(tree[rt].l+tree[rt].r)>>1;
if(l<=m)update(l,r,lson,flag);
if(r>m)update(l,r,rson,flag);
cal(rt);
}
int main()
{
int T,cas=0;scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
int i,j;
for(i=1;i<=m;i++)
scanf("%lld",&price1[i]),price2[i]=price1[i];
price2[0]=0;
sort(price2,price2+m+1);
for(i=1;i<=n;i++)
scanf("%lld%lld%lld%lld%d",&x1[i],&y11[i],&x2[i],&y2[i],&s_th[i]);
ll ans=0;
for(int z=1;z<=m;z++)
{
t=0;
for(i=1;i<=n;i++)
{
if(price1[s_th[i]]>=price2[z])
{
add(x1[i],x2[i],y11[i],y2[i]);
}
}
sort(y+1,y+t+1);
sort(line+1,line+t+1,cmp);
int k=2;
for(i=2;i<=t;i++)
if(y[i]!=y[i-1])
y[k++]=y[i];
k--;
build(1,k,1);
for(i=1;i<=t;i++)
{
int a=lower_bound(y+1,y+k+1,line[i].y_down)-y;
int b=lower_bound(y+1,y+k+1,line[i].y_up)-y-1;
update(a,b,1,line[i].flag);
ans+=(price2[z]-price2[z-1])*(line[i+1].x-line[i].x)*tree[1].len;
}
}
printf("Case %d: %lld\n",++cas,ans);
}
return 0;
}