POJ-2777-CountColor(线段树,位运算)
链接:https://vjudge.net/problem/POJ-2777#author=0
题意:
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.
思路:
线段树,还是普通的线段树,染色的查询和更新使用位运算,因为颜色区间在(1-30)之内。
所以可以使用(1<<1-1<<30)来表示这中二进制1的个数来表示颜色的数量。
不过我之前的写的普通的线段树我也不知道为啥会WA。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <string>
#include <stack>
#include <vector>
#include <queue> using namespace std;
typedef long long LL;
const int MAXN = 1e5+10; int Seg[MAXN*4];
int lazy[MAXN*4];
int Vis[100];
int n, t, o;
int res; void PushDown(int root)
{
if (lazy[root] != 0)
{
Seg[root<<1] = (1<<lazy[root]);
Seg[root<<1|1] = (1<<lazy[root]); lazy[root<<1] = lazy[root];
lazy[root<<1|1] = lazy[root];
lazy[root] = 0;
}
} void PushUp(int root)
{
Seg[root] = Seg[root<<1]|Seg[root<<1|1];
} void Build(int root, int l, int r)
{
if (l == r)
{
Seg[root] = 2;
return;
}
int mid = (l + r) / 2;
Build(root << 1, l, mid);
Build(root << 1 | 1, mid + 1, r);
PushUp(root);
} void Update(int root, int l, int r, int ql, int qr, int c)
{
if (r < ql || qr < l)
return;
if (ql <= l && r <= qr)
{
Seg[root] = (1<<c);
lazy[root] = c;
return;
}
PushDown(root);
int mid = (l+r)/2;
Update(root<<1, l, mid, ql, qr, c);
Update(root<<1|1, mid+1, r, ql, qr, c);
PushUp(root);
} int Query(int root, int l, int r, int ql, int qr)
{
if (r < ql || qr < l)
return 0;
if (ql <= l && r <= qr)
{
return Seg[root];
}
int mid = (l+r)/2;
PushDown(root);
int col1 = 0, col2 = 0;
col1 = Query(root<<1, l, mid, ql, qr);
col2 = Query(root<<1|1, mid+1, r, ql, qr);
return col1|col2;
} int Get(int x)
{
int res = 0;
while (x)
{
if (x&1)
res++;
x >>= 1;
}
return res;
} int main()
{
char op[10];
int a, b, c;
while (~scanf("%d%d%d", &n, &t, &o))
{
Build(1, 1, n);
while (o--)
{
scanf("%s", op);
if (op[0] == 'C')
{
scanf("%d%d%d", &a, &b, &c);
if (a > b)
swap(a, b);
Update(1, 1, n, a, b, c);
}
else
{
scanf("%d%d", &a, &b);
if (a > b)
swap(a, b);
memset(Vis, 0, sizeof(Vis));
int res = Query(1, 1, n, a, b);
printf("%d\n", Get(res));
}
}
} return 0;
}
POJ-2777-CountColor(线段树,位运算)的更多相关文章
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- POJ 2777 Count Color(线段树+位运算)
题目链接:http://poj.org/problem?id=2777 Description Chosen Problem Solving and Program design as an opti ...
- poj 3225 线段树+位运算
略复杂的一道题,首先要处理开闭区间问题,扩大两倍即可,注意输入最后要\n,初始化不能随便memset 采用线段树,对线段区间进行0,1标记表示该区间是否包含在s内U T S ← S ∪ T 即将[l, ...
- hdu 5023 线段树+位运算
主要考线段树的区间修改和区间查询,这里有一个问题就是这么把一个区间的多种颜色上传给父亲甚至祖先节点,在这里题目告诉我们最多30颜色,那么我们可以把这30中颜色用二进制储存和传给祖先节点,二进制的每一位 ...
- Codeforces 620E New Year Tree(线段树+位运算)
题目链接 New Year Tree 考虑到$ck <= 60$,那么用位运算统计颜色种数 对于每个点,重新标号并算出他对应的进和出的时间,然后区间更新+查询. 用线段树来维护. #includ ...
- Count Color(线段树+位运算 POJ2777)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...
- Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)
链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowerca ...
- poj_2777线段树+位运算
第一次没想到用位运算,不出意料的T了,,, PS:在床上呆了接近两个月后,我胡汉三又杀回来刷题啦-- #include<iostream> #include<cstdio> # ...
- poj 2777(线段树的节点更新策略)
/* 之前的思想是用回溯的方式进行颜色的更新的!如果用回溯的方法的话,就是将每一个节点的颜色都要更新 通过子节点的颜色情况来判断父节点的颜色情况 !这就是TLE的原因! 后来想一想没有必要 !加入[a ...
- 【洛谷】【线段树+位运算】P2574 XOR的艺术
[题目描述:] AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的01串. 2. 给定一个范围[ ...
随机推荐
- css中字体大小在不同浏览器兼容性问题
css中使用font-size设定字体大小,不同浏览器的字体height一样,但是width不同,比如在火狐和谷歌中,font-size:20px,字体的高度变为20px,但是谷歌的字体宽度比火狐长 ...
- 2015 年最热门的国人开发开源软件 TOP 50
开源中国在 2015 年得到了快速的发展,单开源软件收藏量就接近 40000 款,其中不乏优质的国产开源项目.本文从软件的收藏.下载.访问等多角度挑选出了 2015 年最热门的国产开源软件前五十名,让 ...
- Java微信公众平台开发_03_消息管理之被动回复消息
GitHub源码:https://github.com/shirayner/weixin_gz 一.本节要点 1.回调url 上一节,我们启用服务器配置的时候,填写了一个服务器地址(url),如下图, ...
- windows与Linux操作系统的差别
用户需要记住:Linux和Windows在设计上就存在哲学性的区别.Windows操作系统 倾向于将更多的功能集成到操作系统内部,并将程序与内核相结合:而Linux不同 于Windows,它的内核空间 ...
- LNMP安装(二)
PHP安装 1.yum安装一些依赖库 yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel ...
- 华为机试 可怕的N阶乘
题目标题: 计算阶乘n!是一件可怕的事情,因为当n并不是很大时,n!将是一个很大的值.例如13! = 6227020800,已经超过了我们常用的unsigned int类型的取值范围.请设计一个程序, ...
- linux命令学习笔记(31): /etc/group文件详解
Linux /etc/group文件与/etc/passwd和/etc/shadow文件都是有关于系统管理员对用户和用户组管理时相关的文件. linux /etc/group文件是有关于系统 管理员对 ...
- bzoj 2159 Crash 的文明世界 & hdu 4625 JZPTREE —— 第二类斯特林数+树形DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2159 使用公式:\( n^{k} = \sum\limits_{i=0}^{k} S(k,i ...
- CDN网络原理
1.用户向浏览器输入www.web.com这个域名,浏览器第一次发现本地没有dns缓存,则向网站的DNS服务器请求: 2.网站的DNS域名解析器设置了CNAME,指向了www.web.51cdn.co ...
- C#如何立即回收内存
1.把对象赋值为null 2.立即调用GC.Collect(); 注意:这个也只是强制垃圾回收器去回收,但具体什么时候执行不确定. 代码: class Test { ~Test() { Consol ...