Note that the rectangle delineated by 0,0 and 2,2 is two units wide and two high. Here's a schematic diagram of the input:
The '4's at 8,0 to 10,19 are only two wide, not three (i.e., the grid contains a 4 and 8,0 and a 4 and 8,1 but NOT a 4 and 8,2 since this diagram can't capture what would be shown on graph paper).
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define Len 200005
#define mod 19999997
const int INF = 0x3f3f3f3f;
#define exp 1e-6
int seq[1001][5];
int col[1001],col_num[2501];
int A,B;
int n;
long long dfs(int begin,int a,int b,int c,int d)
{
if(a >= c || b >= d) return 0;
for(int i = begin; i <= n; i++)
{
int x1=seq[i][1],y1=seq[i][2],x2=seq[i][3],y2=seq[i][4];
if(! (a>=x2 || b>=y2 || c<=x1 || d<=y1) )
{
if(a < x1 && c > x1)
{
return dfs(i, a, b, x1, d)+dfs(i, x1, b, c, d);
}
else if(a < x2 && c > x2)
{
return dfs(i, a, b,x2, d) + dfs(i,x2, b, c, d);
}
else
return dfs(i, a, b, c, y1) + dfs(i, a,y2, c, d);
}
}
return ((c - a) * (d - b));
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d%d%d",&A,&B,&n);
seq[0][1] = seq[0][2] = 0;
seq[0][3] = A,seq[0][4] =B,col[0] = 1;
for(int i = 1; i <= n; i++)
{
scanf("%d%d%d%d%d",&seq[i][1],&seq[i][2],&seq[i][3],&seq[i][4],&col[i]);
}
memset(col_num, 0 ,sizeof(col_num));
for(int i = n; i >= 0; i--)
{
col_num[col[i]] += dfs(i+1,seq[i][1],seq[i][2],seq[i][3],seq[i][4]);
}
for(int i = 1; i <= 2500; i++)
{
if(col_num[i] > 0)
printf("%d %d\n",i,col_num[i]);
}
}
return 0;
}
/**************************************************************
Problem: 1589
User: aking2015
Language: C++
Result: Accepted
Time:16 ms
Memory:1516 kb
****************************************************************/