传送门:【HDU】5023 A Corrupt Mayor's Performance Art
题目分析:水水的线段树,首先颜色只有30种,所以状压就好了,然后每次查询就把区间内所有的颜色“或”出来,用位运算判断一下有哪些颜色就好了。。
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> using namespace std ; typedef long long LL ; #define rep( i , a , b ) for ( int i = a ; i < b ; ++ i ) #define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REV( i , a , b ) for ( int i = a ; i >= b ; -- i ) #define travel( e , H , u ) for ( Edge* e = H[u] ; e ; e = e -> next ) #define clr( a , x ) memset ( a , x , sizeof a ) #define CPY( a , x ) memcpy ( a , x , sizeof a ) #define ls ( o << 1 ) #define rs ( o << 1 | 1 ) #define lson ls , l , m #define rson rs , m + 1 , r #define mid ( ( l + r ) >> 1 ) #define root 1 , 1 , n #define rt o , l , r const int MAXN = 1000005 ; int cover[MAXN << 2] ; int color[MAXN << 2] ; int n , q ; void pushup ( int o ) { color[o] = color[ls] | color[rs] ; } void pushdown ( int o ) { if ( cover[o] ) { cover[ls] = cover[o] ; cover[rs] = cover[o] ; color[ls] = color[rs] = cover[o] ; cover[o] = 0 ; } } void update ( int L , int R , int v , int o , int l , int r ) { if ( L <= l && r <= R ) { cover[o] = 1 << v ; color[o] = 1 << v ; return ; } int m = mid ; pushdown ( o ) ; if ( L <= m ) update ( L , R , v , lson ) ; if ( m < R ) update ( L , R , v , rson ) ; pushup ( o ) ; } int query ( int L , int R , int o , int l , int r ) { if ( L <= l && r <= R ) return color[o] ; int m = mid ; pushdown ( o ) ; if ( R <= m ) return query ( L , R , lson ) ; if ( m < L ) return query ( L , R , rson ) ; return query ( L , R , lson ) | query ( L , R , rson ) ; } void solve () { char s[5] ; int x , y , c ; clr ( color , 0 ) ; clr ( cover , 0 ) ; update ( 1 , n , 1 , root ) ; while ( q -- ) { scanf ( "%s%d%d" , s , &x , &y ) ; if ( s[0] == 'Q' ) { int S = query ( x , y , root ) ; int flag = 0 ; rep ( i , 0 , 30 ) { if ( S & ( 1 << i ) ) { if ( flag ) printf ( " " ) ; flag = 1 ; printf ( "%d" , i + 1 ) ; } } printf ( "\n" ) ; } else { scanf ( "%d" , &c ) ; update ( x , y , c - 1 , root ) ; } } } int main () { while ( ~scanf ( "%d%d" , &n , &q ) && ( n || q ) ) solve () ; return 0 ; }