ZOJ 1610 间隔染色段树

要长8000仪表板。间染色的范围,问:最后,能看到的颜色,而且颜色一共有段出现

覆盖段

数据对比水   水可太暴力

段树:

#include "stdio.h"
#include "string.h"
struct node
{
    int l,r,c;
}data[40010];

int color[8011];
void build(int l,int r,int k)
{
    int mid;
    data[k].l=l;
    data[k].r=r;
    data[k].c=-1;
    if (l==r) return ;

    mid=(l+r)/2;

    build(l,mid,k*2);
    build(mid+1,r,k*2+1);
}

void updata(int l,int r,int k,int op)
{
    int mid;
    if (data[k].c==op) return ;

    if (data[k].l==l && data[k].r==r)
    {
        data[k].c=op;
        return ;
    }

    if (data[k].c!=-1)
    {
        data[k*2].c=data[k*2+1].c=data[k].c;
        data[k].c=-1;
    }

    mid=(data[k].l+data[k].r)/2;

    if (r<=mid) updata(l,r,k*2,op);
    else
        if (l>mid) updata(l,r,k*2+1,op);
    else
    {
        updata(l,mid,k*2,op);
        updata(mid+1,r,k*2+1,op);
    }

    if (data[k*2].c==data[k*2+1].c) data[k].c=data[k*2].c;
    else data[k].c=-1;
}

void query(int k)
{
    if (data[k].l==data[k].r)
    {
        color[data[k].l]=data[k].c;
        return ;
    }
    if (data[k].c!=-1)
        data[k*2].c=data[k*2+1].c=data[k].c;
    query(k*2);
    query(k*2+1);
}
int main()
{
    int n,l,r,c,m,now,i;
    int sum[8011];
    m=8010;
    while (scanf("%d",&n)!=EOF)
    {
        build(0,m,1);
        while (n--)
        {
            scanf("%d%d%d",&l,&r,&c);
            r--;
            updata(l,r,1,c);
        }
        memset(color,-1,sizeof(color));
        query(1);

        memset(sum,0,sizeof(sum));

        now=color[0];
        sum[now]++;
        for (i=1;i<=m;i++)
        if (color[i]!=now)
        {
            now=color[i];
            sum[now]++;
        }

        for (i=0;i<=m;i++)
            if (sum[i]!=0) printf("%d %d\n",i,sum[i]);
        printf("\n");
    }
    return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-07-30 19:05:45

ZOJ 1610 间隔染色段树的相关文章

POJ 2777 &amp;&amp; ZOJ 1610 &amp;&amp;HDU 1698 --线段树--区间更新

直接将这3题 放一起了  今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 -- 区间更新的话 分为 增减 或者 修改 主要就是个 laze 标记 就是延迟更新 对于区间更新的写法 一般是有2种 其一 仔细划分到每个细小的区间    另一 粗略划分 反正 ==我的代码里会给出2种写法 看自己喜好 hdu 1 //线段树 成段更新 ---> 替换 根结点的查询 2 3 #i

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 (线段树区间更新)

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

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.

非结构体线段树版 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

【不可能的任务3/200】bzoj2243树链剖分+染色段数

终于做了一道不是一眼出思路的代码题(⊙o⊙) 之前没有接触过这种关于染色段数的题目(其实上课好像讲过),于是百度了一下(现在思维能力好弱) 实际上每一段有用的信息就是总共有几段和两段各是什么颜色,在开线段树的时候记录一下就好了 事实上我开了一个node,并且写了一个mix还是大大减小了代码量(对于我这种手残党来说同时大大减小了错误率) 由于前几题做的都是树链剖分,并没有在这一方面出问题,然而线段树还是不熟练,刚写完的时候居然忘记down了(mdzz) 对wa了N遍的总结: 由于是树上两个点间的路

PKU A Simple Problem with Integers (段树更新间隔总和)

意甲冠军:一个典型的段树C,Q问题,有n的数量a[i] (1~n),C, a, b,c在[a,b]加c Q a b 求[a,b]的和. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream> #include <queue> #

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-线段树-区域更新,单点查询

线段树的区域更新,然后单点查询. x1 x2 c:区域更新x1-x2为c. 全部染色之后,从0-8000依次查询每个点的颜色.然后存贮每一种颜色有几块. #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> using namespace std; #define lmin 0 #define rmax 8000 #