【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. PostgreSQL 备忘

    truncate table page_frame_mst; select setval('page_frame_mst_id_seq', 1, false): select setval('imag ...

  2. java读取03、07版EXCEL

    03版excel,需要用到jxl.jar这个jar包 package test.poi; import java.io.File; import java.io.IOException; import ...

  3. C_动态库|静态库

    动态库 动态链接库简称DLL,同时以.dll 为后缀,主要用于提供代码和数据 dll 并不是所有数据都能被访问到,必须要进行导出 动态链接库在内存中始终只保存了一份数据,起到了节约内存的作用 生成动态 ...

  4. centos右上角wired图标消失有效解决方案

    最近在学习Linux配置nginx时,左上角的wired图标突然没了,很神奇.然后在网上按着很多博客说的去改,都没用,最后终于根据下面参考博客内的方案解决了问题,嘿嘿. mv /var/lib/Net ...

  5. 19MVC设计模式

    MVC设计模式 MVC英文即Model-View-Controller, 即把一个应用的输入.处理.输出流程按照Model.View.Controller的方式进行分离,这样一个应用被分成三个层——模 ...

  6. 手机端h5复制功能

    html: <a href="javascript:;" id="copyBtn" class="f-r tac" data-clip ...

  7. 「 HDU P2089 」 不要62

    和 HDOJ 3555 一样啊,只不过需要多判断个 ‘4’ 我有写 3555 直接去看那篇吧 这里只放代码 #include <iostream> #include <cstring ...

  8. 字符串匹配「 KMP 算法 」

    引言 众所周知,字符串无论是在 OI 中还是别的计算机领域都占有比较大的比重,今天说的就是一个关于匹配字符串的算法——「 KMP 算法 」. 0x00 KMP 算法用于解决这样的一类问题:给定一个文本 ...

  9. 1054.求平均数-PAT乙级真题

    从其他博客优秀代码中学到了些技巧,记录一下. 思路:使用sscanf和sprintf函数. sscanf() – 从一个字符串中读进与指定格式相符的数据 sprintf() – 字符串格式化命令,主要 ...

  10. ubuntu解压zip文件出现乱码情况解决方法

    使用 unzip datastructure.zip 出现下面的情况: extracting: └╧╗╞/╗·╞ў╤з╧░╝п╜ї/╩¤╛▌╜с╣╣╙ы╦у╖и/╩¤╛▌╜с╣╣╙ы╦у╖иги2гй ...