Farming

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
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #define ls rt<<1
 5 #define rs rt<<1|1
 6 #define lson l,m,ls
 7 #define rson m,r,rs
 8 using namespace std;
 9 const int mm=66666;
10 const int mn=mm<<2;
11 struct seg
12 {
13     int x,y1,y2,c,v;
14 } g[mm];
15 int t[mn][5],sum[mn][5],p[5],q[5];
16 int y[mm];
17 int L,R,C,val,T;
18 void build(int n)
19 {
20     while(n--)for(int i=0; i<T; ++i)t[n][i]=sum[n][i]=0;
21 }
22 void updata(int l,int r,int rt)
23 {
24     if(L<=y[l]&&R>=y[r])t[rt][C]+=val;
25     else
26     {
27         int m=(l+r)>>1;
28         if(L<y[m])updata(lson);
29         if(R>y[m])updata(rson);
30     }
31     int i,j;
32     for(i=T-1; i>=0; --i)
33         if(t[rt][i])
34         {
35             sum[rt][i]=y[r]-y[l];
36             for(j=i+1; j<T; ++j)
37                 sum[rt][i]-=sum[rt][j];
38             for(j=0; j<i; ++j)sum[rt][j]=0;
39             break;
40         }
41         else if(l>=r)sum[rt][i]=0;
42         else sum[rt][i]=sum[ls][i]+sum[rs][i];
43 }
44 bool cmp(seg a,seg b)
45 {
46     return a.x<b.x;
47 }
48 int main()
49 {
50     int i,j,k,n,m,t,cs=0;
51     __int64 ans;
52     scanf("%d",&t);
53     while(t--)
54     {
55         scanf("%d%d",&n,&T);
56         for(i=0; i<T; ++i)scanf("%d",&p[i]),y[i]=i;
57         for(i=0; i<T; ++i)
58             for(j=i+1; j<T; ++j)
59                 if(p[i]>p[j])swap(y[i],y[j]),swap(p[i],p[j]);
60         for(i=0; i<T; ++i)q[y[i]]=i;
61         for(i=0; i<n; ++i)
62         {
63             scanf("%d%d%d%d%d",&g[i].x,&y[i],&g[i+n].x,&y[i+n],&g[i].c);
64             g[i+n].c=g[i].c=q[g[i].c-1];
65             g[i].y1=y[i],g[i].y2=y[i+n],g[i].v=1;
66             g[i+n].y1=y[i],g[i+n].y2=y[i+n],g[i+n].v=-1;
67         }
68         sort(y,y+n+n);
69         sort(g,g+n+n,cmp);
70         for(m=i=0; i<n+n; ++i)
71             if(y[m]<y[i])y[++m]=y[i];
72         for(ans=i=0; i<n+n; ++i)
73         {
74             L=g[i].y1,R=g[i].y2,C=g[i].c,val=g[i].v;
75             updata(0,m,1);
76             if(g[i].x<g[i+1].x)
77                 for(j=0; j<T; ++j)
78                     ans+=(__int64)(g[i+1].x-g[i].x)*(__int64)sum[1][j]*(__int64)p[j];
79         }
80         printf("Case %d: %I64d\n",++cs,ans);
81     }
82     return 0;
83 }
View Code

 

转载于:https://www.cnblogs.com/contestant/p/3266123.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值