【ZOJ】1610 Count the Colors

 1 #include<cstdio>
 2 #include<cstring>
 3 #define MAXN 8010
 4 int tree[MAXN<<2],color[MAXN],ans[MAXN];
 5 inline void PushDown(int rt)
 6 {
 7     if(tree[rt]!=-1)
 8     {
 9         tree[rt<<1]=tree[rt<<1|1]=tree[rt];
10         tree[rt]=-1;
11     }
12 }
13 void Update(int x,int y,int val,int L,int R,int rt)
14 {
15     if(x<=L&&R<=y)
16         tree[rt]=val;
17     else
18     {
19         int mid=(L+R)>>1;
20         PushDown(rt);
21         if(mid>=x)
22             Update(x,y,val,L,mid,rt<<1);
23         if(y>mid)
24             Update(x,y,val,mid+1,R,rt<<1|1);
25     }
26 }
27 void Query(int L,int R,int rt)
28 {
29     if(tree[rt]!=-1)
30     {
31         for(int i=L;i<=R;i++)
32             color[i]=tree[rt];
33     }
34     else if(L!=R)
35     {
36         int mid=(L+R)>>1;
37         Query(L,mid,rt<<1);
38         Query(mid+1,R,rt<<1|1);
39     }
40 }
41 int main()
42 {
43     int n,x,y,val,i,j;
44     while(~scanf("%d",&n))
45     {
46         memset(tree,-1,sizeof(tree));
47         memset(color,-1,sizeof(color));
48         memset(ans,0,sizeof(ans));
49         while(n--)
50         {
51             scanf("%d%d%d",&x,&y,&val);
52             if(x<y)
53                 Update(x+1,y,val,1,MAXN,1);
54         }
55         Query(1,MAXN,1);
56         for(i=0;i<MAXN;i++)
57         {
58             if(color[i]!=-1)
59             {
60                 for(j=i;j<MAXN&&color[i]==color[j];j++);
61                 ans[color[i]]++;
62                 i=j-1;
63             }
64         }
65         for(i=0;i<MAXN;i++)
66         {
67             if(ans[i])
68                 printf("%d %d\n",i,ans[i]);
69         }
70         putchar('\n');
71     }
72     return 0;
73 }

转载于:https://www.cnblogs.com/DrunBee/archive/2012/05/22/2513498.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值