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;
}
上一篇:给JDK提的一个bug(关于AbstractQueuedSynchronizer.ConditionObject)


下一篇:HDU 1166 敌兵布阵 (数状数组,或线段树)