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. 卸载服务器GitLab

    sudo gitlab-ctl uninstall sudo rpm -e gitlab-ce find / -name gitlab|xargs rm -rf

  2. 关于C的int

    在c运行库头文件<stdint.h>中typedef各种类型的int typedef signed char int8_t; typedef unsigned char uint8_t; ...

  3. UI设计工资有多高?怎么快速拿高薪?

    1.UI设计工资有多高? 有人不服UI设计待遇薪资高,那么下面就来看下一线城市的最新UI设计平均薪资待遇,大家也可以打开各招聘网站查询各行业平均薪资情况,一个行业的薪资高不高行业市场决定. 待遇较高说 ...

  4. read temperature

    button1, button2, richtexbox1, serialport1, using System;using System.Collections.Generic;using Syst ...

  5. c# 通过按钮获取文件夹和打开磁盘文件

    Button控件获取文件夹: FolderBrowserDialog fileDialog = new FolderBrowserDialog(); if (fileDialog.ShowDialog ...

  6. spring学习九 spring aop详解

    本文来自于:https://www.cnblogs.com/jingzhishen/p/4980551.html AOP(Aspect-Oriented Programming,面向方面编程),可以说 ...

  7. delphi 中如何访问另一个类中到私有方法?(转载)

    原始连接 http://rvelthuis.blogspot.tw/2018/01/accessing-private-methods-of-another.html Accessing privat ...

  8. 通用Mapper

    原理是:拦截器 1.假设:使用MyBatis只需要定义Mapper接口,无需编写Mapper.xml文件 如果实现无需编写Mapper.xml文件,我们必须要实现动态拼接SQL 如何实现动态拼接SQL ...

  9. Java数据类型的转换

    Java数据类型,从小到大排序 byte ,short ,int ,long ,float, double,char 1.小数据类型转换大的数据类型,自动转换 int a = 3; double b ...

  10. Educational Codeforces Round 61 D 二分 + 线段树

    https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒 ...