ZOJ - 1610
题目:
  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.

Input

The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.

Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:

x1 x2 c

x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.

All the numbers are in the range [0, 8000], and they are all integers.

Input may contain several data set, process to the end of file.

Output

Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.

If some color can't be seen, you shouldn't print it.

Print a blank line after every dataset.

Sample Input

5
    0 4 4
    0 3 1
    3 4 2
    0 2 2
    0 2 3
    4
    0 1 1
    3 4 1
    1 3 2
    1 3 1
    6
    0 1 0
    1 2 1
    2 3 1
    1 2 0
    2 3 0
    1 2 1

Sample Output

1 1
    2 1
    3 1

1 1

0 2
    1 1

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=8010;
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)struct node
{int l,r;int flag;   //flag µÈÓÚ -1 ±íʾʲôɫ¶¼Ã»ÓÐ
}tree[N*3];
int color[N];    //¼Ç¼ÿ¿é°åÉϵÄÊÇʲôÑÕÉ«
void build(int l,int r,int rt){tree[rt].l=l;tree[rt].r=r;tree[rt].flag=-1;if(l==r)return ;int mid=(l+r)>>1;build(l,mid,L(rt));build(mid+1,r,R(rt));
}void PushDown(int rt){tree[L(rt)].flag=tree[rt].flag;tree[R(rt)].flag=tree[rt].flag;tree[rt].flag=-1;
}void update(int val,int l,int r,int rt){if(tree[rt].flag==val)return ;if(l<=tree[rt].l && tree[rt].r<=r){tree[rt].flag=val;return ;}if(tree[rt].flag!=-1)   //ÑÓ³Ù¸²¸Ç ²»È»¿ÉÄÜ»áTLE£¨×î½ü×öÒ»Ö±¶¼ÊÇÑÓ³Ù¸²¸Ç£©PushDown(rt);int mid=(tree[rt].l+tree[rt].r)>>1;if(r<=mid)update(val,l,r,L(rt));else if(l>=mid+1)update(val,l,r,R(rt));else{update(val,l,r,L(rt));update(val,l,r,R(rt));}if((tree[L(rt)].flag==tree[R(rt)].flag) && (tree[L(rt)].flag!=-1))  //µÝ¹é»ØÀ´Ð޸ĸ¸½áµãÑÕÉ«tree[rt].flag=tree[L(rt)].flag;
}void query(int rt)
{if(tree[rt].flag!=-1){for(int i=tree[rt].l;i<=tree[rt].r;i++)color[i]=tree[rt].flag;return ;}if(tree[rt].l==tree[rt].r)  //ÕâÊDz»¼ÓÕâ¸ö¾¹È»»áÔËÐв»ÆðÀ´return ;query(L(rt));query(R(rt));
}void Solve(){int res[N];memset(res,0,sizeof(res));int pre=-1;for(int i=0;i<N;i++)if(pre!=color[i]){  //ÑÕÉ«¿é²»ÏàÁ¬¾Í++pre=color[i];res[pre]++;}for(int i=0;i<N;i++)if(res[i]!=0)printf("%d %d\n",i,res[i]);printf("\n");
}int main(){int n;while(~scanf("%d",&n)){memset(color,-1,sizeof(color));build(0,N,1);int x,y,val;while(n--){scanf("%d%d%d",&x,&y,&val);if(x==y)continue;update(val,x,y-1,1);    //ºóÃæÒ»¸ö²»Í¿¾ÍÄܱÜÃâÄÇÖÖÇé¿ö}   query(1);Solve();}return 0;
}

线段树(区间更新以及统计片段颜色)相关推荐

  1. Codeforces 444C DZY Loves Colors 线段树区间更新

    // Codeforces 444C DZY Loves Colors 线段树区间更新// 题目链接:// http://codeforces.com/problemset/problem/444/C ...

  2. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  3. hdu 5692 Snacks(dfs序+线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5692 解题思路:这道题是树节点的点权更新,而且涉及到子树,常用的思路是利用dfs序,用线段树来对区间进 ...

  4. hdu 1698(线段树区间更新)

    解题思路:线段树区间更新水题. #include<iostream> #include<cstdio> #include<cstring> using namesp ...

  5. hdu 3966(树链剖分+线段树区间更新)

    传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...

  6. POJ 2777 ZOJ 1610 HDU 1698 --线段树--区间更新

    直接将这3题 放一起了  今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 ...

  7. Just a Hook(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 In the game of DotA, Pudge's meat hook is actual ...

  8. hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新

    #1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,"模拟都市"是他们非常喜欢的一个游戏 ...

  9. CodeForces - 272C Dima and Staircase (线段树区间更新)

    题意: 见以下样例,给出 5 个区间,每个区间的高度已知.一共 4 次操作.每次操作都是从最左边开始向下垒一个宽为 w 高为h 的木块,过程见下图. 问每次垒木块的高度是多少? Input 5 1 2 ...

  10. CDOJ-1057 秋实大哥与花(线段树区间更新)

    秋实大哥与花 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit St ...

最新文章

  1. xgboost 正则项_XGBoost入门系列第一讲
  2. D - Triangle Partition HDU - 6300 sort(cmp)
  3. java学习笔记第三章
  4. Myeclipse报错--Animation Start An internal error has occurred. java.lang.NullPointerException解决...
  5. 20180525小测
  6. 温州大学《机器学习》课程课件(七、决策树)
  7. html文件嵌入到reportlab,Reportlab学习笔记
  8. Quartz Java编程
  9. PowerDesigner(四)-业务处理模型
  10. 前端集成解决方案(转)
  11. 05引用类型以及特殊引用类型string
  12. java 动态报表 sql,报表SQL
  13. Mybatis传递多个参数
  14. 【20211228】【信号处理】一文读懂信号处理中频谱混叠、栅栏效应、频谱泄露的产生原因和解决方法
  15. Gym - 100781A Adjoin the Networks (树的直径)
  16. 密码学——对称加密加密模式
  17. 珊瑚虫工作室_2007-12-24
  18. Dubbo实战(一)快速入门
  19. 整数n分解成素数乘积c语言,用C语言和汇编语言实现将1个整数分解成几个素数的乘积...
  20. 一碗酸爽面-倒在黎明前

热门文章

  1. 怎么用itunes来打开手机软件
  2. 常用chrome插件常用FireFox插件
  3. Url传值的Get method and Post method
  4. 三维动画制作流程之间的关系
  5. ISA2006的部署和无人职守
  6. 服务器宕机可能的原因以及服务器宕机解决办法
  7. 《Haskell函数式编程入门》—— 第1章,第1.6节本章小结
  8. Rust 程序语言资料
  9. 左右c++与java中国的垃圾问题的分析与解决
  10. 防火墙配置十大任务之五,有NAT的两个接口的配置