线段树的经典题。。。我拿离散化过掉了。。。哇咔咔。。。数据不够黑啊。这个算是O(N^2)的。
之前写过矩形并,我就想到这题了,今天又想起来了,写了试试,线段的并比矩形并还简单哈。
2574678 2011-07-10 21:24:28 Accepted 1610 C++ 170 656 小媛在努力@SOWHAT
等回校学线段树再来A一次~真不知道这个算啥类型。。还归到计算几何好了。
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 2*10010;
struct LL{ int x,y,c;};
LL b[MAX];
int a[MAX];
int len[MAX];
int col[MAX];
void solve(int n,int cnt)
{
for(int i=0; i<n; i++)
{
int x = lower_bound(a,a+cnt,b[i].x) - a;
int y = lower_bound(a,a+cnt,b[i].y) - a;
for(int k=x; k<y; k++)
col[k] = b[i].c;
}
for(int i=0; i<MAX; i++)
if( col[i+1] != col[i] && col[i] != -1 )
len[col[i]]++;
}
int main()
{
int n,cnt;
while( ~scanf("%d",&n) )
{
memset(len,0,sizeof(len));
memset(col,-1,sizeof(col));
cnt = 0;
for(int i=0; i<n; i++)
{
scanf("%d%d%d",&b[i].x,&b[i].y,&b[i].c);
a[cnt++] = b[i].x; a[cnt++] = b[i].y;
}
sort(a,a+cnt);
solve(n,cnt);
for(int i=0; i<MAX; i++)
if( len[i] )
printf("%d %d\n",i,len[i]);
printf("\n");
}
return 0;
}