zoukankan      html  css  js  c++  java
  • ZOJ1610 Count the Colors —— 线段树 区间染色

    题目链接:https://vjudge.net/problem/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.


    <b< dd="">

    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.


    <b< dd="">

    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


    <b< dd="">

    Sample Output

    1 1
    2 1
    3 1

    1 1

    0 2
    1 1

    题解:

    问最终用多少段颜色相同的区域。经典的区间染色问题。

    写法一:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const double EPS = 1e-8;
    15 const int INF = 2e9;
    16 const LL LNF = 2e18;
    17 const int MAXN = 1e4+10;
    18 
    19 int val[MAXN<<2];
    20 int num[MAXN], color[MAXN];
    21 
    22 void push_down(int u)
    23 {
    24     if(val[u]>=0)
    25     {
    26         val[u*2] = val[u*2+1] = val[u];
    27         val[u] = -1;
    28     }
    29 }
    30 
    31 void set_val(int u, int l, int r, int x, int y, int _val)
    32 {
    33     if(x<=l && r<=y)
    34     {
    35         val[u] = _val;
    36         return;
    37     }
    38 
    39     push_down(u);
    40     int mid = (l+r)>>1;
    41     if(x<=mid) set_val(u*2, l, mid, x, y, _val);
    42     if(y>=mid+1) set_val(u*2+1, mid+1, r, x, y, _val);
    43 }
    44 
    45 void query(int u, int l, int r)
    46 {
    47     if(l==r)
    48     {
    49         color[l] = val[u];
    50         return;
    51     }
    52 
    53     push_down(u);
    54     int mid = (l+r)>>1;
    55     query(u*2, l, mid);
    56     query(u*2+1, mid+1, r);
    57 }
    58 
    59 int main()
    60 {
    61     int m;
    62     while(scanf("%d", &m)!=EOF)
    63     {
    64         memset(val, -1, sizeof(val));
    65         for(int i = 1; i<=m; i++)
    66         {
    67             int x, y, z;
    68             scanf("%d%d%d", &x, &y, &z);
    69             if(x<y) set_val(1, 1, 8000, x+1, y, z);
    70         }
    71 
    72         query(1, 1, 8000);
    73         memset(num, 0, sizeof(num));
    74         for(int i = 1; i<=8000; i++)
    75             if(color[i]!=-1 && (i==1 || color[i]!=color[i-1]))
    76                 num[color[i]]++;
    77 
    78 
    79         for(int i = 0; i<=8000; i++)
    80             if(num[i])
    81                 printf("%d %d
    ", i, num[i]);
    82         printf("
    ");
    83     }
    84 }
    View Code

    写法二:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const double EPS = 1e-8;
    15 const int INF = 2e9;
    16 const LL LNF = 2e18;
    17 const int MAXN = 1e4+10;
    18 
    19 int val[MAXN<<2];
    20 int num[MAXN];
    21 
    22 void push_down(int u)
    23 {
    24     if(val[u]>=0)
    25     {
    26         val[u*2] = val[u*2+1] = val[u];
    27         val[u] = -1;
    28     }
    29 }
    30 
    31 void set_val(int u, int l, int r, int x, int y, int _val)
    32 {
    33     if(x<=l && r<=y)
    34     {
    35         val[u] = _val;
    36         return;
    37     }
    38 
    39     push_down(u);
    40     int mid = (l+r)>>1;
    41     if(x<=mid) set_val(u*2, l, mid, x, y, _val);
    42     if(y>=mid+1) set_val(u*2+1, mid+1, r, x, y, _val);
    43 }
    44 
    45 int pre;
    46 void query(int u, int l, int r)
    47 {
    48     if(l==r)
    49     {
    50         if(val[u]>=0 && val[u]!=pre)
    51             num[val[u]]++;
    52         pre = val[u];
    53         return;
    54     }
    55 
    56     push_down(u);
    57     int mid = (l+r)>>1;
    58     query(u*2, l, mid);
    59     query(u*2+1, mid+1, r);
    60 }
    61 
    62 int main()
    63 {
    64     int m;
    65     while(scanf("%d", &m)!=EOF)
    66     {
    67         memset(val, -1, sizeof(val));
    68         for(int i = 1; i<=m; i++)
    69         {
    70             int x, y, z;
    71             scanf("%d%d%d", &x, &y, &z);
    72             if(x<y) set_val(1, 1, 8000, x+1, y, z);
    73         }
    74 
    75         memset(num, 0, sizeof(num));
    76         pre = -1;
    77         query(1, 1, 8000);
    78         for(int i = 0; i<=8000; i++)
    79             if(num[i])
    80                 printf("%d %d
    ", i, num[i]);
    81         printf("
    ");
    82     }
    83 }
    View Code
  • 相关阅读:
    循环神经网络(三)-RNN进阶
    卷积神经网络CNN【1】-简介
    tensorflow基础【5】-tensorboard
    强化学习8-时序差分控制离线算法Q-Learning
    POJ 1979 Red and Black 四方向棋盘搜索
    POJ 2386 Lake Counting 八方向棋盘搜索
    Problem A: Assembly Required K路归并
    POJ 2051 Argus 优先队列
    优先队列---使用
    Problem B: Bulbs
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7725985.html
Copyright © 2011-2022 走看看