F - Count the Colors - zoj 1610(区间覆盖)

有一块很长的画布,现在想在这块画布上画一些颜色,不过后面画的颜色会把前面画的颜色覆盖掉,现在想知道画完后这块画布的颜色分布,比如 1号颜色有几块,2号颜色有几块。。。。

***********************************************************************

分析:基本上跟帖海报是一样的,不过最后要求输出的是这种颜色的画有几块,可以按照贴海报的方式先做出来,然后对每个点进行查询,不晓得复杂度会不会太高。不过还是先试一下吧。

注意:如果出现


Segmentation Fault

可以看看是不是树建的小了,不能拿N建树

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Lson root<<1,L,tree[root].Mid()
#define Rson root<<1|1,tree[root].Mid()+1,R

const int maxn = 80005;

struct Hook{int l, r, c;}p[maxn];
struct Tree{
    int L, R;
    int color;//记录颜色
    int Mid(){return (L+R)/2;}
}tree[maxn*4];
int Color[maxn];

void CoverColor(int L, int R, int e)//把区间LR涂成颜色e
{
    for(int i=L; i<=R; i++)
        Color[i] = e;
}

void Build(int root, int L, int R)
{
    tree[root].L = L, tree[root].R = R;
    //0代表没有被颜色覆盖,1代表未覆盖,2代表子树有被别的颜色覆盖
    tree[root].color = 0;

if(L == R)return ;

Build(Lson);
    Build(Rson);
}
void Up(int root)
{
    if(tree[root].L != tree[root].R)
    if(tree[root<<1].color == 1 && tree[root<<1|1].color == 1)
        tree[root].color = 1;
}
void Insert(int root, int L, int R, int e)
{
    if(tree[root].color == 1)return ;

if(tree[root].L == L && tree[root].R == R && !tree[root].color)
    {
        tree[root].color = 1;
        CoverColor(L, R, e);
        return ;
    }
    tree[root].color = 2;

if(R <= tree[root].Mid())
        Insert(root<<1, L, R, e);
    else if(L > tree[root].Mid())
        Insert(root<<1|1, L, R, e);
    else
    {
        Insert(Lson, e);
        Insert(Rson, e);
    }

Up(root);
}
int main()
{
    int N;

while(scanf("%d", &N) != EOF)
    {
        int i, ans[maxn] = {0};

for(i=1; i<=N; i++)
            scanf("%d%d%d", &p[i].l, &p[i].r, &p[i].c);

Build(1, 0, maxn-1);
        memset(Color, -1, sizeof(Color));

for(i=N; i>0; i--)//因为给的是紧密区间,而建的树点的,所以把左边的+1,变成点覆盖的
            Insert(1, p[i].l+1, p[i].r, p[i].c);

for(i=0; i<maxn; i++)
        {
            if( Color[i]!=-1 && (!i || Color[i]!=Color[i-1]) )
                ans[Color[i]]++;
        }

for(i=0; i<maxn; i++)if(ans[i])
            printf("%d %d\n", i, ans[i]);
        printf("\n");
    }

return 0;

}

时间: 2024-10-18 04:26:34

F - Count the Colors - zoj 1610(区间覆盖)的相关文章

F - Count the Colors ZOJ 1610 (线段树+结点为长度为一的区间+树的遍历)

F - 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 ones.

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

Count the Colors ZOJ - 1610

GO 题意:给一个区间,有n次染色操作,每次将[x1, x2]染为c,求最后每种颜色各有多少线段可以看到. #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<cstdlib> #include<queue> #include<set> #include<vec

F - Count the Colors

F - Count the Colors ZOJ - 1610 思路:调了一个小时,但是发现自己线段树木有写错,颜色统计出了错误.但是不明白自己颜色统计为什么错了. 求大佬指点迷津.思路很简单,就是一个裸的线段树.只要推出样例就做出来了. 以下是两种颜色统计: 这是我的错误的: for(int i=1;i<=8009;i++){ if(vis[i]!=vis[i-1]&&vis[i-1]!=-1) ans[vis[i-1]]++; if(i==8000&&vis[i]

ZOJ - 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. InputThe first line of each data set contains exactly o

(模拟) zoj 1610

F - Count the Colors Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task

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

Count the Colors (zoj 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 ones. Your task is counting the segments of different colors you can s

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