【POJ 2777】 Count Color(线段树区间更新与查询)
【POJ 2777】 Count Color(线段树区间更新与查询)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 40949 | Accepted: 12366 |
Description
There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment
with only one color. We can do following two operations on the board:
1. "C A B C" Color the board from segment A to segment B with color C.
2. "P A B" Output the number of different colors painted between segment A and segment B (including).
In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the
beginning, the board was painted in color 1. Now the rest of problem is left to your.
Input
be larger than B) as an operation defined previously.
Output
Sample Input
2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2
Sample Output
2
1
Source
感觉有些神经衰弱了。。。写发水水的线段树放松放松。。。
一直在搞图论 是该换换弦 磨得快锈了 太折磨人了……(弱也不知道说了些啥
一块木板 长L(1~L) 有T种颜色的油漆标号1~T 默认木板初始是1号颜色
进行O次操作 操作有两种
C a b c 表示木板a~b段涂c种油漆(若之前涂过其它颜色 则覆盖掉)
P a b 表示询问木板a~b段如今涂了几种油漆
两个数组 一个存树 一个存涂了哪几种油漆
存树的表示a~b涂的某种颜色 然后搞搞就出来了……好乏
代码例如以下:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout) using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8; int tr[400400];
bool col[33];
int L,T,O; void Color(int root,int l,int r,int a,int b,int c)
{
if(a == l && b == r)
{
tr[root] = c;
return;
}
int mid = (l+r)>>1;
if(tr[root])
tr[root<<1] = tr[root<<1|1] = tr[root];
tr[root] = 0; if(mid >= b) Color(root<<1,l,mid,a,b,c);
else if(mid+1 <= a) Color(root<<1|1,mid+1,r,a,b,c);
else
{
Color(root<<1,l,mid,a,mid,c);
Color(root<<1|1,mid+1,r,mid+1,b,c);
}
} void Search(int root,int l,int r,int a,int b)
{
//printf("root:%d l:%d r:%d co:%d\n",root,l,r,tr[root]);
if(tr[root])
{
col[tr[root]] = 1;
return;
}
int mid = (l+r)>>1;
if(mid >= b) Search(root<<1,l,mid,a,b);
else if(mid+1 <= a) Search(root<<1|1,mid+1,r,a,b);
else
{
Search(root<<1,l,mid,a,mid);
Search(root<<1|1,mid+1,r,mid+1,b);
}
} int main()
{
//fread();
//fwrite();
char opt[3];
int a,b,c; while(~scanf("%d%d%d",&L,&T,&O))
{
memset(tr,0,sizeof(tr));
tr[1] = 1;
while(O--)
{
scanf("%s",opt);
scanf("%d%d",&a,&b);
if(a > b) swap(a,b); if(opt[0] == 'C')
{
scanf("%d",&c);
Color(1,1,L,a,b,c);
}
else
{
memset(col,0,sizeof(col));
Search(1,1,L,a,b); int ans = 0;
for(int i = 1; i <= T; ++i)
ans += col[i];
printf("%d\n",ans);
}
}
} return 0;
}
【POJ 2777】 Count Color(线段树区间更新与查询)的更多相关文章
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj 2777 Count Color(线段树区区+染色问题)
题目链接: poj 2777 Count Color 题目大意: 给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C a b c 把区间[a,b]涂为c色,P a b 查 ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- POJ2777 Count Color 线段树区间更新
题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- ZOJ 1610 Count the Color(线段树区间更新)
描述Painting some colored segments on a line, some previously painted segments may be covered by some ...
随机推荐
- Java 基础入门随笔(10) JavaSE版——单例设计模式
设计模式:对问题行之有效的解决方式.其实它是一种思想. 1.单例设计模式. 解决的问题:就是可以保证一个类在内存中的对象唯一性.(单个实例) 使用单例设计模式需求:必须对于多个程序使用同一个配置信息对 ...
- 判断excel是否包含隐藏sheet
Workbook workbook =new XSSFWorkbook("D:\\文档1.xlsx"); System.out.println(workbook.isSheetHi ...
- HDU_3496_(二维费用背包)
Watch The Movie Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...
- RocketMQ学习笔记(16)----RocketMQ搭建双主双从(异步复制)集群
1. 修改RocketMQ默认启动端口 由于只有两台机器,部署双主双从需要四个节点,所以只能修改rocketmq的默认启动端口,从官网下载rocketmq的source文件,解压后使用idea打开,全 ...
- vue-element-admin使用常见问题
一.vue-element-admin添加快捷导航 这个组件是基于vue-i18n因此,首先在项目中安装i18n npm install --save vue-i18n 然后main.js中引入 im ...
- MFC窗体大小变化
对话框的大小变化后,假若对话框上的控件大小不变化,看起来会比较难看.下面就介绍怎么让对话框上的控件随着对话框的大小的变化自动调整. 首先明确的是Windows有一个WM_SIZE消息响应函数,这个函数 ...
- Deployd的使用
deployd一个生成后台数据的软件,可以创建json格式的数据,也可以对数据进行增删改查等操作,甚至可以验证登录,简直就是自学好帮手呀,不用后台搞定后台,就用deployd 下载:链接: https ...
- ThinkPHP---案例2--部门管理功能
[一]部门列表展示 分析: ①控制器DeptController.class.php ②方法showList(不要使用list方法,因为list是关键词) ③模板文件:showList.html 下面 ...
- 00JAVA EE
JAVA EE 三层架构 我们的开发架构一般都是基于两种形式,一种是C/S架构,也就是客户端/服务器,另一种是B/S架构,也就是浏览器服务器.在JavaEE开发中,几乎全都是基于B/S架构的开发.那么 ...
- Jquery 上一步、下一步及提交
111 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...