POJ-2777 Count Color(线段树,区间染色问题)
Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 40510 Accepted: 12215
Description
Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.
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:
- “C A B C” Color the board from segment A to segment B with color C.
- “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
First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains “C A B C” or “P A B” (here A, B, C are integers, and A may be larger than B) as an operation defined previously.
Output
Ouput results of the output operation in order, each line contains a number.
Sample Input
2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2
Sample Output
2
1
线段树区间染色问题,用一个tag数组标记一下就好了
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
#define MAX 100000
int cover[MAX*4+5];
int n;
int t;
int m;
char a;
int x,y,z;
int tag[31];
void PushDown(int node)
{
if(cover[node]!=-1)
{
cover[node<<1|1]=cover[node];
cover[node<<1]=cover[node];
cover[node]=-1;
}
}
void Update(int node,int begin,int end,int left,int right,int num)
{
if(left<=begin&&end<=right)
{
cover[node]=num;
return;
}
PushDown(node);
int m=(begin+end)>>1;
if(left<=m)
Update(node<<1,begin,m,left,right,num);
if(right>m)
Update(node<<1|1,m+1,end,left,right,num);
}
void Query(int node,int begin,int end,int left,int right,int &ans)
{
if(cover[node]!=-1)
{
if(!tag[cover[node]])
{
ans++;
tag[cover[node]]=1;
}
return;
}
PushDown(node);
if(begin==end)
return;
int m=(begin+end)>>1;
if(left<=m)
Query(node<<1,begin,m,left,right,ans);
if(right>m)
Query(node<<1|1,m+1,end,left,right,ans);
}
int main()
{
while(scanf("%d%d%d",&n,&t,&m)!=EOF)
{
memset(cover,0,sizeof(cover));
for(int i=1;i<=m;i++)
{
getchar();
scanf("%c",&a);
if(a=='C')
{
scanf("%d%d%d",&x,&y,&z);
Update(1,1,n,x,y,z-1);
}
else
{
memset(tag,0,sizeof(tag));
scanf("%d%d",&x,&y);
int ans=0;
Query(1,1,n,x,y,ans);
printf("%d\n",ans);
}
}
}
return 0;
}
POJ-2777 Count Color(线段树,区间染色问题)的更多相关文章
- 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 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- 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: 53312 Accepted: 16050 Des ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- POJ P2777 Count Color——线段树状态压缩
Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...
- POJ 2777 Count Color(段树)
职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...
- POJ2777 Count Color 线段树区间更新
题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...
随机推荐
- linux添加PYTHONPATH环境变量
1.添加环境变量到pythonpath export PYTHONPATH=$PYTHONPATH:/home/myproject 查看pythonpathecho $PYTHONPATH 可以进入p ...
- ios开发之--sizeToFit的用法
sizeToFit :即当前视图便捷和便捷大小变化(自动根据文本大小改变自身的宽度) 代码如下: - (void)sizeToFitDemo { UILabel * label = [[UILabel ...
- python中交换两个值的方法
a = 4b = 5 #第1种c = 0c = aa = bb = c #第2种a = a+bb = a-ba = a-b #第3种a,b = b,a 第三种办法本质上是元组之间的赋值 print(& ...
- Python学习笔记(15)- os\os.path 操作文件
程序1 编写一个程序,统计当前目录下每个文件类型的文件数,程序实现如图: import os def countfile(path): dict1 = {} # 定义一个字典 all_files = ...
- PostgreSQL逻辑复制之slony篇
Slony是PostgreSQL领域中最广泛的复制解决方案之一.它不仅是最古老的复制实现之一,它也是一个拥有最广泛的外部工具支持的工具,比如pgAdmin3.多年来,Slony是在PostgreSQL ...
- 【代码审计】CLTPHP_v5.5.3前台XML外部实体注入漏洞分析
0x01 环境准备 CLTPHP官网:http://www.cltphp.com 网站源码版本:CLTPHP内容管理系统5.5.3版本 程序源码下载:https://gitee.com/chichu/ ...
- iOS开发-为UITableViewCell添加横线
在开发过程中经常会遇到设计稿中Cell分割线样式和系统自带的样式差别很大,如何实现这里做下总结,主要包括如下两步: 1. 取消TableView默认的分割线样式 _tableView.separato ...
- Cesium添加水面
var viewer = new Cesium.Viewer('cesiumContainer');var waterPrimitive = new Cesium.Primitive({ //show ...
- CopyTransform
// TransformCopier.cs v 1.1 // homepage: http://wiki.unity3d.com/index.php/CopyTransform using Unity ...
- Java API方式调用Kafka各种协议
众所周知,Kafka自己实现了一套二进制协议(binary protocol)用于各种功能的实现,比如发送消息,获取消息,提交位移以及创建topic等.具体协议规范参见:Kafka协议 这套协议的具 ...