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).
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
#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=;
const int mn=mm<<;
struct seg
{
int x,y1,y2,c,v;
} g[mm];
int t[mn][],sum[mn][],p[],q[];
int y[mm];
int L,R,C,val,T;
void build(int n)
{
while(n--)for(int i=; i<T; ++i)t[n][i]=sum[n][i]=;
}
void updata(int l,int r,int rt)
{
if(L<=y[l]&&R>=y[r])t[rt][C]+=val;
else
{
int m=(l+r)>>;
if(L<y[m])updata(lson);
if(R>y[m])updata(rson);
}
int i,j;
for(i=T-; i>=; --i)
if(t[rt][i])
{
sum[rt][i]=y[r]-y[l];
for(j=i+; j<T; ++j)
sum[rt][i]-=sum[rt][j];
for(j=; j<i; ++j)sum[rt][j]=;
break;
}
else if(l>=r)sum[rt][i]=;
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=;
__int64 ans;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&T);
for(i=; i<T; ++i)scanf("%d",&p[i]),y[i]=i;
for(i=; i<T; ++i)
for(j=i+; j<T; ++j)
if(p[i]>p[j])swap(y[i],y[j]),swap(p[i],p[j]);
for(i=; i<T; ++i)q[y[i]]=i;
for(i=; 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-];
g[i].y1=y[i],g[i].y2=y[i+n],g[i].v=;
g[i+n].y1=y[i],g[i+n].y2=y[i+n],g[i+n].v=-;
}
sort(y,y+n+n);
sort(g,g+n+n,cmp);
for(m=i=; i<n+n; ++i)
if(y[m]<y[i])y[++m]=y[i];
for(ans=i=; i<n+n; ++i)
{
L=g[i].y1,R=g[i].y2,C=g[i].c,val=g[i].v;
updata(,m,);
if(g[i].x<g[i+].x)
for(j=; j<T; ++j)
ans+=(__int64)(g[i+].x-g[i].x)*(__int64)sum[][j]*(__int64)p[j];
}
printf("Case %d: %I64d\n",++cs,ans);
}
return ;
}