Count Color

Time Limit: 1000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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
#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
bool many[]; struct node
{
int l;
int r;
int col;
} tr[*]; void build(int root,int l,int r)
{
tr[root].l=l;
tr[root].r=r;
int mid=(l+r)/;
if(l!=r)
{
build(root*,l,mid);
build(root*+,mid+,r);
}
} void insert(int root,int l,int r,int c)
{
int mid=(tr[root].l+tr[root].r)/;
if(l==tr[root].l && r==tr[root].r)
{
tr[root].col=c;
return ;
}
if(tr[root].col) ///延迟覆盖
{
tr[*root].col=tr[root].col;
tr[*root+].col=tr[root].col;
tr[root].col=;
} if(r<=mid)
{
insert(*root,l,r,c);
return ;
}
if(l>mid)
{
insert(*root+,l,r,c);
return ;
}
insert(*root,l,mid,c);
insert(*root+,mid+,r,c);
} int calu(int root,int l,int r)
{
int sum = ;
if(tr[root].col){
if(many[tr[root].col]==false){
sum++;
many[tr[root].col]=true;
}
return sum;
}
int mid=(tr[root].l+tr[root].r)/;
if(r<=mid)
sum += calu(root*,l,r);
else if(l>mid)
sum += calu(root*+,l,r);
else{
sum += calu(root*,l,mid);
sum += calu(root*+,mid+,r);
}
return sum;
}
int main()
{
int x,y,z;
int n,t,o;
char op;
while(scanf("%d%d%d",&n,&t,&o)!=EOF)
{
tr[].col=;
build(,,n);
while(o--)
{
getchar();
scanf("%c",&op);
if(op=='C')
{
scanf("%d%d%d",&x,&y,&z);
if(x>y) swap(x,y);
insert(,x,y,z);
}
else
{
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
memset(many,false,sizeof(many));
printf("%d\n",calu(,x,y));
}
}
}
return ;
}

poj 2777 线段树的区间更新的更多相关文章

  1. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdu 1698:Just a Hook(线段树,区间更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. UVA 12436-Rip Van Winkle's Code(线段树的区间更新)

    题意: long long data[250001]; void A( int st, int nd ) { for( int i = st; i \le nd; i++ ) data[i] = da ...

  4. hdu1698线段树的区间更新区间查询

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. HDU 1556 Color the ball(线段树:区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...

  6. zoj3686(线段树的区间更新)

    对线段树的区间更新有了初步的了解... A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a ...

  7. Color the ball (线段树的区间更新问题)

    N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但 ...

  8. ZOJ 2301 Color the Ball 线段树(区间更新+离散化)

    Color the Ball Time Limit: 2 Seconds      Memory Limit: 65536 KB There are infinite balls in a line ...

  9. POJ 2777——线段树Lazy的重要性

    POJ 2777 Count Color --线段树Lazy的重要性 原题 链接:http://poj.org/problem?id=2777 Count Color Time Limit: 1000 ...

随机推荐

  1. nodejs 如何操作字节在内存中的位置问题 BE LE

    上代码 function testNumber() { var arr = new Int32Array(1); arr[0] = 1234; var buf1 = Buffer.from(arr); ...

  2. BUG(0):用某位表示特定属性

    用某个bit表示特定属性通常有两种方式: 1.指定某个特定的value #define _PAGE_VALID 0x0001 0bit 为 1 时表示此时的page entry是有效的 用法如下,此时 ...

  3. java正则表达式(字符串)

    参考:http://blog.csdn.net/kdnuggets/article/details/2526588 补充: ?号的使用: ^/branch/([a-z]+[/]?)$  可以匹配 /b ...

  4. oracle的常用99条语句

    1. select * from emp; 2. select empno, ename, job from emp; 3. select empno 编号, ename 姓名, job 工作 fro ...

  5. Flex的Number和Text

    今天要说的问题不是Number和String转换的问题.而是使用时容易出的一些错误: public static function ToFixed(value:Number, digits:uint ...

  6. [C#]RichTextBox实现拖放

    amespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeC ...

  7. Android——编译release版签名系统

    http://blog.csdn.net/jscese/article/details/24243171 在我的第一篇博客里面http://blog.csdn.net/jscese/article/d ...

  8. AOP (切点表达式讲解)

    Spring EL表达式:: 1.execution 表达式 语法格式: execution(返回类型.包名.类名.方法名(参数表)) exection(*.com.xxx.AService.*(.. ...

  9. 关于Code Review

    为了保证代码质量,我们团队内部一直在推行Code Review.现在Code Review帮我们发现了很多代码的问题,提升了代码的可读性和质量,同时我们在Code Review上也花费了很多时间,有些 ...

  10. Graphviz 环境变量设置

    今天晚上解决了一个错误,如下: