(Line Segment tree) a symbol upt mayor's performance art

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5023

Question: Interval Update and interval inquiry;

Question: Interval Update, interval inquiry, 30 colors in total, available in Int.

During the regional tug-of-war competition, the line segment tree was not learned at the time (only single-point update will be performed). This question can only be looked at by others a, so I am in a hurry. Today, I have just read the first update of the NOS shiniu interval and can't wait to try again.

I have to say, the question is water, and people are more water TT.

Finally, I felt that the code of the NOS cool looked really nice.

  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 = 0, s = 1; 29     char c; 30     while ((c = getchar()) == ‘ ‘ || c == ‘\n‘); 31     if (c == ‘-‘) s = -s; else value = c - 48; 32     while ((c = getchar()) >= ‘0‘ && c <= ‘9‘) 33         value = 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 }

 

(Line Segment tree) a symbol upt mayor's performance art

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.