【POJ 2777】 Count Color(线段树区间更新与查询)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40949   Accepted: 12366

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:



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

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

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(线段树区间更新与查询)的更多相关文章

  1. poj 2777 Count Color(线段树)

    题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  2. poj 2777 Count Color(线段树区区+染色问题)

    题目链接:  poj 2777 Count Color 题目大意:  给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C  a  b  c 把区间[a,b]涂为c色,P  a  b 查 ...

  3. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  4. POJ 2777 Count Color(线段树之成段更新)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...

  5. poj 2777 Count Color - 线段树 - 位运算优化

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42472   Accepted: 12850 Description Cho ...

  6. poj 2777 Count Color(线段树、状态压缩、位运算)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38921   Accepted: 11696 Des ...

  7. POJ2777 Count Color 线段树区间更新

    题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...

  8. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  9. ZOJ 1610 Count the Color(线段树区间更新)

    描述Painting some colored segments on a line, some previously painted segments may be covered by some ...

随机推荐

  1. mongodb Shell 启动

    开始运行mongodb 准备 上篇说过,通过brew安装的程序目录在 /usr/local/Cellar下面 下面,我们先看一下 mongodb的可执行程序命令 cd /usr/local/Cella ...

  2. Vue实战之插件 sweetalert 的使用

    安装npm install sweetalert2@7.15.1 --save 封装 sweetalertimport swal from 'sweetalert2' export default { ...

  3. Redis系列(九)--几道面试题

    这里只是一点面试题,想了解更多,可以查看本人的Redis系列:https://www.cnblogs.com/huigelaile/category/1461895.html 1.Redis和Memc ...

  4. 怎么让Eclipse对html和js代码自动提示

    使用eclipse自带的插件,无需另外安装插件,具体步骤如下1.打开eclipse→Windows→Preferences→Java→Editor→Content Assist修改Auto Activ ...

  5. mysql 转载

    一.MySQL概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL ...

  6. kubeadmin 安装k8s集群

    系统设置 CentOS Linux release 7.6.1810 (Core) 修改主机名 vim /etc/hostname k8s-master hostname -F /etc/hostna ...

  7. 基于APE物理引擎的管线容积率计算方法

    容积率一般应用在房地产开发中,是指用地范围内地上总建筑面积与项目总用地面积的比值,这个参数是衡量建设用地使用强度的一项非常重要的指标.在其他行业,容积率的计算也非常重要,如产品利用率.管道使用率等等. ...

  8. 右键快捷打开Git Bash here失败

    右键快捷打开Git Bash here失败,提示: Error: Could not fork child process: Resource temporarily unavailable (-1) ...

  9. 一起看看 scrollHeight,clientHeight,offsetHeight,scrollTop是个啥

    scrollHeight最终数值的组成: var scrollHeight = currentElementContent.height +currentElement.paddingTop+curr ...

  10. DH密钥交换算法

    DH密钥交换算法:DH的全称为Diffie-Hellman ,该算法可以在需要安全传输的前提下,确定双方的对称密钥,该算法的核心在于双方的私钥没有进入网络传输流程,根据对方的公钥和己方的私钥,可以计算 ...