zoukankan      html  css  js  c++  java
  • ZOJ-1610 Count the Colors 【线段树+区间更新】

    Description

    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


    思路:

    线段树区间更新,用一个vector统计每段的颜色,注意没有着色的段也要统计,因为它隔开的两端若是同色如果不统计那么就合成了一段

    Code:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<set>
     6 #include<vector>
     7 #include<string>
     8 #include<queue>
     9 using namespace std;
    10 #define lson o<<1
    11 #define rson o<<1|1
    12 #define M(a, b) memset(a, b, sizeof(a))
    13 #define INF 0x3f3f3f3f
    14 const int maxn = 8000 + 5;
    15 int val[maxn<<2], qL, qR, v, ans[maxn];
    16 vector<int> vec;
    17 
    18 void init() {
    19     M(val, -1);
    20     M(ans, 0);
    21     vec.clear();
    22 }
    23 
    24 void pushdown(int o) {
    25     if (~val[o]) {
    26         val[lson] = val[rson] = val[o];
    27         val[o] = -1;
    28     }
    29 }
    30 
    31 void update(int o, int L, int R) {
    32     if (qL <= L && R <= qR) val[o] = v;
    33     else {
    34         if (val[o] == v) return;
    35         pushdown(o);
    36         int M = (L + R) >> 1;
    37         if (qL <= M) update(lson, L, M);
    38         if (M < qR) update(rson, M+1, R);
    39     }
    40 }
    41 
    42 void query(int o, int L, int R) {
    43     if (~val[o]) vec.push_back(val[o]);
    44     else {
    45         if (L == R) {
    46             vec.push_back(-1);
    47             return;
    48         }
    49         int M = (L + R) >> 1;
    50         query(lson, L, M);
    51         query(rson, M+1, R);
    52     }
    53 }
    54 
    55 int main() {
    56     int n;
    57     while (~scanf("%d", &n)) {
    58         init();
    59         while (n--) {
    60             scanf("%d%d%d", &qL, &qR, &v);
    61             if (qL >= qR) continue;
    62             qL++;
    63             update(1, 1, maxn);
    64         }
    65         query(1, 1, maxn);
    66         int temp = -1;
    67         for (int i = 0; i < vec.size(); ++i) {
    68             if (temp == vec[i]) continue;
    69             else {
    70                 temp = vec[i];
    71                 if (~temp) ++ans[temp];
    72             }
    73         }
    74         for (int i = 0; i < maxn; ++i)
    75             if (ans[i]) printf("%d %d
    ", i, ans[i]);
    76         printf("
    ");
    77     }
    78 
    79     return 0;
    80 }
  • 相关阅读:
    Linux 线程间通信方式+进程通信方式 总结
    使用opencv第三方库的makefile文件示例
    rplidar SDK 二次开发---之获取目标信息(0.1)
    #include "Target_orientation.h"
    opencv —— 调用摄像头采集图像 VideoCapture capture(0);
    cmake 支持-lpthread
    ROS下sensor_msgs::ImagePtr到sensor_msgs::Image之间的转换
    JAVA 校验身份证号码工具类(支持15位和18位)
    python面向对象游戏练习:好人坏人手枪手榴弹
    python 私有属性的作用
  • 原文地址:https://www.cnblogs.com/robin1998/p/6596448.html
Copyright © 2011-2022 走看看