线段树 poj 2777 Count Color
poj 2777
https://vjudge.net/problem/POJ-2777
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
代码如下
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
/*
L T O L线段 T 颜色种类 O操作个数
L (1 <= L <= 100000), T (1 <= T <= 30) , O (1 <= O <= 100000)
2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2
Sample Output
2
1
*/
const int N = 100010;
struct SEGMENT_TREE {
int l; int r;
int color;
}tr[N*4];
int n, m, o;
int cArr[50];
void build(int p, int l, int r) {
tr[p].l = l; tr[p].r = r;
if (l == r) {
tr[p].color = 0;
tr[p].l = l; tr[p].r = r;
return;
}
int mid = (l + r) >> 1;
if (l <= mid) build(p*2,l,mid);
if (r > mid) build(p * 2 + 1, mid + 1, r);
return;
}
void query(int p, int l, int r) {
if (tr[p].color != 0) {
cArr[tr[p].color] = 1;
return;
}
int mid = (tr[p].l + tr[p].r) >> 1;
if (l <= mid) query(p * 2, l, r);
if (r > mid) query(p*2+1,l,r);
}
void pushDown(int p) {
if (tr[p].color != 0) {
tr[p * 2].color = tr[p].color; tr[p * 2+1].color = tr[p].color;
tr[p].color = 0;
}
}
void freshUp(int p) {
if (tr[p * 2].color == tr[p * 2 + 1].color) {
tr[p].color = tr[p * 2].color;
}
}
void update(int p, int l, int r, int c) {
if (l <= tr[p].l && r >= tr[p].r) {
//刷颜色
tr[p].color = c;
return;
}
pushDown(p);
int mid = (tr[p].l + tr[p].r) >> 1;
if (l <= mid) update(p * 2, l, r, c);
if (r > mid) update(p * 2 + 1, l, r, c);
freshUp(p);
}
int main()
{
cin >> n >> m >> o;
build(1,1,n);
//初始所有线段均涂抹颜色1
tr[1].color = 1;
while (o--) {
char op[2]; int l, r, c;
scanf("%s %d %d", op, &l, &r);
if (op[0] == 'C') {
scanf("%d", &c);
update(1,l,r,c);
}
else if (op[0] == 'P') {
memset(cArr, 0, sizeof cArr);
query(1, l, r);
int count = 0;
for (int i = 1; i <= m; i++) {
if (cArr[i] != 0) count++;
}
printf("%d\n", count);
}
}
return 0;
}
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
2020-10-08 Acwing 196 质数距离 数论