zoj 1610 Count the Colors

题目

Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.
Your task is counting the segments of different colors you can see at last.
大意:在一条线段上画彩色线段,后画的可以覆盖前面的,最后每种颜色可以看到多少条线段

题解

线段树,统计错的一塌糊涂

代码

var
  t:array[1..100000]of longint;
  f,x,y,k:array[0..10000]of longint;
  n,i,j,m,r,s,c:longint;

procedure insert(p,l,r,a,b,i:longint);
var
  m:longint;
begin
  if (l=a)and(r=b) then begin t[p]:=i;exit;end;
  m:=(l+r) div 2;
  if t[p]>=0 then
    begin
      t[p*2]:=t[p];
      t[p*2+1]:=t[p];
      t[p]:=-1;
    end;
  if b<=m then insert(p*2,l,m,a,b,i) else
  if a>=m then insert(p*2+1,m,r,a,b,i) else
    begin
      insert(p*2,l,m,a,m,i);
      insert(p*2+1,m,r,m,b,i);
    end;
end;

function count(p,l,r:longint):longint;
var
  m:longint;
begin
  if t[p]=0 then c:=0 else
  if (t[p]>0)and(c<>t[p]) then begin c:=t[p];inc(f[c]);end else
  if (l-r>1) then
    begin
      m:=(l+r) div 2;
      count(p*2,l,m);
      count(p*2+1,m,r);
    end;
end;

begin
  while not eoln do
    begin
      readln(n);
      r:=0;s:=0;j:=0;m:=0;
      fillchar(f,sizeof(f),0);
      for i:=1 to n do
        begin
          readln(x[i],y[i],k[i]);
          inc(k[i]);
          if k[i]<j then j:=k[i] else if k[i]>m then m:=k[i];
          if x[i]<r then r:=x[i];
          if y[i]>s then s:=y[i];
        end;
      for i:=1 to n do
        insert(1,r,s,x[i],y[i],k[i]);
      count(1,r,s);
      for i:=j to m do
        if f[i]>0 then
          writeln(i-1,' ',f[i]);
      writeln;
    end;
end.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值