首页 > 代码库 > (线段树)A Corrupt Mayor's Performance Art

(线段树)A Corrupt Mayor's Performance Art

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023

题意: 区间更新, 区间询问;

题解: 区间更新, 区间询问, 一共30种颜色, 可用int 来存。

地区选拔赛的一道题,当时还没怎么学线段树(只会单点更新), 这道题只能看着别人A, 自己干着急。  今天刚看了NOS神牛的第一道区间更新就迫不及待的尝试了下。

不得不说, 题目水, 人更水TT。

最后在感慨一下, NOS神牛的代码真tm好看。

  1 /***Good Luck***/  2 #define _CRT_SECURE_NO_WARNINGS  3 #include <iostream>  4 #include <cstdio>  5 #include <cstdlib>  6 #include <cstring>  7 #include <string>  8 #include <algorithm>  9 #include <stack> 10 #include <map> 11 #include <queue> 12 #include <vector> 13 #include <set> 14 #include <functional> 15 #include <cmath> 16 #include <numeric> 17  18 #define Zero(a) memset(a, 0, sizeof(a)) 19 #define Neg(a)  memset(a, -1, sizeof(a)) 20 #define All(a) a.begin(), a.end() 21 #define PB push_back 22 #define inf 0x3f3f3f3f 23 #define inf2 0x7fffffffffffffff 24 #define ll long long 25 using namespace std; 26 //#pragma comment(linker, "/STACK:102400000,102400000") 27 void get_val(int &a) { 28     int value = http://www.mamicode.com/0, s = 1; 29     char c; 30     while ((c = getchar()) ==   || c == \n); 31     if (c == -) s = -s; else value = http://www.mamicode.com/c - 48; 32     while ((c = getchar()) >= 0 && c <= 9) 33         value = http://www.mamicode.com/value * 10 + c - 48; 34     a = s * value; 35 } 36 #define lson l, m, rt << 1 37 #define rson m + 1, r, rt << 1| 1 38 const int maxn = 1e6 + 10; 39 int A[maxn << 2]; 40 int n, m; 41 int col[maxn << 2]; 42 void pu(int rt) { 43     A[rt] = A[rt << 1] | A[rt << 1 | 1]; 44 } 45  46 void pd(int rt) { 47     if (col[rt]) { 48         col[rt << 1] = col[rt << 1 | 1] = col[rt]; 49         A[rt << 1] = col[rt]; 50         A[rt << 1 | 1] = col[rt]; 51         col[rt] = 0; 52     } 53 } 54  55 void update(int L, int R, int v, int l, int r, int rt) { 56     if (L <= l && r <= R) { 57         A[rt] = 1 << (v - 1); 58         col[rt] = 1 << (v - 1); 59         return; 60     } 61     pd(rt); 62     int m = (l + r) >> 1; 63     if (L <= m) update(L, R, v, lson); 64     if (R > m) update(L, R, v, rson); 65     pu(rt); 66 } 67  68 int query(int L, int R, int l, int r, int rt) { 69     if (L <= l && r <= R) { 70         return A[rt]; 71     } 72     pd(rt); 73     int m = (l + r) >> 1; 74     int ret = 0; 75     if (L <= m) ret |= query(L, R, lson); 76     if (R > m) ret |= query(L, R, rson); 77     return ret; 78 } 79 int main() { 80     //freopen("data.out", "w", stdout); 81     //freopen("data.in", "r", stdin); 82     //cin.sync_with_stdio(false); 83     while (scanf("%d%d", &n, &m), n|m) { 84         int t = n << 2; 85         char q[3]; 86         int a, b, v, ans; 87         for (int i = 0; i <= t; ++i) { 88             A[i] = 2; 89             col[i] = 0; 90         } 91         vector<int> vr; 92         for (int i = 1; i <= m; ++i) { 93             scanf("%s", q); 94             if (q[0] == P) { 95                 scanf("%d%d%d", &a, &b, &v); 96                 update(a, b,v, 1, n, 1); 97             } else { 98                 scanf("%d%d", &a, &b); 99                 ans = query(a, b, 1, n, 1);100                 for (int w = 1; w <= 30; ++w) {101                     if (ans & 1) vr.push_back(w );102                     ans >>= 1;103                 }104                 for (int w = 1; w < vr.size(); ++w) printf("%d ", vr[w - 1]);105                 printf("%d\n", *vr.rbegin());106                 vr.clear();107             }108         }109     }110     return 0;111 }

 

(线段树)A Corrupt Mayor's Performance Art