(线段树)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 = 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 }

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

时间: 2024-12-08 13:48:16

(线段树)A Corrupt Mayor's Performance Art的相关文章

HDOJ 5023 A Corrupt Mayor&#39;s Performance Art

水线段树 A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 538    Accepted Submission(s): 213 Problem Description Corrupt governors always find ways to get dirty m

HDU 5023 A Corrupt Mayor&#39;s Performance Art 线段树区间更新+状态压缩

Link:  http://acm.hdu.edu.cn/showproblem.php?pid=5023 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <string> 7 #include <cmath> 8 using namesp

HDU 5023 A Corrupt Mayor&#39;s Performance Art(线段树区间更新)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色,有两种操作: P a b c  把区间a到b涂成c颜色 Q a b 查询区间a到b的颜色 线段树区间更新,每个节点保存的信息有,存储颜色的c,30种颜色可以压缩到一个int型里面存储,然后还有一个tot,表示这个区间一共有多少种颜色. 对于P操作,依次往下寻找,找要更新的区间,找到要更新的区间之前

HDU 5023 A Corrupt Mayor&#39;s Performance Art (线段树)

A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 255    Accepted Submission(s): 114 Problem Description Corrupt governors always find ways to get dirty money.

hdu-5023 A Corrupt Mayor&#39;s Performance Art (线段树区间修改)

今天集训队打比赛的一道题,很明显是个线段树,我们队照着lrj蓝书敲了一通,机智的将修改值和加和改成了位运算:|= 但是好像哪里出了点小问题,就是不对,赛后又水了一遍,竟然过了...发现还是lrj的书好啊,市面上的模板一点也不好用,连区间修改都没有 . 等集训完了要静心好好系统的学习一下线段树 . 多看多刷lrj的书 . 细节参见代码: #include<bits/stdc++.h> using namespace std; const int maxn = 1000000 + 5; int n

HDU5023 A Corrupt Mayor&#39;s Performance Art 【线段树】

A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 507    Accepted Submission(s): 203 Problem Description Corrupt governors always find ways to get dirty money.

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

HDU 5023 A Corrupt Mayor&#39;s Performance Art(线段树+优美的位运算)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this

【线段树】 HDU 5025 A Corrupt Mayor&#39;s Performance Art

更新区间内颜色 输出区间内的颜色总数 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <string> #include <iostream> #include <algorithm> #include <sstream> #include <math.h> using namespace std; #include <