【POJ 2777】Count Color

【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 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
#include <iostream>

#include <cstdio>

using namespace std;

struct SegmentTree{

    int l,r,col;

    bool cov;

}t[400040];

int L,T,O,A,B,C;

void build(int l,int r,int p){

    t[p].l=l;

    t[p].r=r;

    t[p].col=(1<<1);

    t[p].cov=true;

    if(l==r)    return;

    build(l,(l+r)/2,p*2);

    build((l+r)/2+1,r,p*2+1);

}

void add(int l,int r,int col,int p){

    if(l<=t[p].l&&r>=t[p].r){

        t[p].cov=true;

        t[p].col=(1<<col);

        return;

    }

    if(t[p].cov){

        t[p*2].cov=true;

        t[p*2+1].cov=true;

        t[p*2].col=t[p].col;

        t[p*2+1].col=t[p].col;

        t[p].cov=false;

    }

    if(l<=(t[p].l+t[p].r)/2)

        add(l,r,col,p*2);

    if(r>(t[p].l+t[p].r)/2)

        add(l,r,col,p*2+1);

    t[p].col=t[p*2].col|t[p*2+1].col;

}

int query(int l,int r,int p){

    if(l==t[p].l&&r==t[p].r)

        return t[p].col;

    if(t[p].cov){

        t[p*2].cov=true;

        t[p*2+1].cov=true;

        t[p*2].col=t[p].col;

        t[p*2+1].col=t[p].col;

        t[p].cov=false;

    }

    if(l>(t[p].l+t[p].r)/2)

        return query(l,r,p*2+1);

    else if(r<=(t[p].l+t[p].r)/2)

        return query(l,r,p*2);

    else    

        return query(l,(t[p].l+t[p].r)/2,p*2)|query((t[p].l+t[p].r)/2+1,r,p*2+1);

}

int fj(int k){

    int ans=0;

    while(k){

        if(k%2)

            ans++;

        k=k/2;

    }

    return ans;

}

int main(){

    char ch;

    scanf("%d %d %d",&L,&T,&O);

    build(1,L,1);

    for(int i=1;i<=O;i++){

        scanf("\n%ch",&ch);

        if(ch==‘C‘){

            scanf("%d %d %d",&A,&B,&C);

            if(A>B)    swap(A,B);

            add(A,B,C,1);

        }else{

            scanf("%d %d",&A,&B);

            if(A>B)    swap(A,B);

            int ans=fj(query(A,B,1));

            cout<<ans<<endl;

        }

    }

    return 0;

}

原文地址:https://www.cnblogs.com/labelray/p/8722217.html

时间: 2024-08-29 15:09:42

【POJ 2777】Count Color的相关文章

【POJ 1741】Tree

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11570   Accepted: 3626 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an

bzoj2287【POJ Challenge】消失之物(dp+补集转化,好题)

2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 657  Solved: 382[Submit][Status][Discuss] Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. “要使用剩下的 N - 1 物品装满容积为 x 的背包,有几种方法呢?” -- 这是经典的问题了.她把答案记为 Count(

【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

[POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11294   Accepted: 3091 Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remembe

【POJ 2411】Mondriaan&#39;s Dream(状压dp)

[POJ 2411]Mondriaan's Dream(状压dp) Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14107   Accepted: 8152 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in hi

【POJ 1739】Tony&#39;s Tour

Tony's Tour Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3545   Accepted: 1653 Description A square township has been divided up into n*m(n rows and m columns) square plots (1<=N,M<=8),some of them are blocked, others are unblocked.

BZOJ2287: 【POJ Challenge】消失之物

2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][Status] Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. “要使用剩下的 N - 1 物品装满容积为 x 的背包,有几种方法呢?” -- 这是经典的问题了.她把答案记为 Count(i, x) ,想要

BZOJ 2287 【POJ Challenge】消失之物

2287: [POJ Challenge]消失之物 Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. “要使用剩下的 N - 1 物品装满容积为 x 的背包,有几种方法呢?” -- 这是经典的问题了.她把答案记为 Count(i, x) ,想要得到所有1 <= i <= N, 1 <= x <= M的 Count(i, x) 表格. Input 第1行:两个整数 N (1 ≤ N ≤ 2

BZOJ 2287: 【POJ Challenge】消失之物( 背包dp )

虽然A掉了但是时间感人啊.... f( x, k ) 表示使用前 x 种填满容量为 k 的背包的方案数, g( x , k ) 表示使用后 x 种填满容量为 k 的背包的方案数. 丢了第 i 个, 要填满容量为 k 的背包 , 则 ans( i , k ) = ∑ f( i - 1, h ) * g( i + 1 , k - h ) ( 0 <= h <= k ) 这样就转化为经典的背包问题了 f( x , k ) = f( x - 1 , k ) + f( x - 1 , k - w( x

【POJ 2409】 Let it Bead(Polya)

[POJ 2409] Let it Bead(Polya) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5378   Accepted: 3596 Description "Let it Bead" company is located upstairs at 700 Cannery Row in Monterey, CA. As you can deduce from the company name, t