以下是题目大意:

有水平方向上很多块板子拼成的墙,一开始每一块都被涂成了颜色1,有C和P两个操作,代表的意思是:
C X Y Z —— 从X到Y将板子涂成颜色Z
P X Y    —— 查询X到Y的板子共有多少种颜色

            //有2块板子   两2颜色   4个询问
C
P
C
P

自己AC后上网查阅了许多别人的题解,看很多人用的什么“状态压缩”、“位运算”等等方法。感觉自己不会的知识还有很多。我用的方法是类似于hash的思想,每次将颜色查询时标记,由于这题的数据范围很小,不需要用离散化处理,所以比较好写。下面附上代码。

 /*********************************************/
/* author: Desgard_Duan */
/* motto : Everything is surprise for you! */
/*********************************************/ #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <string>
#include <limits.h>
#include <vector>
#include <set>
#include <map> using namespace std; const int maxn = ;
int col[maxn << ], ans = ;
int Hash[]; void pushDown (int rt) {
if (col[rt]) {
col[rt * ] = col[rt * + ] = col[rt];
col[rt] = ;
}
} void build (int l, int r, int rt) {
col[rt] = ;
if (l == r) return ;
int m = (l + r) / ;
build (l, m, rt * );
build (m + , r, rt * + );
} void update (int L, int R, int c, int l, int r, int rt) {
if (L <= l && r <= R) {
col[rt] = c;
return ;
}
pushDown (rt);
int m = (l + r) / ;
if (L <= m) update (L, R, c, l, m, rt * );
if (m < R) update (L, R, c, m + , r, rt * + );
} void query (int L, int R, int l, int r, int rt) {
if (col[rt]) {
if (Hash[col[rt]] == ) {
ans ++;
Hash[col[rt]] = ;
}
return ;
}
int m = (l + r) / ;
if (m >= L) query (L, R, l, m, rt * );
if (m < R) query (L, R, m + , r, rt * + );
} int main () {
int L, T, O, x, y, z;
char op[];
while (~scanf ("%d%d%d", &L, &T, &O)) {
memset (col, , sizeof (col));
build (, L, );
while (O --) {
scanf ("%s%d%d", op, &x, &y);
if (x > y) swap (x, y);
if (op[] == 'C') {
scanf ("%d", &z);
update (x, y, z, , L, );
} else {
memset (Hash, , sizeof (Hash));
ans = ;
query (x, y, , L, );
printf ("%d\n", ans);
}
}
}
return ;
}
04-24 07:57
查看更多