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\n",i,pos[i]);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-12 04:49:42

POJ 1610 Count the Colors的相关文章

zoj 1610 Count the Colors

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610 先用线段树维护区间颜色的覆盖,然后在把区间的颜色映射到数组,再从数组统计颜色. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 100000 5 using namespace std; 6 7 int n; 8 int min1; 9

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

题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头输出. //ZOJ 1610 #include <cstdio> #include <cstring> #include <iostream> using namespace std ; int p[8010*4],lz[8010*4] ,hashh[8010*4],has

非结构体线段树版 ZJU 1610 Count the Colors (线段树区间更新)

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

ZOJ 1610 Count the Colors【题意+线段树区间更新&amp;&amp;单点查询】

任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 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

ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】

Count the Colors Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 1610 Description Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent o

zoj 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 s

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

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 s

ZOJ 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 s

ZOJ 1610 Count the Colors(线段树,但暴力未必不行)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 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 color