|
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2301
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//*
题意:
给出N(N <= 2000)组操作,每组操作的形式如下:
A B C: 将[A,B]区间内的颜色染成C,C可以是白色或者黑色,序列开始
是全黑色的,并且范围在int范围内,问染色完毕后最长的白色序列。
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
解法:
线段树+离散化
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
思路:
线段树染色问题,我的做法比较麻烦,如果直接用离散化会简单许多
,但是这题是练习线段树的好题,它同样涉及到了lazy思想。因为要求得
区间内最长的白色序列,线段树的字段里面必须保存和这个长度有关的信
息,我的域如下:
int Color; // 当前区间的颜色
int lMax; // 包含左区间的连续白色的最大值时的最右端点
int rMax; // 包含右区间的连续白色的最大值时的最左端点
int MaxLR[2]; // 当前结点管辖区间的最优值的区间
int l, r; // 当前结点管理的左右区间
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
我们用以下函数从左右儿子中得到当前结点的信息:
void UpdateBy(Tree* ls, Tree* rs);
之所以把它写成函数是因为这里的处理比较麻烦,很容易出错,并且需要
调用多次,这个函数的作用就是通过左右儿子的信息填充本身的信息。
信息一多,处理的时候就要极为小心,因为很容易出错,Color字段其实是
充当了lazy标记的作用,用于染色的时候将它专递到子树。
[l, lMax]组成了当前结点的包含左闭区间的最优解。
[rMax, r]组成了当前结点的包含右闭区间的最优解。
[MaxLR[0], MaxLR[1]] 则是当前区间的最优解。
这样我们就可以通过传递性在儿子结点的属性都得知的情况下将父亲的值
计算出来,最后递归到根结点。具体的计算过程可以自己画棵树看一下,
在计算MaxLR的时候稍微麻烦一点。
*/
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#define MUTIPLE_COLOR -1
#define BLACK 0
#define WHITE 1
#define maxn 8200
#define ul unsigned int
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) struct point {
int l, r;
char color;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) point() {}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) point(int _l, int _r, char _c) {
l = _l;
r = _r;
color = _c;
}
};
vector <point> pt;
int n;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
ul tmp[maxn], tmpsize;
ul bin[maxn], size;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) void Process() {
sort(tmp, tmp + tmpsize);
bin[ size = 1 ] = tmp[0];
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) for(int i = 1; i < tmpsize; i++) {
if(tmp[i] != tmp[i-1])
bin[ ++size ] = tmp[i];
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) int Binary(int v) {
int l = 1;
int r = size;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) while(l <= r) {
int m = (l + r) >> 1;
if(bin[m] == v)
return m;
if(v > bin[m])
l = m + 1;
else
r = m - 1;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) struct Tree {
int Color; // 当前区间的颜色
int lMax; // 包含左区间的连续白色的最大值时的最右端点
int rMax; // 包含右区间的连续白色的最大值时的最左端点
int MaxLR[2]; // 当前结点管辖区间的最优值的区间
int l, r;
int son[2];
char lVal, rVal;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) void clear() {
son[0] = son[1] = -1;
AssignColor(BLACK);
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int len() {
return r - l + 1;
}
void AssignColor(int nColor);
void TranslateToSon();
void WhiteProcess();
void BlackProcess();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void UpdateBy(Tree* ls, Tree* rs);
void MaxVal(int xl, int xr);
}T[maxn*3];
int root, tot;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) int MMax(int a, int b) {
return a > b ? a : b;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) int GetID(int& root, int l, int r) {
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(root == -1) {
root = tot++;
T[root].l = l;
T[root].r = r;
T[root].clear();
}
return root;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) void Tree::WhiteProcess() {
lMax = r;
rMax = l;
MaxLR[0] = l;
MaxLR[1] = r;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) void Tree::BlackProcess() {
lMax = l - 1;
rMax = r + 1;
MaxLR[0] = -1;
MaxLR[1] = -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) void Tree::AssignColor(int nColor) {
Color = nColor;
if(nColor == MUTIPLE_COLOR)
return ;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(Color == WHITE) {
WhiteProcess();
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) }else if(Color == BLACK) {
BlackProcess();
}
lVal = rVal = Color;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) void Tree::TranslateToSon() {
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(Color != MUTIPLE_COLOR) {
int mid = (l + r) >> 1;
int i0 = GetID(son[0], l, mid);
T[i0].AssignColor(Color);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int i1 = GetID(son[1], mid+1, r);
T[i1].AssignColor(Color);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
Color = MUTIPLE_COLOR;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) void Tree::MaxVal(int xl, int xr) {
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(xl != -1 && xl <= xr) {
bool con = (MaxLR[0] == -1)
|| (bin[xr] - bin[xl] > bin[ MaxLR[1] ] - bin[ MaxLR[0] ])
|| (bin[xr] - bin[xl] == bin[ MaxLR[1] ] - bin[ MaxLR[0] ] && bin[xl] < bin[MaxLR[0]]);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(con) {
MaxLR[0] = xl;
MaxLR[1] = xr;
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) void Tree::UpdateBy(Tree* ls, Tree* rs) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
lVal = ls->lVal;
rVal = rs->rVal;
lMax = (ls->lMax==ls->r) ? rs->lMax : ls->lMax;
rMax = (rs->rMax==rs->l) ? ls->rMax : rs->rMax;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(ls->Color == rs->Color) {
Color = ls->Color;
}else
Color = MUTIPLE_COLOR;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
MaxLR[0] = MaxLR[1] = -1;
MaxVal(l, lMax);
MaxVal(rMax, r);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
MaxVal(ls->rMax, rs->lMax);
MaxVal(ls->MaxLR[0], ls->MaxLR[1]);
MaxVal(rs->MaxLR[0], rs->MaxLR[1]);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) void Insert(int& root, int nl, int nr, int l, int r, int val) {
if(nl > r || nr < l)
return ;
GetID(root, l, r);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(T[root].Color == val)
return ;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(nl <= l && r <= nr) {
T[root].AssignColor(val);
return ;
}
T[root].TranslateToSon();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int mid = (l + r) >> 1;
Insert(T[root].son[0], nl, nr, l, mid, val);
Insert(T[root].son[1], nl, nr, mid + 1, r, val);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
T[root].UpdateBy(&T[ T[root].son[0] ], &T[ T[root].son[1] ]);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) int main() {
int i;
int a, b;
char str[10];
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) while(scanf("%d", &n) != EOF) {
tmpsize = 0;
pt.clear();
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) for(i = 0; i < n; i++) {
scanf("%d %d %s", &a, &b, str);
tmp[ tmpsize++ ] = a - 1;
tmp[ tmpsize++ ] = a;
tmp[ tmpsize++ ] = b;
tmp[ tmpsize++ ] = (ul)b + 1;
pt.push_back(point(a, b, str[0]=='w' ? WHITE : BLACK));
}
Process();
root = -1;
tot = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
Insert(root, 1, size, 1, size, BLACK);
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) for(i = 0; i < n; i++) {
int l = Binary(pt[i].l);
int r = Binary(pt[i].r);
Insert(root, l, r, 1, size, pt[i].color);
}
if(T[root].MaxLR[0] == -1)
printf("Oh, my god\n");
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) else {
printf("%d %d\n", bin[ T[root].MaxLR[0] ], bin[ T[root].MaxLR[1] ]);
}
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//*
3
1 100000 w
2 6 b
100 1000 b
*/
|