Count Color
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 34605 | Accepted: 10445 |
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.
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
题意:
给出 L, T, O,代表有一条长度为 L(1 ~ 100000) 的板,有 T(1 ~ 30) 种颜色,有 O (1 ~ 100000)个操作。后给出 O 个操作,操作有两类,第一类 C 代表有将 L 到 R 这段区间涂为 Ti 号颜色,第二类 P 代表询问 L 到 R 这段颜色有多少种,每次 P 操作的时候就输出这个数。
思路:
线段树 + 延迟标记。因为一共有 30 号颜色,所以用一个二进制数来表示每一段涂色的情况,树维护二进制涂色的情况,同时标记维护覆盖的情况,push_down 操作为直接覆盖操作,所以直接等于 mark 标记颜色就好,push_up 操作为左右子区间的或操作。统计区间内的颜色数的时候也需要用 或 来维护结果,最后对结果求出二进制有多少个 1 的情况即为答案。注意这题给出的左右区间数不严格遵守左边大于右边,所以还要判断要不要交换。
AC:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAX = 100005; int color[MAX * 3], mark[MAX * 3]; int sum; int count_one() { int ans = 0; while (sum) { if (sum % 2) ++ans; sum >>= 1; } return ans; } void push_up(int node) { color[node] = color[node << 1] | color[node << 1 | 1]; } void push_down(int node, int l, int r) { if (mark[node]) { mark[node << 1] = mark[node]; mark[node << 1 | 1] = mark[node]; color[node << 1] = mark[node]; color[node << 1 | 1] = mark[node]; mark[node] = 0; } } void build (int node, int l, int r) { if (l == r) { color[node] = 1; mark[node] = 0; } else { int mid = (l + r) >> 1; build(node << 1, l, mid); build(node << 1 | 1, mid + 1, r); push_up(node); } } void update (int node, int l, int r, int cl, int cr, int c) { if (cl > r || cr < l) return; if (cl <= l && cr >= r) { mark[node] = c; color[node] = c; return; } push_down(node, l, r); int mid = (r + l) >> 1; update(node << 1, l, mid, cl, cr, c); update(node << 1 | 1, mid + 1, r, cl, cr, c); push_up(node); } void query (int node, int l, int r, int cl, int cr) { if (cl > r || cr < l) return; if (cl <= l && cr >= r) { sum |= color[node]; return; } push_down(node, l, r); int mid = (r + l) >> 1; query(node << 1, l, mid, cl, cr); query(node << 1 | 1, mid + 1, r, cl, cr); push_up(node); } int main() { int n, c, o; while(~scanf("%d%d%d", &n, &c, &o)) { build(1, 1, n); while (o--) { char c; scanf(" %c", &c); if (c == 'C') { int cl, cr, col; scanf("%d%d%d", &cl, &cr, &col); if (cl > cr) swap(cl, cr); update(1, 1, n, cl, cr, 1 << (col - 1)); } else { int cl, cr; scanf("%d%d", &cl, &cr); if (cl > cr) swap(cl, cr); sum = 0; query(1, 1, n, cl, cr); printf("%d\n", count_one()); } } } return 0; }
相关推荐
SourceCount代码统计工具,可以统计出代码行数。方便实用,无需注册。 SourceCount代码行数统计工具
定义线段树的数据结构 struct Line{ int left, right, count; Line *leftChild, *rightChild; Line(int l, int r): left(l), right(r) {} };
代码行数统计工具,特别好用,可以统计代码的注释行数、空行数、代码有效行数。值得拥有。 代码行数统计工具,特别好用,可以统计代码的注释行数、空行数、代码有效行数。值得拥有。
题目:分类统计字符个数COUNT_CHAR程序接收用户键入的一行字符(字符个数不超过80个,该字符串用回车符结束),
Count Lines of code 统计代码行数
countlines 代码统计工具 统计代码 你想知道你一天写了多少java代码、JSP代码之类的么?下载下来看看吧,才9K,好用。
int count; static double average; static double value[]; static double sum = 0; public static void main(String args[]) { Scanner sc = new Scanner(System.in); int count = sc.nextInt(); double...
sourcecount 代码行统计工具,可以统计C/C++、JAVA、Basic三中言语的代码行数
代码统计工具 sourcecount,很好用的统计代码工具
i<dt.Columns.Count;i++) { if(i == dt.Columns.Count - 1) { colHeaders +=dt.Columns[i].Caption.ToString() +"\n"; } else { colHeaders +=dt.Columns[i].Caption.ToString()+"\t"; } } foreach...
SourceCount能够轻松帮助你统计代码行数,可统计单一文件代码行数,也可以添加目录分别统计总行数。开发者必备工具。1:选择程序语言 目前支持三种类型的工程,在后期版本中将增加对HTML/JSP、HTML/ASP类工程的支持...
代码统计工具,可看新增,修改,删除行数。语言Java,html,jsp,c++等各种语言 操作是将两个不同版本的项目导入到工具中,然后点击count按钮,直接就能看结果
很好用的代码统计工具很好用的代码统计工具很好用的代码统计工具
代码行统计工具CountLines.exe
i < da.Columns.Count; i++)//填充表头 { { excel.Cells[1, i + 1] = da.Columns[i].ColumnName.ToString();//列索引值递增,指定列填充数据 } for (int i = 0; i < da.Rows.Count; i++)//填充数据 { { for...
layui laypage插件如何通过ajax返回动态count值,然后重置laypage count值
用于CAD作图时,汇总数量,统计,制作成表格
string[] row = new string[this.dgvStockList.Columns.Count]; for (int n = 0; n < this.dgvStockList.Columns.Count; n++) { row[n] = this.dgvStockList.Rows[i].Cells[n].Value.ToString(); } writer.Add...
i < dt.Columns.Count; i++) { row.CreateCell(i).SetCellValue(dt.Columns[i].ColumnName); } for (int i = 0; i < dt.Rows.Count; i++) { NPOI.SS.UserModel.Row row2 = sheet.CreateRow(i + 1); for ...
北大POJ3432-Count Squares 解题报告+AC代码