亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

POJ 2777 Count Color(線段樹(shù)+位運(yùn)算)

系統(tǒng) 1695 0

題目鏈接: http://poj.org/problem?id=2777


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

    

Source


題意:

給一個(gè)固定長(zhǎng)度為L(zhǎng)的畫(huà)板

有兩個(gè)操作:

C A B C:區(qū)間A--B內(nèi)涂上顏色C。

P A B:查詢區(qū)間AB內(nèi)顏色種類數(shù)。

PS:

此題和 HDU:5023 是類似的!

附題解: http://blog.csdn.net/u012860063/article/details/39434665


代碼例如以下:

      #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define LL int

const int maxn = 110017;
LL add[maxn<<2];
LL sum[maxn<<2];
void PushUp(int rt)
{
    //把當(dāng)前結(jié)點(diǎn)的信息更新到父結(jié)點(diǎn)
    sum[rt] = sum[rt<<1] | sum[rt<<1|1];//總共的顏色
}
void PushDown(int rt,int m)
{
    if(add[rt])
    {
        add[rt<<1] = add[rt];
        add[rt<<1|1] = add[rt];
        sum[rt<<1] = add[rt];
        sum[rt<<1|1] = add[rt];
        add[rt] = 0;//將標(biāo)記向兒子節(jié)點(diǎn)移動(dòng)后,父節(jié)點(diǎn)的延遲標(biāo)記去掉
        //傳遞后,當(dāng)前節(jié)點(diǎn)標(biāo)記域清空
    }
}
void build(int l,int r,int rt)
{
    add[rt] = 0;//初始化為全部結(jié)點(diǎn)未被標(biāo)記
    if (l == r)
    {
        sum[rt] = 1;//初始顏色為1
        return ;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if (L <= l && r <= R)
    {
        add[rt] =1<<(c-1);//位運(yùn)算左移表示有某種顏色
        sum[rt] =1<<(c-1);
        return ;
    }
    PushDown(rt , r - l + 1);//----延遲標(biāo)志域向下傳遞
    int mid = (l + r) >> 1;
    if (L <= mid)
        update(L , R , c , lson);//更新左兒子
    if (mid < R)
        update(L , R , c , rson);//更新右兒子
    PushUp(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
    if (L <= l && r <= R)
    {
        return sum[rt];
    }
    //要取rt子節(jié)點(diǎn)的值時(shí),也要先把rt的延遲標(biāo)記向下移動(dòng)
    PushDown(rt , r - l + 1);
    int mid = (l + r) >> 1;
    LL ret = 0;
    if (L <= mid)
        ret |= query(L , R , lson);
    if (mid < R)
        ret |= query(L , R , rson);
    return ret;
}
int main()
{
    int L, T, O;
    int a, b, c;
    while(~scanf("%d%d%d",&L,&T,&O))
    {
        build(1, L, 1);//建樹(shù)
        while(O--)//Q為詢問(wèn)次數(shù)
        {
            char op[2];
            scanf("%s",op);
            if(op[0] == 'P')
            {
                scanf("%d%d",&a,&b);
                if(a > b)
                {
                    int t = a;
                    a = b;
                    b = t;
                }
                LL tt=query(a, b, 1, L, 1);
                int ans = 0;
                while(tt)
                {
                    if(tt&1)
                    {
                        ans++;
                    }
                    tt>>=1;
                }
                printf("%d\n",ans);
            }
            else
            {
                scanf("%d%d%d",&a,&b,&c);
                if(a > b)
                {
                    int t = a;
                    a = b;
                    b = t;
                }
                update(a, b, c, 1, L, 1);
            }
        }
    }
    return 0;
}

    



POJ 2777 Count Color(線段樹(shù)+位運(yùn)算)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 手机看片久久国产免费不卡 | 九九99香蕉在线视频网站 | 国产成人亚洲综合a∨婷婷 国产成人亚洲综合欧美一部 | 色婷婷精品 | 久久久久亚洲精品美女 | 一区二区中文字幕在线观看 | 成人免费草草视频 | 免费看国产片 | 91精品国产91久久 | 久久网站在线观看 | 中文字幕日韩欧美 | 欧美jizz18性欧美 | 一级特级欧美午夜片免费观看 | 欧美福利视频在线观看 | 国产全黄a一级毛片视频 | 色18美女社区 | 一级免费大片 | 91在线 | 亚洲 | chinese国产人妖视频网站 | 九九色视频在线观看 | 久久国产乱子伦精品免费强 | 欧美成人一区二区三区不卡视频 | 精品一区二区久久久久久久网站 | 亚洲综合啪啪 | 日韩亚洲综合精品国产 | 日日夜夜天天干 | 久久久精品2018免费观看 | 日本一区精品久久久久影院 | 亚洲欧美日韩国产综合高清 | 中文字字幕乱码视频 | 色综合久久88色综合天天 | 久久一区二区三区不卡 | 亚洲不卡视频 | 最近中文日本字幕免费完整 | 香蕉久久网 | 成人亚洲| 久久综合给合久久狠狠狠97色 | 夜夜摸夜夜操 | 欧美成人性做爰网站免费 | 日本特级黄色录像 | 国产精品夜色视频一区二区 |