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 ... 
随机推荐
- 最牛B的编码套路
			最近,我大量阅读了Steve Yegge的文章.其中有一篇叫“Practicing Programming”(练习编程),写成于2005年,读后令我惊讶不已: 与你所相信的恰恰相反,单纯地每天埋头 ... 
- Django views 中 View decorators
			decorators(装饰器) 1. require_http_methods 在django.views.decorators.http中,可以用来限制请求的权限. require_http_met ... 
- 类似新浪 腾讯微博字数统计 控制js(区分中英文 符号)
			<script> ; function Q(s) { return document.getElementById(s); } function checkWord(c) { len = ... 
- maven安装(linux)
			1.首先到Maven官网下载安装文件,目前最新版本为3.0.3,下载文件为apache-maven-3.0.3-bin.tar.gz,下载可以使用wget命令: 2.进入下载文件夹,找到下载的文件,运 ... 
- mysql Can't connet MySQL server to '@localhost'
			10063/10060/10038好像都能解决 mysql -nt -remove mysql -nt install 
- 解决PHP在IE中下载文件,中文文件名乱码问题
			if( stripos($_SERVER['HTTP_USER_AGENT'], 'MSIE')!==false ) $filename = urlencode( $filename ); // 输入 ... 
- 开启telnet的几种方法
			开启telnet方法一:需要VTY的密码和进入超级权限的密码(VTY虚拟终端,一种网络设备的连接方式) [R1]int g0/0/0[R1-GigabitEthernet0/0/0]ip add 19 ... 
- windows下安装多个tomcat服务
			摘要 公司服务器已经部署2个tomcat,分别属于不同的系统.今天新开发的系统也要上线测试,故新增一个tomcat服务器. 1.官网下载tomcat 7 解压缩版本.我使用的是 apache-tomc ... 
- Docx读写Word
			Docx.dll功能比较强大,具备以下功能: 创建新的word文档或者读取已有的world文档 替换书签处内容: 插入表格或者在已有表格新增数据行: 插入图片,轻松设置图片大小: 保存或者另存为: 分 ... 
- linux c libcurl的简单使用(转)
			curl是Linux下一个非常著名的下载库,通过这个库,可以很简单的实现文件的下载等操作.看一个简单的例子: #include <curl/curl.h> #include <std ... 
