思路:线段树。这道题有很多细节
eg: 0 2 1
3 4 1
output 应该是 1 2 因为中间隔了一段空的
具体见代码
#include <stdio.h>
#include <string.h>
#define M 8005
#define L(x) (x<<1)
#define R(x) ((x<<1)+1)
struct data
{
l,r,col;
//col 等于 -1 表示什么色都没有
}node[M*3];
int
color[M];
//记录每块板上的是什么颜色
void BuildTree(int left,int right,int u)
{
left;
right;
= -1;
right)
return ;
(left + right)>>1;
BuildTree(left,mid,L(u));
BuildTree(mid+1,right,R(u));
}
void getdown(int u)
{
node[L(u)].col = node[u].col;
node[R(u)].col = node[u].col;
= -1;
}
void updata (int left,int right,int val,int u)
{
(node[u].col == val)
return ;
<=
node[u].l&&node[u].r
<= right)
node[u].col = val;
return ;
(node[u].col !=
-1)
//延迟覆盖 不然可能会TLE(最近做一直都是延迟覆盖)
getdown (u);
(node[u].l+ node[u].r)>>1;
<= mid)
updata (left,right,val,L(u));
(left >= mid+1)
updata (left,right,val,R(u));
updata (left,right,val,L(u));
updata (left,right,val,R(u));
((node[L(u)].col ==
node[R(u)].col)&&node[L(u)].col !=
-1)
node[u].col = node[L(u)].col;
}
void query (int u)
{
(node[u].col != -1)
for (int i = node[u].l;i <= node[u].r;i ++)
color[i] = node[u].col;
return ;
(node[u].l == node[u].r)
//这是不加这个竟然会运行不起来
return ;
(L(u));
(R(u));
}
void ans ()
{
res[M];
(res,0,sizeof(res));
-1,i;
< M;i ++)
if (pre != color[i]) //颜色块不相连就++
{