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

解决农场中不同矩形区域种植不同价值蔬菜的问题,通过矩形覆盖算法计算最终收益。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


### HDU 3342 查集 解题思路与实现 #### 题目背景介绍 HDU 3342 是一道涉及查集的数据结构题目。该类问题通常用于处理动态连通性查询,即判断若干元素是否属于同一集合,支持高效的合操作。 #### 数据描述 给定一系列的人际关系网络中的朋友关系对 (A, B),表示 A 和 B 是直接的朋友。目标是通过这些已知的关系推断出所有人之间的间接友谊连接情况。具体来说,如果存在一条路径使得两个人可以通过中间人的链条相连,则认为他们是间接朋友。 #### 思路分析 为了高效解决此类问题,可以采用带按秩压缩启发式的加权快速联合-查找算法(Weighted Quick Union with Path Compression)。这种方法不仅能够有效地管理大规模数据集下的分组信息,而且可以在几乎常数时间内完成每次查找和联合操作[^1]。 当遇到一个新的友链 `(a,b)` 时: - 如果 a 和 b 已经在同一棵树下,则无需任何动作; - 否则,执行一次 `union` 操作来把它们所在的两棵不同的树合成一棵更大的树; 最终目的是统计有多少个独立的“朋友圈”,也就是森林里的树木数量减一即是所需新建桥梁的数量[^4]。 #### 实现细节 以下是 Python 版本的具体实现方式: ```python class DisjointSet: def __init__(self, n): self.parent = list(range(n)) self.rank = [0] * n def find(self, p): if self.parent[p] != p: self.parent[p] = self.find(self.parent[p]) # 路径压缩 return self.parent[p] def union(self, p, q): rootP = self.find(p) rootQ = self.find(q) if rootP == rootQ: return # 按秩合 if self.rank[rootP] > self.rank[rootQ]: self.parent[rootQ] = rootP elif self.rank[rootP] < self.rank[rootQ]: self.parent[rootP] = rootQ else: self.parent[rootQ] = rootP self.rank[rootP] += 1 def solve(): N, M = map(int, input().split()) dsu = DisjointSet(N+1) # 初始化不相交集 for _ in range(M): u, v = map(int, input().split()) dsu.union(u,v) groups = set() for i in range(1,N+1): groups.add(dsu.find(i)) bridges_needed = len(groups)-1 print(f"Bridges needed to connect all components: {bridges_needed}") solve() ``` 这段代码定义了一个名为 `DisjointSet` 的类来进行查集的操作,包括初始化、寻找根节点以及联合两个子集的功能。最后,在主函数 `solve()` 中读取输入参数对每一对好友调用 `dsu.union()` 方法直到遍历完所有的边为止。之后计算不同组件的数量从而得出所需的桥接次数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值