//9379418 NKHelloWorld 1389 Accepted 296K 16MS C++ 1997B 2011-10-01 12:31:16
//9379419 NKHelloWorld 1389 Accepted 844K 0MS G++ 1997B 2011-10-01 12:31:31
/*
POJ1151的整数简化版本,套用模板1A
*/
#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
int y[3000];
struct Line
{
int x, y1, y2,flag;
} line[3000];
struct Node
{
int l, r, cover;
int lf, rf, len;
} node[10000];
bool cmp ( Line a, Line b )
{
return a.x < b.x;
}
void length ( int u )
{
if ( node[u].cover > 0 )
{
node[u].len = node[u].rf - node[u].lf;
return;
}
else if ( node[u].l + 1 == node[u].r )
node[u].len = 0; /* 叶子节点,len 为 0 */
else
node[u].len = node[L(u)].len + node[R(u)].len;
}
void build ( int u, int l, int r )
{
node[u].l = l; node[u].r = r;
node[u].lf = y[l]; node[u].rf = y[r];
node[u].len = node[u].cover = 0;
if ( l + 1 == r ) return;
int mid = ( l + r ) / 2;
build ( L(u), l, mid );
build ( R(u), mid, r );
}
void update ( int u, Line e )
{
if ( e.y1 == node[u].lf && e.y2 == node[u].rf )
{
node[u].cover += e.flag;
length ( u );
return;
}
if ( e.y1 >= node[R(u)].lf )
update ( R(u), e );
else if ( e.y2 <= node[L(u)].rf )
update ( L(u), e );
else
{
Line temp = e;
temp.y2 = node[L(u)].rf;
update ( L(u), temp );
temp = e;
temp.y1 = node[R(u)].lf;
update ( R(u), temp );
}
length ( u );
}
int main()
{
int n, t,i;
int x1,y1,x2,y2,ans;
while(1)
{
t = n = 1;
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2),x1!=-1)
{
line[t].x = x1; line[t].y1 = y1; line[t].y2 = y2; line[t].flag = 1; y[t] = y1; t++;
line[t].x = x2; line[t].y1 = y1; line[t].y2 = y2; line[t].flag = -1; y[t] = y2; t++;
n++;
}
if(n==1) break;
n--;
sort ( line + 1, line + t, cmp );
sort ( y + 1, y + t );
build ( 1, 1, t-1 );
update ( 1, line[1] );
ans = 0;
for ( i = 2; i < t; i++ )
{
ans += node[1].len * ( line[i].x - line[i-1].x );
update ( 1, line[i] );
}
printf("%d\n",ans);
}
return 0;
}
转载于:https://www.cnblogs.com/nkhelloworld/archive/2011/10/01/2197050.html