HDU 5023 A Corrupt Mayor's Performance Art (据说是线段树)
题意:给定一个1-n的墙,然后有两种操作,一种是P l ,r, a 把l-r的墙都染成a这种颜色,另一种是 Q l, r 表示,输出 l-r 区间内的颜色。
析:应该是一个线段树+状态压缩,但是我用set暴力过去了。用线段树+状态压缩,区间更新,很简单,就不说了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 5;
const int mod = 1e9 + 7;
const int dr[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int dc[] = {1, 0, -1, 0, 1, 1, -1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int l, r;
Node() { }
Node(int ll, int rr) : l(ll), r(rr) { }
bool operator < (const Node &p) const{
return r < p.r;
}
};
set<Node> sets[35];
set<Node> :: iterator it, it1; int main(){
while(scanf("%d %d", &n, &m) == 2){
if(!m && !n) break;
for(int i = 1; i <= 30; ++i) sets[i].clear();
sets[2].insert(Node(1, n));
char s[5];
int a, b, c;
Node u;
while(m--){
scanf("%s", s);
if(s[0] == 'P'){
scanf("%d %d %d", &a, &b, &c);
for(int i = 1; i <= 30; ++i){
if(sets[i].size() == 0) continue;
while(true){
it1 = sets[i].lower_bound(Node(0, a));
if(it1 == sets[i].end() || it1->l > b) break;
u = *it1;
sets[i].erase(it1);
if(u.l < a){
sets[i].insert(Node(u.l, a-1));
if(u.r > b) sets[i].insert(Node(b+1, u.r));
}
else if(u.r > b) sets[i].insert(Node(b+1, u.r));
}
}
sets[c].insert(Node(a, b));
}
else{
scanf("%d %d", &a, &b);
int cnt = 0;
for(int i = 1; i < 31; ++i){
if(sets[i].size() == 0) continue;
it1 = sets[i].lower_bound(Node(0, a));
if(it1 == sets[i].end() || it1->l > b) continue;
if(cnt) putchar(' ');
printf("%d", i);
++cnt;
}
putchar('\n');
}
}
}
return 0;
}
线段树:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
const int dr[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int dc[] = {1, 0, -1, 0, 1, 1, -1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL sum[maxn<<2], sets[maxn<<2]; void pushup(int rt){
sum[rt] = sum[rt<<1] | sum[rt<<1|1];
} void pushdown(int rt, int len){
int l = rt<<1, r = rt<<1|1;
if(sets[rt]){
sum[r] = sum[l] = sets[rt];
sets[l] = sets[r] = sets[rt];
sets[rt] = 0;
}
} void build(int l, int r, int rt){
sets[rt] = 0;
if(l == r){
sum[rt] = 2;
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
pushup(rt);
} void update(int L, int R, int val, int l, int r, int rt){
if(L <= l && R >= r){
sets[rt] = 1<<val-1;
sum[rt] = 1<<val-1;
return ;
}
pushdown(rt, r-l+1);
int m = (l + r) >> 1;
if(L <= m) update(L, R, val, lson);
if(R > m) update(L, R, val, rson);
pushup(rt);
} LL query(int L, int R, int l, int r, int rt){
if(L <= l && R >= r) return sum[rt];
pushdown(rt, r-l+1);
LL ans = 0;
int m = (l + r) >> 1;
if(L <= m) ans |= query(L, R, lson);
if(m < R) ans |= query(L, R, rson);
return ans;
} int main(){
while(scanf("%d %d", &n, &m) == 2 && m+n){
build(1, n, 1);
int l, r, val;
char s[5];
while(m--){
scanf("%s", s);
if(s[0] == 'P'){
scanf("%d %d %d", &l, &r, &val);
update(l, r, val, 1, n, 1);
}
else{
scanf("%d %d", &l, &r);
LL ans = query(l, r, 1, n, 1);
int cnt = 0;
for(int i = 0; i < 30; ++i) if(ans & (1<<i)){
if(cnt) putchar(' ');
printf("%d", i+1);
++cnt;
}
printf("\n");
}
}
}
return 0;
}
HDU 5023 A Corrupt Mayor's Performance Art (据说是线段树)的更多相关文章
- hdu 5023 A Corrupt Mayor's Performance Art 线段树
A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100 ...
- ACM学习历程—HDU 5023 A Corrupt Mayor's Performance Art(广州赛区网赛)(线段树)
Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sel ...
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
- HDU 5023 A Corrupt Mayor's Performance Art 线段树区间更新+状态压缩
Link: http://acm.hdu.edu.cn/showproblem.php?pid=5023 #include <cstdio> #include <cstring&g ...
- 2014 网选 广州赛区 hdu 5023 A Corrupt Mayor's Performance Art
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #d ...
- hdu - 5023 - A Corrupt Mayor's Performance Art(线段树)
题目原文废话太多太多太多,我就不copyandpaste到这里啦..发个链接吧题目 题目意思就是:P l r c 将区间 [l ,r]上的颜色变成c Q l r 就是打印出区间[l,r ...
- hdu----(5023)A Corrupt Mayor's Performance Art(线段树区间更新以及区间查询)
A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100 ...
- A Corrupt Mayor's Performance Art(线段树区间更新+位运算,颜色段种类)
A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100 ...
- HDU5023:A Corrupt Mayor's Performance Art(线段树区域更新+二进制)
http://acm.hdu.edu.cn/showproblem.php?pid=5023 Problem Description Corrupt governors always find way ...
随机推荐
- [CF148E] Porcelain (分组背包)
题目链接:http://codeforces.com/problemset/problem/148/E 题目大意:有n组数据,每次可以从任意一组的两端取出1个数,问你取m个数最大能组成多少? 思路:先 ...
- 64. ZigZag Conversion
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- Ajax+json实现菜单动态级联
1:jsp //级联ajax处理函数 function areaChange(){ var areano=document.all("areaNo").value; v ...
- Android:去掉默认的标题bar
要使用自己定义的bar,只需要在layout文件中添加:<include layout="@layout/actionbar" />;当然你需要新建一个actionba ...
- 2016-08-15: C++ traits
#include <stdio.h> template <typename T> struct TraitsHelper { static const bool isPoint ...
- archlinux pacman 常用选项
pacman -S package_name #安装软件包pacman -R package_name #删除软件包 pacman -Rs package_name #顺便删除软件包相关依赖pacma ...
- 在MySQL中阻止UPDATE语句没有添加WHERE条件的发生
如果在生产环境中使用UPDATE语句更新表数据,此时如果忘记携带本应该添加的WHERE条件,那么..Oh,no…后果可能不堪设想.那么有没有什么办法可以阻止这样的事情发生,又不使用任何的审核工具呢.. ...
- jQuery触发<a>标签的点击事件无效
<a id="workFrame" href="pages/work.html" target="FrameBox">首页< ...
- 循序渐进Python3(三) -- 0 -- 初识函数
函数 如果我们要计算一个圆的面积,就需要知道它的半径,然后根据公式S=3.14*r*r算出它的面积,如果我们要算100个圆的面积,则每次我们都需要写公式去计算,是不是很麻烦,但是有了函数的话,我们就不 ...
- 手机抓包xcode自带命令行工具配合wireshark实现
三.最佳方式:rvictl命令 优点:简单,而且可以抓所有网络接口的数据: 缺点:似乎没有,要求手机iOS5以上不算要求吧?如果说缺点,就是这个命令是Xcode的Command Line Tools ...