poj 2777(线段树+lazy思想) 小小粉刷匠
http://poj.org/problem?id=2777
题目大意 涂颜色,输入长度,颜色总数,涂颜色次数,初始颜色都为1,然后当输入为C的时候将x到y涂为颜色z,输入为Q的时候输出x到y的颜色总数
很明显的区间线段树,然后加lazy思想记录
lazy操作为了避免查找到每一个子节点区间而费时,将查找到的区间作标记,但查找到这个区间或还要继续像下查找的时候
将此区间的数据传给下面两个区间树
因为这题颜色总类只有30种很少,所以偷了个懒,将判断与记录操作合并到一个结构体上了,然后用类似hash的数组记录颜色总数
code
#include<cstdio>
#include<string.h>
using namespace std;
struct point
{
int l,r;
int color;
};
point tree[*];
int visit[];
void build(int i,int left,int right)
{
tree[i].l=left;tree[i].r=right;
tree[i].color=;
if(left==right)return;
int mid=(left+right)/;
build(i*,left,mid);
build(i*+,mid+,right);
}
void update(int i,int left,int right,int val)
{
if (left<=tree[i].l&&tree[i].r<=right){tree[i].color=val;return ;}
if (tree[i].color!=-) //lazy操作
{
tree[i*].color=tree[i*+].color=tree[i].color;
tree[i].color=-;
}
if (left<=tree[i*].r) update(i*,left,right,val);
if (right>=tree[i*+].l) update(i*+,left,right,val);
}
void find(int i,int left,int right)
{
if (tree[i].color!=-) {visit[tree[i].color]=;return ;}
if (tree[i].l==tree[i].r) return;
if (tree[i].color!=-)
{
tree[i*].color=tree[i*+].color=tree[i].color;
tree[i].color=-;
}
if (left<=tree[i*].r) find(i*,left,right);
if (right>=tree[i*+].l) find(i*+,left,right);
}
int main()
{
int x,y,z,n,m,k;
char op;
while (~scanf("%d %d %d",&x,&y,&z))
{
build(,,x);
while (z--)
{
scanf(" %c",&op);
if (op=='C')
{
scanf("%d %d %d",&n,&m,&k);
update(,n,m,k);
}
else
{
scanf("%d %d",&n,&m);
memset(visit,,sizeof(visit));
find(,n,m);
int sum=;
for (int i=;i<=;i++)
if (visit[i]==)
sum++;
printf("%d\n",sum);
}
}
}
return ;
}
poj 2777(线段树+lazy思想) 小小粉刷匠的更多相关文章
- POJ 2777——线段树Lazy的重要性
POJ 2777 Count Color --线段树Lazy的重要性 原题 链接:http://poj.org/problem?id=2777 Count Color Time Limit: 1000 ...
- poj 3468(线段树+lazy思想)
题目链接:http://poj.org/problem?id=3468 思路:如果直接去做,每次都更新到叶子节点,那必然会TLE,我们可以采用lazy的思想:没必要每次更新都更新到叶子节点,只要有一个 ...
- POJ 2777(线段树)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42507 Accepted: 12856 Des ...
- POJ 2777 线段树基础题
题意: 给你一个长度为N的线段数,一开始每个树的颜色都是1,然后有2个操作. 第一个操作,将区间[a , b ]的颜色换成c. 第二个操作,输出区间[a , b ]不同颜色的总数. 直接线段树搞之.不 ...
- poj 2777线段树应用
敲了n遍....RE愉快的debug了一晚上...发现把#define maxn = 100000 + 10 改成 #define maxn = 100010 就过了....感受一下我呵呵哒的表情.. ...
- Count Color POJ - 2777 线段树
Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...
- POJ 3225 线段树+lazy标记
lazy写崩了--. 查了好久 /* U-> [l,r]–>1 I-> [1,l-1] [r+1,+无穷] –>0 D-> [l,r]–>0 C-> [1,l ...
- poj 2777 线段树的区间更新
Count Color Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java ...
- poj 2777 线段树 区间更新+位运算
题意:有一个长板子,分成多段,有两种操作,第一种是C给从a到b那段染一种颜色c,另一种是P询问a到b有多少种不同的颜色.Sample Input2 2 4 板长 颜色数目 询问数目C 1 1 2P ...
随机推荐
- 在windows上通过ssh远程链接linux服务器[转]
本文分别转自 [http://jingyan.baidu.com/article/6d704a130de40e28db51cab5.html] [http://www.cnblogs.com/mliu ...
- CMake Error at cmake/OpenCVModule.cmake:295 (message): No extra modules found in folder:Please provide path to 'opencv_contrib/modules' folder
其实,我们使用的opencv中要用的contrib/modules 是需要额外下载并在cmakelists.txt中指定的 git clone https://github.com/opencv/ ...
- pandas 中的常用数学计算
1.开方: >>> s = pd.Series([1.2 + 1j]) >>> s.abs()
- 启用mongodb授权认证
linux下 1.以–auth 启动mongod 2.在配置文件mongod.conf 中加入 auth = true
- SimpleAdapter用法
[SimpleAdapter用法] public class TestSimpleAdapter extends Activity { @Override protected void onCreat ...
- javascript学习笔记(五):异常捕获和事件处理
异常捕获 Try{ 发生异常的代码块 }catch(err){ 异常信息处理 } <!DOCTYPE html> <html> <head lang="en&q ...
- WPF HyperLink链接下划线隐藏
两种方法: 1.在Grid标签内添加资源样式. <Grid.Resources> <Style TargetType="Hyperlink"> <Se ...
- HDU-1069.MonkeyandBanana(LIS)
本题大意:给出n个长方体,每种长方体不限量,让你求出如何摆放长方体使得最后得到的总高最大,摆设要求为,底层的长严格大于下层的长,底层的宽严格大于下层的宽. 本题思路:一开始没有啥思路...首先应该想到 ...
- 使用Python计算IP、TCP、UDP校验和
IP数据报的校验: IP数据报只需要对数据头进行校验,步骤如下: 将接收到的数据的checksum字段设置为0 把需要校验的字段的所有位划分为16位(2字节)的字 把所有16位的字相加,如果遇到进位, ...
- swift - 接入听云监测 - 问题
1. 正常下载 探针SDK:https://report.tingyun.com/mobile-web/#/onlyHeader/sdkDownload 2.按步骤接入 ,添加库啊,什么的URLSc ...