mobile

//mobile 题目来自Mr.S
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<memory.h>
#include<vector>
#include<math.h>

#define Sleft(x)   ((x<<1))
#define Sright(x)  ((x<<1|1))
#define Sfather(x) ((x>>1))

using namespace std;

const int MAXN = 101; //max N
const int root = 1;
const int first = 1;
const int PASS = 1;
const int EMPTY = 0;
const double e = 1e-10;

int N; //rectangles' quantity
struct Line
{
     double X_axis;
     double Y_axisB,Y_axisT;
     int Tag;
     Line(double a=0.0,double b=0.0,double c=0.0,int d=1):
    X_axis(a),Y_axisB(b),Y_axisT(c),Tag(d){}
     bool operator<(const Line &V) const
     {
       return X_axis<V.X_axis;
     }
}; //Line node
struct Line S[MAXN*2]; //Line G
int tot; // Lines' quantity 
int sumy; // scanning Line's quantity
double Y_axis[MAXN*2];

struct Tree 
{
     double Len;
     int L,R;
     int Cover;
     Tree(double a=0.0,int b=0,int c=0,int d=0):
     Len(a),L(b),R(c),Cover(d){}
}; //Segment Tree node
struct Tree T[MAXN*MAXN]; //Segment Tree

double ans = 0.0; //answer
int times = 0;
void put()
{
    printf("%d %.2lf\n",times,ans);
}

void Build_Tree(int t,int s,int e)
{
    T[t]=Tree(0.0,s,e,0);
    int mid = (s+e)/2;
   if (s>=e) return ;
    if (s+1<e) Build_Tree(Sleft(t),s,mid);
    if (s+1<e) Build_Tree(Sright(t),mid,e);
}
void init()
{
    int i;
    tot=sumy=0;
    ans=0.0;
    memset(Y_axis,0,sizeof(Y_axis));
    for (i=1;i<=N;i++)
    {
      double xi,yi;
      double ri;
      scanf("%lf %lf %lf",&xi,&yi,&ri);
      S[++tot]=Line(xi-ri,yi-ri,yi+ri,1);
      S[++tot]=Line(xi+ri,yi-ri,yi+ri,-1);
      Y_axis[++sumy]=yi-ri;
      Y_axis[++sumy]=yi+ri;
    }
    sort(Y_axis+1,Y_axis+sumy+1);
    sort(S+1,S+tot+1);
    sumy = unique(Y_axis+1,Y_axis+sumy+1)-Y_axis-1;
    Build_Tree(root,1,sumy);
}
void Calc(int t)
{
    if (T[t].Cover!=EMPTY)
    T[t].Len=Y_axis[T[t].R]-Y_axis[T[t].L];
    else if (T[t].L+1==T[t].R) T[t].Len=0;
    else T[t].Len=T[Sleft(t)].Len+T[Sright(t)].Len;
}
void Update(int t,int s,int e,int tag)
{
    if (s>=e) return ;
    int mid = (T[t].L+T[t].R)/2;
    if (s<=T[t].L&&e>=T[t].R)
    {
     T[t].Cover+=tag;
     Calc(t);
     return ;
   }
   if (e<=mid) Update(Sleft(t),s,e,tag);
   else if (s>=mid) Update(Sright(t),s,e,tag);
   else Update(Sleft(t),s,mid,tag),Update(Sright(t),mid,e,tag);
   Calc(t);
}
void work()
{
    int i,j;
    for (i=1;i<=tot;i++)
    {
      if (i!=first) ans+=T[root].Len*(S[i].X_axis-S[i-1].X_axis);
      j = i;
      while (j<=tot&&(i==j||fabs(S[j].X_axis-S[j-1].X_axis)<e))
      {
         int l=lower_bound(Y_axis+1,Y_axis+sumy+1,S[j].Y_axisB)-Y_axis;
         int r=lower_bound(Y_axis+1,Y_axis+sumy+1,S[j].Y_axisT)-Y_axis;
         Update(root,l,r,S[j].Tag);
         j++;
      }
      i = j-1;
    }
}
void groups()
{
    while (PASS)
    {
       scanf("%d",&N);
       if (N==EMPTY) return ;
       times++;
       init();
       work();
       put();
    }
}
int main()
{
    freopen("mobile.in","r",stdin);
    freopen("mobile.out","w",stdout);
    groups();
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值