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. BZOJ1047或洛谷2216 [HAOI2007]理想的正方形

    BZOJ原题链接 洛谷原题链接 显然可以用数据结构或\(ST\)表或单调队列来维护最值. 这里采用单调队列来维护. 先用单调队列维护每一行的最大值和最小值,区间长为正方形长度. 再用单调队列维护之前维 ...

  2. Codeforces 757B. Bash's Big Day GCD

    B. Bash's Big Day time limit per test:2 seconds memory limit per test:512 megabytes input:standard i ...

  3. Python之路番外(第二篇):PYTHON基本数据类型和小知识点

    一.基础小知识点 1.如果一行代码过长,可以用续行符 \换行书写 例子 if (signal == "red") and \ (car == "moving") ...

  4. jmeter脚本录制的两种方式

    完成一次完整的性能测试: 1.创建用户: 2.选择协议(HTTP) 3.使用工具去模拟协议操作(1.手工编写(抓包工具):2.工具自带录制) 4.运行工具进行压力测试

  5. 编译sgbm_ros中遇到的问题

    出现的问题 这个会报错 1.解决方法是在文件sudo gedit /usr/local/cuda/include/crt/common_functions.h中注释掉如下 #define __CUDA ...

  6. mybatis3.2初学感悟

    新手,学了mybatis框架一周,写点感悟下来. mybatis,是操作数据库,持久层的一个框架,它是对JDBC的封装.看到了这个框架,我突然感受到封装与抽象的力量.也明白了些为什么要分层的原因. 记 ...

  7. Python module : simuPOP

    conda config --add channels conda-forge conda install simupop simuPOP is a general-purpose individua ...

  8. 【Linux】zlib安装

    zlib简介 zlib是提供数据压缩用的函式库,由Jean-loup Gailly与Mark Adler所开发,初版0.9版在1995年5月1日发表.zlib使用DEFLATE算法,最初是为libpn ...

  9. unity技巧

    在之前的程序编写过程中,虽然对相关的方法进行了实例化,但是在运行的时候总是会出现“未将对象引用设置到对象的实例”,出现该种问题的原因是由于在实例化后,没有对实例化进行引用赋值,所以导致相关变量无法在其 ...

  10. delphi 10 seattle 安卓服务开发(一)

    从delphi 开始支持安卓的开发开始, 安卓service 开发一直都是delphier 绕不过去的坎, 以前也有开发service  的方法,但是都是手工处理启动文件,而且要修改很多东西,基本上成 ...