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
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define maxn 10000
int cl[maxn*4];
int num[maxn];
void push_up(int rt)
{

}
void push_down(int rt)
{
    if(cl[rt]!=-1)
    {
        cl[rt<<1]=cl[rt];
        cl[rt<<1|1]=cl[rt];
        cl[rt]=-1;
    }
}
void update(int ll,int rr,int x,int l,int r,int rt)
{
    if(l>rr||r<ll)return;
    if(ll<=l&&rr>=r)
    {
      //  cout<<l<<"-"<<r<<"-"<<rt<<"-"<<x<<endl;
        cl[rt]=x;
        return;
    }
    push_down(rt);
    update(ll,rr,x,lson);
    update(ll,rr,x,rson);
}
int query(int st,int l,int r,int rt)
{
    if(r<st)return 0;
    if(l>st)return 0;
    if(l==r&&st==l)return cl[rt];
    if(l<=st&&r>=st&&cl[rt]!=-1)return cl[rt];
    return query(st,lson)+query(st,rson);
}
int main()
{
    int n,l,r,c;
    while(~scanf("%d",&n))
    {
        memset(cl,-1,sizeof(cl));
        memset(num,0,sizeof(num));
        while(n--)
        {
            scanf("%d%d%d",&l,&r,&c);
            update(l,r-1,c,root);
        }
        int y=-1;
        for(int i=0;i<=8000;i++)
        {
            int x=query(i,root);
            //cout<<x<<"-"<<endl;
           // getchar();
            if(x==y)continue;
            y=x;
            if(x>=0)num[x]++;
        }
        for(int i=0;i<=8000;i++)
        {
            if(num[i])
            {
                printf("%d %d\n",i,num[i]);
            }
        }
        cout<<endl;
    }
    return 0;
}

zoj-1610-Count the Colors-线段树-区域更新,单点查询

时间: 2024-10-12 19:55:20

zoj-1610-Count the Colors-线段树-区域更新,单点查询的相关文章

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

题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以对于区间(L, R)我们只要让左端点+1即可按照正常的线段树操作来做. #include<bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 using namespace std; co

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

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

POJ 2777 Count Color (线段树区间更新加查询)

Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a positive integer, so we can evenly d

Light OJ 1080 - Binary Simulation - (线段树区间更新 单点查询)

Description Given a binary number, we are about to do some operations on the number. Two types of operations can be here. 'I i j'    which means invert the bit from i to j (inclusive) 'Q i'    answer whether the ith bit is 0 or 1 The MSB (most signif

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 Color(线段树)

描述 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 exactl

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

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