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. TP 真阳性 TN FP FN

    TP.True Positive   真阳性:预测为正,实际也为正 FP.False Positive  假阳性:预测为正,实际为负 FN.False Negative 假阴性:预测与负.实际为正 T ...

  2. 11. pt-heartbeat

    pt-heartbeat [OPTIONS] [DSN] --update|--monitor|--check|--stop ------------------------------------- ...

  3. Vsphere初试——使用Vsphere client

    好不容易安装好ESXi之后,就要安装一个Vsphere Client,为什么要安装这个东东.使用过vmware workstation的人都知道,安装完就可以添加虚拟机,但是ESXi要通过Vspher ...

  4. idea maven 创建webapp项目没有src目录

    archetypeCatalog=internal

  5. 学习selenium的过程

  6. Java学习笔记:数据校验

    在后台开发过程中,需要对参数进行校验. validation bean 是基于JSR-303标准开发出来的,使用注解的方式实现,是一套规范,可以实现参数的校验. Hibernate Validator ...

  7. JWT设计实现

    一.JWT基于token的认证流程: 二.JWT SDK的选取: https://jwt.io/ 三.编写JWT Helper: 1.获取token 设置密钥,规定算法,设置过期时间,设置发行方,生命 ...

  8. WordCount改进 小组项目

    GitHub地址:https://github.com/DM-Star/WordCount-opt 作业需求:http://www.cnblogs.com/ningjing-zhiyuan/p/865 ...

  9. 687. Longest Univalue Path

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  10. xml 转 数组

    function xml_to_array($xml){ if(!$xml){ return false; } //将XML转为array //禁止引用外部xml实体 libxml_disable_e ...