poj 2777

        题意:在一个长度为n的数轴上染色,有m种颜色,用1到m表示,o次操作,每次操作为P A B或C A B C,C A B C 表示把区间[A,B]染成颜色C,P A B表示询问区间[A,B]有多少种颜色。

       分析:线段树的入门题,旨在考lazy操作,也便是对区间的更新用一个标记记录下来,以便不用每次都一直更新至叶子结点。另外就是要用上位操作,因为颜色数目较少,最多只有30种,用一个int型变量就能表示所有颜色了,因此,我将线段树的结点存颜色的种类和数量,即对应颜色的集合,查询一个结点的颜色信息也只花常量时间。

                  另外要注意的是,由于线段树结点存的是区间颜色的集合,因此在查询多个区间的颜色总数量时,要先将集合做并运算(在这里也就是将表示对应集合的数字做或运算),再算颜色数量,否则可能会有重复。

     代码如下:


#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <list>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <iomanip>
#include <cmath>
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lrt rt << 1
#define rrt rt << 1|1
#define root 1,n,1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const double eps = 1e-5;
const int MAXN = 300 + 10;
const int MOD = 1000007;
const double M=1e-8;
const int N=1e6+10;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
const int d[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int n,m,st[N],lazy[N];
struct node
{
    int x,y;
};
void pushdown(int rt)
{
    if (lazy[rt]) {
        st[lrt]=st[rt];
        st[rrt]=st[rt];
        lazy[lrt]=lazy[rt];
        lazy[rrt]=lazy[rt];
        lazy[rt]=0;
    }
}
void fun(int rt,int c)
{
    st[rt]=(1<<(c-1));
    lazy[rt]=c;
}
int get(int x)
{
    int ans=0;
    for (int i=0;i<=m;i++) if (x&(1<<i)) ans++;
    return ans;
}
void updata(int a,int b,int c,int l,int r,int rt)
{
    if (a>r || b<l) return ;
    if (a<=l && r<=b) {
        fun(rt,c);
        return ;
    }
    pushdown(rt);
    int mid=MID(l,r);
    if (a<=mid) updata(a,b,c,lson);
    if (b>mid) updata(a,b,c,rson);
    int t=st[rrt]|st[lrt];
    st[rt]=t;
}
int query(int a,int b,int l,int r,int rt)
{
    if (a>r || b<l) return -1;
    if (a<=l && r<=b) {
        return st[rt];
    }
    pushdown(rt);
    int mid=MID(l,r);
    int u=query(a,b,lson);
    int v=query(a,b,rson);
    if (u<0) return v;
    if (v<0) return u;
    return u|v;
}
int main()
{
    int i,j,o;
    while(~scanf("%d%d%d",&n,&m,&o))
    {
        MS(lazy,0);
        MS(st,0);
        updata(root,root);
        for (j=0;j<o;j++) {
            char c[2];
            cin>>c;
            if (c[0]=='C') {
                int x,y,z;
                if (x>y) swap(x,y);
                scanf("%d%d%d",&x,&y,&z);
                updata(x,y,z,root);
            }
            else {
                int x,y;
                scanf("%d%d",&x,&y);
                if (x>y) swap(x,y);
                cout<<get(query(x,y,root)) <<endl;
            }
        }

    }
}


/*
3 3 33
C 1 1 1
C 2 2 2
C 3 3 3
P 1 1
P 2 2
P 3 3
P 1 2
P 1 3

8 8 88
C 2 2 2
C 3 3 2
C 4 4 2
P 2 3


*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值