poj 2777

poj 2777

觉得其实线段树就是一个模版,所有东西都是往里套。

简而言之,就是3667我还没有ac所以,噫,我来更2777.

-----------------------------

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. 

 

不想逐字翻译,看懂题意就行了。就是讲,有一个带子,固定长度为L。有两种操作,和3468非常像啊,非常像。

直接粘代码好了

-----------------------------

对了,写代码之前,我先声明会使用位运算的相关知识,不懂的请自行百度,google

-----------------------------

#include<iostream>

using namespace std;

int n;

#define MAXN 100100

int L=1;

struct st

{

    int left,right,c,lazy;

}t[6*MAXN]; //结构体,没啥好说

void build_tree()  //建树

{

    while(L<n) L*=2;

    for(int i=L;i<2*L;i++)

    {

        t[i].left=t[i].right=i-L+1;

        t[i].c=1;

    }

    for(int i=L-1;i>=1;i--)

    {

        t[i].left=t[i*2].left;

        t[i].right=t[2*i+1].right;

        t[i].c=1;

    }

}

void pushdown(int id)

{

    if(t[id].lazy==0) return ;

    t[id].c=t[id].lazy;

    t[id].lazy=0;

    if(t[id].left==t[id].right) return ;

    t[2*id].lazy=t[2*id+1].lazy=t[id].c;

}

void update(int id)

{

    pushdown(id*2);

    pushdown(id*2+1);

    t[id].c=t[2*id].c|t[2*id+1].c;

}

void change(int id,int left,int right,int c)

{

    if(t[id].left==left && t[id].right==right)

    {

        t[id].lazy=1<<(c-1); //左移c-1

        return ;

    }

    pushdown(id);

    if(t[2*id].right>=right)

        change(2*id,left,right,c);

    else if(t[2*id+1].left<=left)

        change(2*id+1,left,right,c);

    else

        change(2*id,left,t[2*id].right,c),

        change(2*id+1,t[2*id+1].left,right,c);

    update(id);

}

int query(int id,int left,int right)

{

    if(t[id].left==left && t[id].right==right)

    {

        pushdown(id);

        return t[id].c;

    }

    pushdown(id);

    if(t[2*id].right>=right)

        return query(2*id,left,right);

    else if(t[2*id+1].left<=left)

        return query(2*id+1,left,right);

    else

        return query(2*id,left,t[2*id].right)|

        query(2*id+1,t[2*id+1].left,right);

}

int cnc(int s)

{

    int sum=0;

    while(s)

    {

        sum+=s%2;

        s/=2;

    }

    return sum;

}

int main()

{

    char c;

    int t,o;

    scanf("%d%d%d",&n,&t,&o);

    int a,b,ddd;

    build_tree();

    while(o--)

    {

        getchar();

        scanf("%c",&c);

        if(c=='P')

        {

            scanf("%d%d",&a,&b);

            if(a>b) swap(a,b);

            printf("%d\n",cnc(query(1,a,b)));

        }

        else if(c=='C')

        {

            scanf("%d%d%d",&a,&b,&ddd);

            change(1,a,b,ddd);

        }

    }

    return 0;

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值