• xtu数据结构 G. Count the Colors


    G. Count the Colors

    Time Limit: 2000ms
    Memory Limit: 65536KB
    64-bit integer IO format: %lld      Java class name: Main
     
     
    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

     解题:线段树。。。比较奇葩的线段树,此树的叶子节点必须是一个长度为1的线段,而不是我通常所写的那种叶子节点就是一个点的那种。
     
    [0,4]
    [0,2][2,4]
    [0,1][1,2][2,3][3,4]
    是这种线段树。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #define LL long long
    13 #define INF 0x3f3f3f
    14 using namespace std;
    15 const int maxn = 8010;
    16 struct node {
    17     int lt,rt,color;
    18 } tree[maxn<<2];
    19 int col[maxn],temp;
    20 void build(int lt,int rt,int v) {
    21     int mid = (lt+rt)>>1;
    22     tree[v].lt = lt;
    23     tree[v].rt = rt;
    24     tree[v].color = -1;
    25     if(lt+1 == rt) return;
    26     build(lt,mid,v<<1);
    27     build(mid,rt,v<<1|1);
    28 }
    29 void update(int lt,int rt,int c,int v) {
    30     if(lt == rt || tree[v].color == c) return;
    31     if(tree[v].lt >= lt && tree[v].rt <= rt) {
    32         tree[v].color = c;
    33         return;
    34     }
    35     if(tree[v].color >= 0) {
    36         tree[v<<1].color = tree[v<<1|1].color = tree[v].color;
    37         tree[v].color = -2;
    38     }
    39     int mid = (tree[v].lt+tree[v].rt)>>1;
    40     if(rt <= mid) {
    41         update(lt,rt,c,v<<1);
    42     } else if(lt >= mid) {
    43         update(lt,rt,c,v<<1|1);
    44     } else {
    45         update(lt,mid,c,v<<1);
    46         update(mid,rt,c,v<<1|1);
    47     }
    48     tree[v].color = -2;
    49 }
    50 void query(int v) {
    51     if(tree[v].color == -1) {
    52         temp = -1;
    53         return;
    54     }
    55     if(tree[v].color != -2) {
    56         if(tree[v].color != temp) {
    57             temp = tree[v].color;
    58             col[temp]++;
    59         }
    60         return;
    61     }
    62     if(tree[v].lt+1 != tree[v].rt) {
    63         query(v<<1);
    64         query(v<<1|1);
    65     }
    66 }
    67 int main() {
    68     int n,i,j,x,y,c,mx;
    69     while(~scanf("%d",&n)) {
    70         build(0,8002,1);
    71         memset(col,0,sizeof(col));
    72         mx = 0;
    73         for(i = 0; i < n; i++) {
    74             scanf("%d%d%d",&x,&y,&c);
    75             update(x,y,c,1);
    76             if(c > mx) mx = c;
    77         }
    78         temp = -1;
    79         query(1);
    80         for(i = 0; i <= mx; i++)
    81             if(col[i]) printf("%d %d
    ",i,col[i]);
    82         printf("
    ");
    83     }
    84     return 0;
    85 }
    View Code
     
  • 相关阅读:
    CoreText实现图文混排
    iOS中的数据的存储方式
    获得自定义的所有相簿
    swift基础语法(31- swift可选类型)
    swift内存管理(30- Swift内存管理)
    swift基础语法(29- 析构方法)
    swift基础语法(27- 构造方法,带参数的构造方法,常量存储属性与构造方法,结构体构造方法)
    iOS --旋转动画
    iOS--UILabel上画横线
    iOS--(转)集成银联3.3.0
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3858359.html
Copyright © 2020-2023  润新知