zoukankan      html  css  js  c++  java
  • 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

    题解:各种错。。。最终发现还是要插a+1,b才可以,因为是颜色段,并不是点,还有,那个n是n条线段,所以查找和建树均要是8000内建,否则会wa;

    思路:-1代笔多色,0代表无色;

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<vector>
    using namespace std;
    const int INF=0x3f3f3f3f;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define PI(x) printf("%d",x)
    #define SD(x,y) scanf("%lf%lf",&x,&y)
    #define P_ printf(" ")
    #define ll root<<1
    #define rr root<<1|1
    #define lson ll,l,mid
    #define rson rr,mid+1,r
    #define V(x) tree[x]
    typedef long long LL;
    const int MAXN=8010;
    int color[MAXN];
    int temp;
    int tree[MAXN<<2];
    void pushdown(int root){
    	if(V(root)>0){
    		V(ll)=V(root);
    		V(rr)=V(root);
    		V(root)=-1;
    	}
    }
    void build(int root,int l,int r){
    	int mid=(l+r)>>1;
    	V(root)=0;
    	if(l==r)return;
    	build(lson);build(rson);
    }
    void update(int root,int l,int r,int A,int B,int v){
    	if(l>=A&&r<=B){
    		V(root)=v;
    		return;
    	}
    	int mid=(l+r)>>1;
    	pushdown(root);
    	if(mid>=A)update(lson,A,B,v);
    	if(mid<B)update(rson,A,B,v);
    	V(root)=-1;
    }
    void query(int root,int l,int r){
    	int mid=(l+r)>>1;
    	if(temp==V(root))return;
    	if(!V(root)){
    		temp=0;return;
    	}
    	if(V(root)!=-1){
    		if(temp!=V(root)){
    			temp=V(root);
    			color[temp]++;
    			return;
    		}
    		return;
    	}
    	if(l==r)return;
    		query(lson);
    		query(rson);
    }
    int main(){
    	int n;
    	while(~scanf("%d",&n)){
    		mem(color,0);
    		build(1,1,8000);
    		int a,b,c;
    		int t=n;
    		while(t--){
    			scanf("%d%d%d",&a,&b,&c);
    			update(1,1,8000,a+1,b,c+1);
    		}
    		query(1,1,8000);
    		for(int i=1;i<=8001;i++){
    			if(color[i])printf("%d %d
    ",i-1,color[i]);
    		}puts("");
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    海思HISI 3518EV200 AEC回音消除+ANR语音降噪(转)
    重磅消息 | 2020年最新全栈测试开发技能实战指南(第1期)
    推荐一款Python开源库,技术人必备的造数据神器!
    DevOps研发模式下「产品质量度量」方案实践
    自动化测试实战技巧:「用例失败重试机制」实现方案分享
    C#/VB.NET 在Word中添加条码、二维码
    Java 在PDF中绘制形状(基于Spire.Cloud.SDK for Java)
    Java 添加条码、二维码到Word文档
    Java 添加、下载、读取PDF附件信息(基于Spire.Cloud.SDK for Java)
    Java 添加、提取PDF中的图片
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5126951.html
Copyright © 2011-2022 走看看