zoukankan      html  css  js  c++  java
  • POJ 1610 Count the Colors

    Count the Colors

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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>
    #include <queue>
    #include <cmath>
    #include <vector>
    #include <algorithm>
    using namespace std;
    #define lowbit(x) (x&(-x))
    #define max(x,y) (x>y?x:y)
    #define min(x,y) (x<y?x:y)
    #define mem(a) (memset(a,0,sizeof(a)))
    typedef long long ll;
    int vis[80006];
    int li,ri,w;
    int pos[80006];
    int ans[40006];
    void pushdown(int node)
    {
        if(vis[node]!=-1)
        {
            vis[node<<1]=vis[node];
            vis[node<<1|1]=vis[node];
            vis[node]=-1;
        }
    }
    void update(int ll,int rr,int val,int l,int r,int node)
    {
        if(ll<=l && rr>=r)
        {
            vis[node]=val;
            return ;
        }
        pushdown(node);
        int mid=(l+r)>>1;
        if(ll<=mid) update(ll,rr,val,l,mid,node<<1);
        if(rr>mid) update(ll,rr,val,mid+1,r,node<<1|1);
    }
    void query(int l,int r,int node)
    {
        if(vis[node]>=0)
        {
            for(int i=l;i<=r;i++)
                ans[i]=vis[node];
            return ;
        }
        if(vis[node]==-1 && l!=r)
        {
            int mid=(l+r)>>1;
            query(l,mid,node<<1);
            query(mid+1,r,node<<1|1);
        }
    }
    int main()
    {
        int m;
        while(scanf("%d",&m)!=EOF)
        {
            memset(vis,-1,sizeof(vis));
            memset(pos,0,sizeof(pos));
            memset(ans,-1,sizeof(ans));
            for(int i=0;i<m;i++)
            {
                scanf("%d%d%d",&li,&ri,&w);
                if(li>=ri) continue;
                update(li+1,ri,w,1,8000,1);
            }
            query(1,8000,1);
            int i=1;
            while(i<8001)
            {
                int coo=ans[i],j=i+1;
                if(coo==-1){i++;continue;}
                while(j<8001 && ans[j]==coo && ans[j]!=-1)
                {
                    j++;
                }
                pos[coo]++;
                i=j;
            }
            for(int i=0;i<8001;i++)
            {
                if(pos[i]) printf("%d %d
    ",i,pos[i]);
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    单例
    淘宝在数据处理领域的项目及开源产品介绍 | 岭南六少
    数据库垂直拆分,水平拆分利器,cobar升级版mycat
    基于Gtid的mysql主从复制 和 mysql-proxy 读写分离
    几行lua代码计算http包总长度_指甲锉_新浪博客
    使用Lua和OpenResty搭建验证码服务器
    测试比json更快更小的二进制数据传输格式Msgpack [pythono MessagePack 版本]
    使用 HAProxy, PHP, Redis 和 MySQL 轻松构建每周上亿请求Web站点
    nginx+lua+redis实现验证码防采集
    OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能 | 琥珀志
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7181806.html
Copyright © 2011-2022 走看看