Count the Colors

原创 2015年07月08日 20:24:46
B - Count the Colors
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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


/*
Author: 2486
Memory: 232 KB		Time: 110 MS
Language: C++ (g++ 4.7.2)		Result: Accepted
*/
//暴力可过,或者是线段树
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int maxn=8000+5;
int n,a,b,c,ans[maxn],col[maxn];
int main() {
    while(~scanf("%d",&n)) {
        memset(ans,0,sizeof(ans));
        memset(col,0,sizeof(col));
        int Max=0;
        for(int i=0; i<n; i++) {
            scanf("%d%d%d",&a,&b,&c);
            for(int j=a; j<b; j++) {
                col[j]=c+1;
            }
            Max=max(Max,b);
        }
        for(int i=0; i<=Max; i++) {
            while(i!=0&&col[i]&&col[i]==col[i-1]) {
                i++;
            }
            if(col[i]) {
                ans[col[i]-1]++;
            }
        }
        for(int i=0; i<=8001; i++) {
            if(ans[i]) {
                printf("%d %d\n",i,ans[i]);
            }
        }
        printf("\n");
    }
}
/*
Author: 2486
Memory: 356 KB		Time: 20 MS
Language: C++ (g++ 4.7.2)		Result: Accepted
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=8000+5;
int sum[maxn],col[maxn<<2],res[maxn];
int n,x1,x2,c;
void pushup(int rt) {}
void pushdown(int rt,int l,int r) {
    if(col[rt]!=-1) {
        col[rt<<1]=col[rt<<1|1]=col[rt];//直接往下递归就可以了,不用管其他
        col[rt]=-1;
    }
}
void build(int rt,int l,int r) {
    col[rt]=-1;
    if(l==r)return;
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
}
void update(int L,int R,int c,int rt,int l,int r) {
    if(L<=l&&r<=R) {
        col[rt]=c;
        return;
    }
    pushdown(rt,l,r);
    int mid=(l+r)>>1;
    if(L<=mid)update(L,R,c,rt<<1,l,mid);
    if(mid<R)update(L,R,c,rt<<1|1,mid+1,r);
    pushup(rt);
}
void query(int L,int R,int rt,int l,int  r) {
    if(l==r) {
        sum[l]=col[rt];//赋值,放回区间的位子颜色
        return;
    }
    pushdown(rt,l,r);
    int mid=(l+r)>>1;
    if(L<=mid)query(L,R,rt<<1,l,mid);
    if(mid<R)query(L,R,rt<<1|1,mid+1,r);
    pushup(rt);
}
int main() {
    while(~scanf("%d",&n)) {
        memset(sum,-1,sizeof(sum));
        memset(res,0,sizeof(res));
        build(1,0,maxn-1);
        for(int i=0; i<n; i++) {
            scanf("%d%d%d",&x1,&x2,&c);
            if(x1==x2)continue;
            update(x1,x2-1,c,1,0,maxn-1);
        }
        query(0,maxn-1,1,0,maxn-1);//将所有数据都更新到底部
        for(int i=0; i<maxn; i++) {
            while(i!=0&&sum[i]!=-1&&sum[i]==sum[i-1])//如果没有颜色就加一,如果与之前相等那么也是加一
                i++;
            res[sum[i]]++;
        }
        for(int i=0; i<maxn; i++)if(res[i])printf("%d %d\n",i,res[i]);
        printf("\n");
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

zoj 1610 Count the Colors(线段树,成段更新染色)

链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 题目大意: 在一条长度为8000的线段上染...
  • shuangde800
  • shuangde800
  • 2012年11月15日 12:54
  • 4550

ZOJ 1610 Count the Colors

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time...
  • Silenceneo
  • Silenceneo
  • 2015年08月18日 10:23
  • 941

ZOJ 1610 Count the Colors(线段树lazy+暴力统计)

Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segme...
  • u013582254
  • u013582254
  • 2015年01月17日 00:17
  • 970

Count the Colors(两种)

Description Painting some colored segments on a line, some previously painted segments may be cov...
  • u012346225
  • u012346225
  • 2015年05月22日 08:57
  • 268

zoj 1610 Count the Colors 线段树,成段更新染色

Count the ColorsTime Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu SubmitSt...
  • wr132
  • wr132
  • 2015年11月10日 13:51
  • 690

Count the Colors(线段树染色)

Count the Colors Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu SubmitS...
  • u014665013
  • u014665013
  • 2015年11月10日 10:41
  • 486

U - Count the Colors(成段更新+统计)

http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=216#problem/U 有n个操作,每个操作定义为x1,x2,c,表...
  • u013081425
  • u013081425
  • 2014年08月15日 19:36
  • 619

Count the Colors+ZOJ+线段树成段更新

Count the Colors Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %...
  • u012870383
  • u012870383
  • 2014年09月15日 07:33
  • 324

Count the Colors (线段树,暴力)

上题: Painting some colored segments on a line, some previously painted segments may be covered by ...
  • wjmwsgj
  • wjmwsgj
  • 2017年05月09日 15:00
  • 52

ZOJ1610 Count the Colors(线段树区间更新)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 题意:一块8000长度的板子,每次覆盖啊(a,b)的格子...
  • xtttgo
  • xtttgo
  • 2015年08月27日 11:35
  • 157
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Count the Colors
举报原因:
原因补充:

(最多只允许输入30个字)