zoukankan      html  css  js  c++  java
  • POJ训练计划2777_Count Color(线段树/成段更新/区间染色)

    解题报告

    题意:

    对线段染色。询问线段区间的颜色种数。

    思路:

    本来直接在线段树上染色,lz标记颜色。每次查询的话訪问线段树,求出颜色种数。结果超时了,最坏的情况下,染色能够染到叶子节点。

    换成存下区间的颜色种数,这样每次查询就不用找到叶子节点了。用按位或来处理颜色种数。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int sum[500000],lz[500000],ans;
    void push_down(int rt,int l,int r)
    {
        if(lz[rt])
        {
            lz[rt<<1]=lz[rt<<1|1]=lz[rt];
            sum[rt<<1]=lz[rt];
            sum[rt<<1|1]=lz[rt];
            lz[rt]=0;
        }
    }
    void push_up(int rt,int l,int r)
    {
        sum[rt]=sum[rt<<1]|sum[rt<<1|1];
    }
    void cbtree(int rt,int l,int r)
    {
        if(l==r)
        {
            sum[rt]=1;
            return ;
        }
        int mid=(l+r)>>1;
        cbtree(rt<<1,l,mid);
        cbtree(rt<<1|1,mid+1,r);
        push_up(rt,l,r);
    }
    void update(int rt,int l,int r,int ql,int qr,int v)
    {
        if(ql>r||qr<l)return ;
        if(ql<=l&&r<=qr)
        {
            lz[rt]=v;
            sum[rt]=v;
            return ;
        }
        int mid=(l+r)>>1;
        push_down(rt,l,r);
        update(rt<<1,l,mid,ql,qr,v);
        update(rt<<1|1,mid+1,r,ql,qr,v);
        push_up(rt,l,r);
    }
    int _q(int rt,int l,int r,int ql,int qr)
    {
        if(ql>r||qr<l)return 0;
        if(ql<=l&&r<=qr)
        {
            return sum[rt];
        }
        push_down(rt,l,r);
        int mid=(l+r)>>1;
        return _q(rt<<1,l,mid,ql,qr) | _q(rt<<1|1,mid+1,r,ql,qr);
    
    }
    int main()
    {
        int n,t,q,ql,qr,i,j,a,k;
        char str[10];
        scanf("%d%d%d",&n,&t,&q);
        cbtree(1,1,n);
        while(q--)
        {
            scanf("%s",str);
            if(str[0]=='C')
            {
                scanf("%d%d%d",&ql,&qr,&a);
                if(ql>qr)swap(ql,qr);
                update(1,1,n,ql,qr,1<<(a-1));
            }
            else
            {
                scanf("%d%d",&ql,&qr);
                if(ql>qr)swap(ql,qr);
                ans=_q(1,1,n,ql,qr);
                int cnt=0;
                while(ans)
                {
                    if(ans%2)
                        cnt++;
                    ans/=2;
                }
                printf("%d
    ",cnt);
            }
        }
        return 0;
    }
    


    Count Color
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 35143   Accepted: 10591

    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 divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board:

    1. "C A B C" Color the board from segment A to segment B with color C.
    2. "P A B" Output the number of different colors painted between segment A and segment B (including).

    In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your.

    Input

    First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.

    Output

    Ouput results of the output operation in order, each line contains a number.

    Sample Input

    2 2 4
    C 1 1 2
    P 1 2
    C 2 2 2
    P 1 2
    

    Sample Output

    2
    1
    
  • 相关阅读:
    OD调试4--去除NAG窗口
    《微信小程序项目开发实战:用WePY、mpvue、Taro打造高效的小程序》(笔记3)支持Vue.js语法的mpvue框架
    《微信小程序项目开发实战:用WePY、mpvue、Taro打造高效的小程序》(笔记2)WePY版HelloWorld
    《微信小程序项目开发实战:用WePY、mpvue、Taro打造高效的小程序》(笔记1)WePY开发环境的安装
    《从机器学习到深度学习》笔记(5)集成学习之随机森林
    《从机器学习到深度学习》笔记(4)划分数据集
    《从机器学习到深度学习》笔记(3)强化学习
    《从机器学习到深度学习》笔记(2)无监督学习
    《从机器学习到深度学习》笔记(1)有监督学习
    Koa与Node.js开发实战(3)——Nunjucks模板在Koa中的应用(视频演示)
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5316476.html
Copyright © 2011-2022 走看看