Farming
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1375 Accepted Submission(s): 402
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 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.
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.
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
题目意思:
给n块矩形田地,每块田地上种编号为id的庄稼。每种庄稼有不同的价值和竞争力,价值越高竞争力越高,每个单位的田地只能存活竞争力最高的庄稼,求最终总价值为多少。
思路:
总价值=面积*该田地上庄稼的价值,若总价值=体积,该田地上庄稼的价值=高,那么问题就转换为求n块立方体的总体积(覆盖二次或二次以上仅算一次),其中立方体的高即为初始田地所种的庄稼的价值。
而立方体体积并即为扫描线经典模型。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <set> 9 using namespace std; 10 11 #define N 30005 12 #define ll root<<1 13 #define rr root<<1|1 14 #define mid (a[root].l+a[root].r)/2 15 16 int max(int x,int y){return x>y?x:y;} 17 int min(int x,int y){return x<y?x:y;} 18 int abs(int x,int y){return x<0?-x:x;} 19 20 21 struct node{ 22 int l, r, val, sum; 23 }a[N*8]; 24 25 struct Line{ 26 int x1, x2, y, val; 27 Line(){} 28 Line(int a,int b,int c,int d){ 29 x1=a; 30 x2=b; 31 y=c; 32 val=d; 33 } 34 }line[N*2]; 35 36 bool cmp(Line a,Line b){ 37 return a.y<b.y; 38 } 39 40 struct Cube{ 41 int x1, y1, z1, x2, y2, z2; 42 }cu[N]; 43 44 int n, m; 45 int p[5]; 46 int xx[N*2]; 47 int zz[N*2]; 48 int nx, nz; 49 50 int b_s(int key){ 51 int l=0, r=nx-1; 52 while(l<=r){ 53 int mm=(l+r)/2; 54 if(xx[mm]==key) return mm; 55 else if(xx[mm]>key) r=mm-1; 56 else if(xx[mm]<key) l=mm+1; 57 } 58 } 59 60 void build(int l,int r,int root){ 61 a[root].l=l; 62 a[root].r=r; 63 a[root].val=a[root].sum=0; 64 if(l==r) return; 65 build(l,mid,ll); 66 build(mid+1,r,rr); 67 } 68 69 void up(int root){ 70 if(a[root].val) a[root].sum=xx[a[root].r+1]-xx[a[root].l]; 71 else if(a[root].l==a[root].r) a[root].sum=0; 72 else a[root].sum=a[ll].sum+a[rr].sum; 73 } 74 75 void update(int l,int r,int val,int root){ 76 if(a[root].l==l&&a[root].r==r){ 77 a[root].val+=val; 78 up(root); 79 return; 80 } 81 if(r<=a[ll].r) update(l,r,val,ll); 82 else if(l>=a[rr].l) update(l,r,val,rr); 83 else{ 84 update(l,mid,val,ll); 85 update(mid+1,r,val,rr); 86 } 87 up(root); 88 } 89 90 main() 91 { 92 int t, i, j, k; 93 int kase=1; 94 cin>>t; 95 while(t--){ 96 scanf("%d %d",&n,&m); 97 for(i=1;i<=m;i++) scanf("%d",&p[i]); 98 nx=nz=0; 99 for(i=0;i<n;i++){ 100 scanf("%d %d %d %d %d",&cu[i].x1,&cu[i].y1,&cu[i].x2,&cu[i].y2,&cu[i].z2); 101 cu[i].z1=0; 102 cu[i].z2=p[cu[i].z2]; 103 xx[nx++]=cu[i].x1; 104 xx[nx++]=cu[i].x2; 105 zz[nz++]=cu[i].z1; 106 zz[nz++]=cu[i].z2; 107 } 108 sort(xx,xx+nx); 109 sort(zz,zz+nz); 110 nx=unique(xx,xx+nx)-xx; 111 nz=unique(zz,zz+nz)-zz; 112 __int64 ans=0; 113 for(i=1;i<nz;i++){ 114 k=0; 115 for(j=0;j<n;j++){ 116 if(cu[j].z1<=zz[i-1]&&cu[j].z2>=zz[i]){ 117 line[k++]=Line(cu[j].x1,cu[j].x2,cu[j].y1,1); 118 line[k++]=Line(cu[j].x1,cu[j].x2,cu[j].y2,-1); 119 } 120 } 121 sort(line,line+k,cmp); 122 build(0,nx,1); 123 __int64 num=0; 124 for(j=0;j<k-1;j++){ 125 update(b_s(line[j].x1),b_s(line[j].x2)-1,line[j].val,1); 126 num+=(__int64)a[1].sum*(__int64)(line[j+1].y-line[j].y); 127 } 128 ans+=num*(__int64)(zz[i]-zz[i-1]); 129 } 130 printf("Case %d: %I64d\n",kase++,ans); 131 } 132 }