Farming(扫描线体积并)

传送门 HDU3255

描述

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.

输入

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.

输出

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

样例

  • Input
    2
    1 1
    25
    0 0 10 10 1
    2 2
    5 2
    0 0 2 1 1
    1 0 3 2 2
  • Output
    Case 1: 2500
    Case 2: 16

思路

  • 枚举高度算面积并求和

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<bits/stdc++.h>
#define INIT(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson d<<1,l,mid
#define rson d<<1|1,mid+1,r
#define PB push_back
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=3e4+7;
const int mod=1e9+7;
struct Seg{
int l,r,y;
int flag;
bool operator < (const Seg& a){return y<a.y;}
};
vector<Seg> seg;
vector<int> dx;
int len[maxn<<4];
int cnt[maxn<<4];
int h[5],ph[maxn];
int px1[maxn],py1[maxn],px2[maxn],py2[maxn],n,m;
void push_up(int d,int l,int r){
if(cnt[d]) len[d]=dx[r+1]-dx[l];
else if(l==r) len[d]=0;
else len[d]=len[d<<1]+len[d<<1|1];
}
void update(int d,int l,int r,int ql,int qr,int val){
if(ql<=l&&r<=qr){
cnt[d]+=val;
push_up(d,l,r);
}
else{
int mid=(l+r)>>1;
if(ql<=mid) update(lson,ql,qr,val);
if(mid<qr) update(rson,ql,qr,val);
push_up(d,l,r);
}
}
LL solve(){
LL ans=0;
for(int i=1;i<=m;i++){
seg.clear();
for(int j=0;j<n;j++){
if(ph[j]>h[i-1]){
seg.PB((Seg){px1[j],px2[j],py1[j],1});
seg.PB((Seg){px1[j],px2[j],py2[j],-1});
}
}
sort(seg.begin(),seg.end());
for(int j=0;j<seg.size();j++){
int ql=lower_bound(dx.begin(),dx.end(),seg[j].l)-dx.begin();
int qr=lower_bound(dx.begin(),dx.end(),seg[j].r)-dx.begin()-1;
update(1,0,dx.size()-1,ql,qr,seg[j].flag);
if(j<seg.size()-1) ans+=1ll*len[1]*(seg[j+1].y-seg[j].y)*(h[i]-h[i-1]);
}
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
for(int _=1;_<=t;_++){
scanf("%d%d",&n,&m);
seg.clear();dx.clear();
INIT(cnt,0);INIT(len,0);
for(int i=1;i<=m;i++)
scanf("%d",&h[i]);

for(int i=0;i<n;i++){
scanf("%d%d%d%d%d",&px1[i],&py1[i],&px2[i],&py2[i],&ph[i]);
ph[i]=h[ph[i]];
dx.PB(px1[i]);
dx.PB(px2[i]);
}
sort(dx.begin(),dx.end());
sort(h+1,h+m+1);
dx.erase(unique(dx.begin(),dx.end()),dx.end());
printf("Case %d: %lld\n",_,solve());
}
return 0;
}
Donate comment here