Count Color(线段树)

本文介绍了一种使用线段树数据结构解决区间染色及查询不同颜色数量问题的方法。通过构建线段树并定义节点状态表示颜色的存在状态,实现区间染色更新和快速查询区间内不同颜色的数量。

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

题意:L个球,T种颜色,O次操作,初始每个球颜色为1, C为修改a,b区间的颜色为c, P表示询问a,b区间的颜色种数。

题解:线段数,tree[node]表示颜色的状态,0表示不存在,1表示存在

//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node << 1
#define rson mid + 1, r, node << 1 | 1
const int INF  =  0x3f3f3f3f;
const int O    =  1e6;
const int mod  =  10007;
const int maxn =  1e5+5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;

const int _ = 1;
const int __ = 2;
const int ___ = 3;

int tree[maxn<<2];
int lazy[maxn<<2];

int pushup(int sta1, int sta2) { return sta1 | sta2; }

void pushdown(int l ,int r, int node){
    if(lazy[node]){
        tree[node<<1] = tree[node<<1|1] = lazy[node];
        lazy[node<<1] = lazy[node<<1|1] = lazy[node];
        lazy[node] = 0;
    }
}

void build(int l, int r, int node){
    if(l == r) {
        tree[node] = 2;
        lazy[node] = 0;
        return;
    }
    int mid = (l + r) >> 1;
    build(lson); build(rson);
    tree[node] = pushup(tree[node<<1] , tree[node<<1|1]);
}

int ql, qr, v;

void update(int l, int r, int node){
    if(ql <=l && qr >= r) {
        tree[node] = 1 << v;
        lazy[node] = 1 << v;
        return ;
    }
    pushdown(l ,r, node);
    int mid = (l + r) >> 1;
    if(qr > mid) update(rson);
    if(ql <= mid) update(lson);
    tree[node] = pushup(tree[node<<1], tree[node<<1|1]);
}

int query(int l, int r, int node){
    if(ql <= l && qr >= r) return tree[node];
    pushdown(l, r, node);
    int ans = 0;
    int mid = (l + r) >> 1;
    if(qr > mid) ans |= query(rson);
    if(ql <= mid) ans |= query(lson);
    return ans;
}

int main(){
    int l, t, q;
    while(~scanf("%d%d%d", &l, &t, &q)){
        build(1, l, 1);
        while(q --) {
            char c; scanf(" %c", &c);
            if(c == 'C') {
                scanf("%d%d%d", &ql, &qr, &v);
                if(ql > qr) swap (ql, qr);
                update(1, l, 1);
            }
            else if(c == 'P'){
                scanf("%d%d", &ql ,&qr);
                if(ql > qr) swap (ql, qr);
                int sta = query(1, l, 1);
                int ans = 0;
                while(sta) {
                    if(sta & 1) ans ++;
                    sta >>= 1;
                }
                printf("%d\n", ans);
            }
        }
    }
    return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值