[POJ2777]Count Color(线段树)

时间:2023-03-08 17:19:47

题目链接:http://poj.org/problem?id=2777

给你一个长为L想线段,向上面染色,颜色不超过30种,一共有O次操作,操作有两种:

C a b c 在[a,b]上染上c颜色

P a b 查询[a,b]上所有颜色数。

思路:线段树维护每个线段上颜色种类,用位来存颜色。好题。

 /*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &(a))
#define Rll(a) scanf("%I64d", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f, sizeof(a))
#define lrt rt << 1
#define rrt rt << 1 | 1
#define pi 3.14159265359
#define RT return
#define lowbit(x) x & (-x)
#define onenum(x) __builtin_popcount(x)
typedef long long LL;
typedef long double LD;
typedef unsigned long long Uint;
typedef pair<int, int> pii;
typedef pair<LL, LL> pLL;
typedef pair<string, LL> psi;
typedef map<string, LL> msi;
typedef vector<LL> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; #define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
const int maxn = ;
int sum[maxn<<];
int add[maxn<<];
int L, T, O; void pushUP(int rt) {
sum[rt] = sum[lrt] | sum[rrt];
} void pushDOWN(int rt) {
if(add[rt] != -) {
sum[lrt] = sum[rrt] = ( << add[rt]);
add[lrt] = add[rrt] = add[rt];
add[rt] = -;
}
} void build(int l, int r, int rt) {
sum[rt] = ; add[rt] = ;
if(l == r) return;
int m = (l + r) >> ;
build(l, m, lrt);
build(m+, r, rrt);
} void update(int L, int R, int c, int l, int r, int rt) {
if(L <= l && r <= R) {
add[rt] = c;
sum[rt] = << c;
return;
}
pushDOWN(rt);
int m = (l + r) >> ;
if(L <= m) update(L, R, c, lson);
if(m < R) update(L, R, c, rson);
pushUP(rt);
} int query(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) return sum[rt];
pushDOWN(rt);
int m = (l + r) >> ;
int ret = ;
if(L <= m) ret |= query(L, R, lson);
if(m < R) ret |= query(L, R, rson);
return ret;
} int main() {
// FRead();
char cmd[];
int l, r, c;
while(~scanf("%d%d%d", &L, &T, &O)) {
build(, L, );
W(O) {
Rs(cmd);
if(cmd[] == 'C') {
scanf("%d%d%d",&l,&r,&c);
if(l > r) swap(l, r);
update(l, r, c, , L, );
}
else {
scanf("%d%d",&l,&r);
if(l > r) swap(l, r);
int p = query(l, r, , L, );
int ret = ;
while(p) {
if(p & ) ret++;
p >>= ;
}
printf("%d\n", ret);
}
}
}
RT ;
}