hdu 3255 Farming(矩形面积并 多种矩形)

Farming

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 588    Accepted Submission(s): 151


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 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
 

Source
 

Recommend
wujianhua
 

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

题意:在一块地上种蔬菜,对于同一块地蔬菜价值高的一定是最后存活,求最后的蔬菜总值,也就是不同的矩形覆盖,有的矩形肯定在最上面。。。

分析:其实跟矩形面积并的求法没多大区别,只要开一个多维数组来统计每一种蔬菜就行,先把蔬菜按值大小排序,方便处理,然后对于一个区间,如果被一种蔬菜覆盖,那么这种蔬菜的就覆盖整个区间,还要减去覆盖这个区间大于它 的蔬菜,对于小于它的蔬菜则全部置零,要不就wa了、、、

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#define ls rt<<1
#define rs rt<<1|1
#define lson l,m,ls
#define rson m,r,rs
using namespace std;
const int mm=66666;
const int mn=mm<<2;
struct seg
{
    int x,y1,y2,c,v;
}g[mm];
int t[mn][5],sum[mn][5],p[5],q[5];
int y[mm];
int L,R,C,val,T;
void build(int n)
{
    while(n--)for(int i=0;i<T;++i)t[n][i]=sum[n][i]=0;
}
void updata(int l,int r,int rt)
{
    if(L<=y[l]&&R>=y[r])t[rt][C]+=val;
    else
    {
        int m=(l+r)>>1;
        if(L<y[m])updata(lson);
        if(R>y[m])updata(rson);
    }
    int i,j;
    for(i=T-1;i>=0;--i)
        if(t[rt][i])
        {
            sum[rt][i]=y[r]-y[l];
            for(j=i+1;j<T;++j)
                sum[rt][i]-=sum[rt][j];
            for(j=0;j<i;++j)sum[rt][j]=0;
            break;
        }
        else if(l>=r)sum[rt][i]=0;
        else sum[rt][i]=sum[ls][i]+sum[rs][i];
}
bool cmp(seg a,seg b)
{
    return a.x<b.x;
}
int main()
{
    int i,j,k,n,m,t,cs=0;
    __int64 ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&T);
        for(i=0;i<T;++i)scanf("%d",&p[i]),y[i]=i;
        for(i=0;i<T;++i)
            for(j=i+1;j<T;++j)
                if(p[i]>p[j])swap(y[i],y[j]),swap(p[i],p[j]);
        for(i=0;i<T;++i)q[y[i]]=i;
        for(i=0;i<n;++i)
        {
            scanf("%d%d%d%d%d",&g[i].x,&y[i],&g[i+n].x,&y[i+n],&g[i].c);
            g[i+n].c=g[i].c=q[g[i].c-1];
            g[i].y1=y[i],g[i].y2=y[i+n],g[i].v=1;
            g[i+n].y1=y[i],g[i+n].y2=y[i+n],g[i+n].v=-1;
        }
        sort(y,y+n+n);
        sort(g,g+n+n,cmp);
        for(m=i=0;i<n+n;++i)
            if(y[m]<y[i])y[++m]=y[i];
        for(ans=i=0;i<n+n;++i)
        {
            L=g[i].y1,R=g[i].y2,C=g[i].c,val=g[i].v;
            updata(0,m,1);
            if(g[i].x<g[i+1].x)
                for(j=0;j<T;++j)
                    ans+=(__int64)(g[i+1].x-g[i].x)*(__int64)sum[1][j]*(__int64)p[j];
        }
        printf("Case %d: %I64d\n",++cs,ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值