发现自己越来越傻逼了。一道傻逼题搞了一晚上一直超时,凭啥子就我不能过???

然后发现cin没关stdio同步。。。


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


题目大意

给你一根小木棍,最开始颜色为 1, 然后有两种操作: 将某一段区间全染成某种颜色,或者询问某一个区间共有几种颜色。

题解

因为 颜色最多只有30种, 所以可以用一个int来存某个颜色是否出现,向上更新是直接 | (或) 一下就好了。最后枚举一下答案中共出现了几种颜色。

Ps:cin 卡一晚上我不服啊,辣鸡题,毁我青春233

代码

#include <iostream>
using namespace std;
#define ls u<<1,l,mid
#define rs u<<1|1,mid+1,r
#define pushup(u) {nod[u] = nod[u<<1] | nod[u<<1|1];}
#define pushdown(u) {add[u<<1] = add[u<<1|1] = add[u];nod[u<<1] = nod[u<<1|1] = (1<<(add[u]-1));add[u] = 0;} const int maxn = 1e5 + 5;
int add[maxn << 2]; int nod[maxn << 2]; void build(int u,int l,int r) {
nod[u] = 1;
add[u] = 0;
if(l == r) return;
int mid = (l + r) >> 1;
build(ls);
build(rs);
} void update(int u,int l,int r,int x,int y,int ad) {
if(l == x && r == y) {
nod[u] = (1<<(ad-1));add[u] = ad;return;
}
if(add[u]) pushdown(u);
int mid = (l + r) >> 1;
if(y <= mid) update(ls,x,y,ad);
else if(x > mid) update(rs,x,y,ad);
else update(ls,x,mid,ad), update(rs,mid+1,y,ad);
pushup(u);
} int query(int u,int l,int r,int x,int y) {
if(l == x && r == y)return nod[u];
if(add[u])pushdown(u);
int mid = (l + r) >> 1;
if(y <= mid) return query(ls,x,y);
if(x > mid) return query(rs,x,y);
return query(ls,x,mid) | query(rs,mid+1,y);
} int main() {
ios::sync_with_stdio(false);cin.tie(0);
int n,t,o;
char op[2];
int a,b,c;
while(cin >> n >> t >> o) {
build(1,1,n);
while(o--){
cin >> op >> a >> b;
if(a > b) swap(a,b);
if(op[0] == 'C') {
cin >> c;
update(1,1,n,a,b,c);
}
else {
int ans = 0;
int x = query(1,1,n,a,b);
for(int i = 0;i < t;i++)if(x & (1<<i))ans++;
cout << ans << endl;
}
}
}
return 0;
}

[poj2777] Count Color (线段树 + 位运算) (水题)的更多相关文章

  1. Count Color(线段树+位运算 POJ2777)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...

  2. poj 2777 Count Color - 线段树 - 位运算优化

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42472   Accepted: 12850 Description Cho ...

  3. POJ2777 Count Color 线段树区间更新

    题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...

  4. Count Colour_poj2777(线段树+位)

    POJ 2777 Count Color (线段树)   Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  5. poj 2777 Count Color(线段树、状态压缩、位运算)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38921   Accepted: 11696 Des ...

  6. POJ 2777 Count Color(线段树+位运算)

    题目链接:http://poj.org/problem?id=2777 Description Chosen Problem Solving and Program design as an opti ...

  7. poj 2777 Count Color(线段树区区+染色问题)

    题目链接:  poj 2777 Count Color 题目大意:  给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C  a  b  c 把区间[a,b]涂为c色,P  a  b 查 ...

  8. POJ 2777 Count Color(线段树之成段更新)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...

  9. poj 3225 线段树+位运算

    略复杂的一道题,首先要处理开闭区间问题,扩大两倍即可,注意输入最后要\n,初始化不能随便memset 采用线段树,对线段区间进行0,1标记表示该区间是否包含在s内U T S ← S ∪ T 即将[l, ...

随机推荐

  1. []with[[]]

    [brand@localhost ~]$ echo $name hello hello [brand@localhost ~]$ [$name = "hello"] -bash: ...

  2. BizTalk开发系列(十) ESB Guidance安装笔记

    ESB指导工具包(ESB Guidance)是一个运行于BizTalk Server 2006 R2之上的一个框架.详细信息访问ESB指导工具包社区网站 .源码下载 ESB Guidance的安装过程 ...

  3. DHTML概述

    <!-- DHTML概述:动态的HTML.不是一门语言,是多项技术综合体的简称. 其中包含了HTML.CSS.DOM.JavaScript. 这四个技术在动态HTML页面效果定义时,都处于什么样 ...

  4. Java类成员(成员变量和方法)的覆盖与隐藏归纳

    以前就知道重写override和重载overload的区别,平时也是逮着用就是了,Eclipse报错再说. 最近看一本书里面出现了重写.替换.覆盖.置换.隐藏.重载,简直乱得不行,归纳整理一下. 从全 ...

  5. smarty模板原理和增删改查例题

    Smarty模板:(前后端分离)原理:核心是一个类,先修改配置文件,在使用的时候引入配置文件即可,(init.inc.php)$smarty->assign("ceshi", ...

  6. SQL2008 SQL2012 远程连接配置方法

    第一步: SQL2008(或2012): 打开SQL Server Management Studio-->在左边[对象资源管理器]中选择第一项(主数据库引擎)-->右键-->方面- ...

  7. LeetCode Kth Largest Element in an Array

    原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...

  8. SQL Server索引进阶第五篇:索引包含列 .

    包含列解析所谓的包含列就是包含在非聚集索引中,并且不是索引列中的列.或者说的更通俗一点就是:把一些底层数据表的数据列包含在非聚集索引的索引页中,而这些数据列又不是索引列,那么这些列就是包含列.同时,这 ...

  9. Lua Rings库介绍

    Rings需求 如果有一段lua脚本代码, 本来来源不可靠, 可能有安全性问题, 或者不像让这份代码污染了正在执行的lua环境, 则需要lua rings工具出厂了. 其在主lua环境中,即在宿主脚本 ...

  10. 关于 Android 5.0 原生系统网络图标上的感叹号问题解决方法

    解决方案 adb shell settings put global captive_portal_server g.cn 参考 关于 android 5.0 网络图标上的感叹号及其解决办法