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 ...
随机推荐
- C#客户端嵌入Chrome浏览器的实现
https://blog.csdn.net/lanwilliam/article/details/79639823 客户端软件,也就是传统的Winform软件,在很多时候是很好用的.因为在做一些打印. ...
- 线程同步 –AutoResetEvent和ManualResetEvent
上一篇介绍了通过lock关键字和Monitor类型进行线程同步,本篇中就介绍一下通过同步句柄进行线程同步. 在Windows系统中,可以使用内核对象进行线程同步,内核对象由系统创建并维护.内核对象为内 ...
- php查找之二分查找
二分查找,往往是针对有序的数组进行查找,我们假设一个序列是数组有序,然后给定一个数字,查出它应该在这个数组中的排序位置 百度百科中讲到 二分查找也称折半查找(Binary Search),它是一种效率 ...
- Extended VM Disk In VirtualBox or VMware (虚拟机磁盘扩容)
First, Clean VM all snapshot, and poweroff your VM. vmdk: vmware-vdiskmanager -x 16GB myDisk.vmdk vd ...
- linux给当前用户添加环境变量
比如当前用户为oracel,则添加环境变量操作为: vim /home/oracel/.bashrc
- 高并发应对:淘宝CDN缓存服务器部署探秘
转自:http://server.chinabyte.com/6/12663506.shtml “好,时间到,开抢!”坐在电脑前早已等待多时的宋兰(化名)一看时间已到2011年11月11日零时,便迫不 ...
- centos6.4安装GCC
1. Last login: Mon Aug 4 11:46:15 2014 from 10.3.7.128 [jifeng@jifeng04 ~]$ ls hadoop jdk1.7.0_45 ...
- Java bean中布尔类型使用注意
JavaBean是一个标准,遵循标准的Bean是一个带有属性和getters/setters方法的Java类. JavaBean的定义很简单,但是还有有一些地方需要注意,例如Bean中含有boolea ...
- JavaScript的格式--从格式做起,做最严谨的工程师
1.JavaScript的格式: JavaScript区分大小写: JavaScript脚本程序须嵌入在HTML文件中: JavaScript脚本程序中不能包含HTML标记代码:(双引号) 每行写一条 ...
- 获取访客IP、地区位置信息、浏览器、来源页面
<?php //这个类似用来获取访客信息的 //方便统计 class visitorInfo { //获取访客ip public function getIp() { $ip=false; if ...