题意:
对一个栅栏有两种操作,一种是把从a到b的所有栅栏都刷成c,另一种是询问从a到b有多少种颜色。(栅栏初始的颜色是2)。
思路:
线段树的一道题。
利用位运算存储颜色,反正颜色只用30种,int正好够用。
利用延迟标记更新节点。
想下延伸的时候,需要把本身就置成要修改的颜色,因为标记的时候,这个节点下面的节点就应该都被修改。
Code:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<map>
#include<cctype>
#include<vector>
//#define TEST
#define LL long long
#define Mt(f, x) memset(f, x, sizeof(f));
#define rep(i, s, e) for(int i = (s); i <= (e); ++i)
#ifdef TEST
#define See(a) cout << #a << " = " << a << endl;
#define See2(a, b) cout << #a << " = " << a << ' ' << #b << " = " << b << endl;
#define debug(a, s, e){ rep(_i, s, e) {cout << a[_i] << ' '; }cout << endl;}
#define debug2(a, s, e, ss, ee) rep(i_, s, e) {debug(a[i_], ss, ee);}
#else
#define See(a)
#define See2(a, b)
#define debug(a, s, e)
#define debug2(a, s, e, ss, ee)
#endif
const int MAX = 2e9;
const int MIN = -2e9;
const int PI = acos(-1.0);
const double eps = 1e-8;
using namespace std;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
const int N = 1000000 + 5;
int col[N << 2], dif[N << 2];
void pushUp(int rt)
{
// See(rt);
col[rt] = col[rt << 1] | col[rt << 1 | 1];
}
void build(int l, int r, int rt)
{
if(l == r)
{
col[rt] = 1 << 2;
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
pushUp(rt);
}
void pushDown(int rt)
{
// See(rt);
if(dif[rt])
{
dif[rt << 1] = dif[rt << 1 | 1] = dif[rt];
col[rt << 1] = col[rt << 1 | 1] = dif[rt];
dif[rt] = 0;
}
}
void update(int L, int R, int c, int l, int r, int rt)
{
if(l >= L && r <= R)
{
// See(rt);
dif[rt] = 1 << c;
col[rt] = dif[rt];
return ;
}
pushDown(rt);
int m = (l + r) >> 1;
if(L <= m) update(L, R, c, lson);
if(R > m) 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 col[rt];
}
pushDown(rt);
int ret = 0;
int m = (l + r) >> 1;
if(L <= m) ret |= query(L, R, lson);
if(R > m) ret |= query(L, R, rson);
return ret;
}
void print(int t)
{
bool first = true;
for(int i = 1; i <= 30; ++i)
{
if(t & (1 << i))
{
printf(first ? "%d" : " %d", i);
first = false;
}
}
printf("\n");
}
int main()
{
int n, q;
while(~scanf("%d%d", &n, &q))
{
Mt(dif, 0);
if(n + q == 0)
{
break;
}
build(1, n, 1);
while(q--)
{
char op[2];
int a, b, c;
scanf("%s%d%d", op, &a, &b);
if(op[0] == 'P')
{
scanf("%d", &c);
update(a, b, c, 1, n, 1);
}
else
{
int t = query(a, b, 1, n, 1);
print(t);
}
}
}
return 0;
}