线段树区间更新,区间统计 poj 2777 Count Color

题意:

将一段长为L的板子染色,板子可分为编号为1,2,3...L的L段,总共有O次操作,操作有两种:1.将A到B区间染为颜色C 2.询问A到B区间有多少种颜色。颜色从1到T编号,不超过30种。


思路:1.由于颜色不超过30种,所以可以考虑位运算,每一位代表一种颜色,一个32位整数就可以存储所有的颜色状态。

2.对于操作一,就是区间更新操作,需要用lazy操作,当需要更新子节点时才往下更新,把子节点的颜色染为lazy的颜色(即上一个节点染得颜色),并且把lazy下移。(代码中的pushdown函数),更新子节点后,用pushup函数将子节点的染色状态更新到父节点。

3.对于操作二,就是区间询问,也用到lazy操作,需要询问子节点时使用pushdown函数将子节点染色,然后查询到目标区间后,返回目标区间的染色状态(一个32位整数),最后将所查区间的染色状态按位与,二进制中1的个数即所用颜色的数目。

易错点:

1.注意使用lazy操作,不然超时。

2.建树的时候也要用pushup操作更新父节点

3.在pushup函数中,是把左右子节点的染色状态按位或,而pushdown函数中,是直接将父节点的上次所染颜色更新到子节点。原因是pushup是将子区间并起来,而pushdown是区间染色。

4.查询的时候也要使用pushdown,因为有可能所查子节点状态未被更新。

5.A可能大于B

代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/
long long int col[400000+10];
int num;
int lazy[400000+10];
int l,t,o;
void pushdown(int rt)
{
    if(lazy[rt])
    {
        lazy[rt<<1]=lazy[(rt<<1)+1]=lazy[rt];
        col[rt<<1]=lazy[rt];
        col[(rt<<1)+1]=lazy[rt];
        lazy[rt]=0;
    }
}
void pushup(int rt)
{
    col[rt]=col[rt<<1]|(col[(rt<<1)+1]);
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        col[rt]=1;


        return;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,(rt<<1)+1);
    pushup(rt);
}
void update(int l,int r,int rt,int a,int b,int c)
{
    if(a<=l&&r<=b)
    {
        col[rt]=c;
        lazy[rt]=c;
        return;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(a<=m)
        update(l,m,rt<<1,a,b,c);
    if(b>m)
        update(m+1,r,(rt<<1)+1,a,b,c);
    pushup(rt);

}
long long int query(int l,int r,int rt,int a,int b)
{
    if(a<=l&&r<=b)
    {

        return col[rt];
    }
    pushdown(rt);
    int m=(l+r)>>1;
    long long int ans=0;
    if(a<=m)
        ans=ans|query(l,m,rt<<1,a,b);
    if(b>m)
        ans=ans|query(m+1,r,(rt<<1)+1,a,b);
     return ans;

}



int main()
{
cin>>l>>t>>o;
MS0(col);
MS0(lazy);
num=0;
build(1,l,1);


for(int i=0;i<o;i++)
{   getchar();
    char c;
    int a,b,co;
    scanf("%c",&c);
    if(c=='C')
    {

        RIII(a,b,co);
        if(a>b)
        {
            Swap(a,b);
        }
        update(1,l,1,a,b,1<<(co-1));
    }
    else
    if(c=='P')
    {
        RII(a,b);
        num=0;
        if(a>b)
            Swap(a,b);
        long long int ans=query(1,l,1,a,b);
        for(int j=0;j<t;j++)
            if(ans&(1<<j))
            num++;
        printf("%d\n",num);

    }
}



        return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值