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. robot framework测试驱动无法定位页面元素

    robot framework错误提示: [ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: NoSuchW ...

  2. Eloquent Attach/Detach/Sync Fires Any Event

    eloquent-attach-detach-sync-fires-any-event I have a laravel project, and I need to make some calcul ...

  3. SSRF-php初探

    0x00 前言 1)    SSRF的概念很好理解,请自行百度. 2)    JAVA/PHP/PYTHON都存在SSRF漏洞(至于其他语言的情况,了解粗浅尚不得知). 3)    SSRF的利用方式 ...

  4. mysql链接

    一 Mysql命令行连接 一般对于刚刚安装好的mysql,如果勾选启用mysql lineclient的话.可以直接通过找到开始---程序--- mysql command line client如下 ...

  5. SparkStreaming 监控文件目录

    SparkStream 监控文件目录时,只能监控文件内是否添加新的文件,如果文件名没有改变只是文件内容改变,那么不会检测出有文件进行了添加. )) )).reduceByKey(_ + _) word ...

  6. springboot配置il8n

    springMvc下,配置il8n: 1.配置ResourceBundleMessageSource管理国际化资源文件 2.在页面使用fmt标签取出国际化内容 springBoot下,自动配置了il8 ...

  7. canvas 实现时钟效果

    var clock = document.getElementById('clock'); var cxt = clock.getContext('2d'); function drawClock() ...

  8. Rest架构风格的实践(使用通用Mapper技术)

    1.根据用户 id 查询用户数据 1.1 controll控制器 @RequestMapping("restful/user") @Controller public class ...

  9. 2019.02.06 bzoj4503: 两个串(fft)

    传送门 题意简述:给两个字符串s,ts,ts,t,ttt中可能有通配符,问ttt在sss出现的次数和所有位置. 思路:一道很熟悉的题,跟bzoj4259bzoj4259bzoj4259差不多的. 然后 ...

  10. 2018.11.08 NOIP模拟 景点(倍增+矩阵快速幂优化dp)

    传送门 首先按照题意构造出转移矩阵. 然后可以矩阵快速幂求出答案. 但是直接做是O(n3qlogm)O(n^3qlogm)O(n3qlogm)的会TTT掉. 观察要求的东西发现我们只关系一行的答案. ...