Count Colour_poj2777(线段树+位)
POJ 2777 Count Color (线段树)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 29895 | Accepted: 8919 |
Description
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
Output
Sample Input
2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2
Sample Output
2
1
Source
分析 一个有n(n<100000)个单位的画板,但颜色只有30种,我没有马上想到位运算,但后来和一个宋词学仙交流的时候她告诉了我可以用位,不过她也是先用的布尔数组来判断的,可能会TLE
#include<iostream>
#include<cstdio>
#define maxx 100005
#define L(u) (u<<1)
#define R(u) (u<<1|1)
using namespace std;
int n,t,Q;
int result,ans;
struct xx{
int ls,rs;
int clr;
int sta;
}node[maxx<<];
void pushup(int u)
{
node[u].clr=node[R(u)].clr|node[L(u)].clr;
}
void pushdown(int u)
{
node[u].sta=;
node[R(u)].clr=node[u].clr;
node[L(u)].clr=node[u].clr;
if(node[L(u)].ls!=node[L(u)].rs)
node[L(u)].sta=;
if(node[R(u)].ls!=node[R(u)].rs)
node[R(u)].sta=;
}
void Build(int u,int l,int r)
{
node[u].ls=l;
node[u].rs=r;
node[u].clr=;
if(l==r)
return;
int mid=(l+r)>>;
Build(L(u),l,mid);
Build(R(u),mid+,r);
}
void Updata(int u,int l,int r,int c)
{
if(l==node[u].ls&&node[u].rs==r)
{
node[u].clr=(<<c);
if(node[u].ls!=node[u].rs)node[u].sta=;
return;
}
if(node[u].sta)pushdown(u); //下传是必要的
int mid=(node[u].ls+node[u].rs)>>;
if(r<=mid) Updata(L(u),l,r,c);
else if(l>mid) Updata(R(u),l,r,c);
else{
Updata(L(u),l,mid,c);
Updata(R(u),mid+,r,c);
}
pushup(u);
}
void Qurey(int u,int l,int r)
{
if(l==node[u].ls&&node[u].rs==r)
{
result=result|node[u].clr;
return;
}
if(node[u].sta)pushdown(u); //下传是必要的
int mid=(node[u].ls+node[u].rs)>>;
if(r<=mid) Qurey(L(u),l,r);
else if(l>mid) Qurey(R(u),l,r);
else{
Qurey(L(u),l,mid);
Qurey(R(u),mid+,r);
}
}
int main()
{
while(~scanf("%d %d %d\n",&n,&t,&Q))
{
Build(,,n);
while(Q--)
{
char x;
int a,b,c;
scanf("%c",&x);
if(x=='P')
{
result=,ans=;
scanf("%d %d\n",&a,&b);
if(a>b)
{a=a+b;b=a-b;a=a-b;}
Qurey(,a,b);
while(result)
{
result>>=;
if(&result)ans++;
}
cout<<ans<<endl;
}
else
{
scanf("%d %d %d\n",&a,&b,&c);
if(a>b)
{a=a+b;b=a-b;a=a-b;}//注意查询的区间可能左大于右数据怪怪的
Updata(,a,b,c);
}
}
}
return ;
}
Count Colour_poj2777(线段树+位)的更多相关文章
- Count Color(线段树+位运算 POJ2777)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- [poj2777] Count Color (线段树 + 位运算) (水题)
发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- poj 2777 Count Color(线段树区区+染色问题)
题目链接: poj 2777 Count Color 题目大意: 给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C a b c 把区间[a,b]涂为c色,P a b 查 ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- poj 3225 线段树+位运算
略复杂的一道题,首先要处理开闭区间问题,扩大两倍即可,注意输入最后要\n,初始化不能随便memset 采用线段树,对线段区间进行0,1标记表示该区间是否包含在s内U T S ← S ∪ T 即将[l, ...
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- Subsequence Count (线段树)
Time Limit: 1000 ms Memory Limit: 256 MB Description 给定一个01串 $S_{1 \cdots n}$ 和 $Q$ 个操作. 操作有两种类型: ...
随机推荐
- python笔记一
好奇,想一探究竟.安装就出点小问题,win7,64位,一直卡在这里不动了? 只好取消.第二天安装仍是如此. 于是下载Windows6.1-KB2999226-x64.msu,安装,仍卡顿不动: 于是找 ...
- 在python中处理XML
XML是实现不同语言或程序之间进行数据交换的协议,XML文件格式如下: <data> <country name="Liechtenstein"> < ...
- ArcGIS生成根据点图层生成等值面并减小栅格锯齿的操作步骤
一.打开ArcMAP并加载上相应的点图层和边界面图层 二.ArcToolbox--Spatial Analyst工具--差值分析--克里金法(根据不同的情况选择不同的算法,这次的处理实际上使用的是样条 ...
- c++子类调用基类方法的一个例子
Base.h #pragma once class Base { public: Base(void); ~Base(void); bool CreatClone( ...
- 关于mysql 的一些零碎.
/* 又在做自己以前做的事.总是拿以前的眼光来看现在,导致了其实自己已经很low,但是还觉得自己很xxx. 好吧,最近开始PHP审计.jishigou!!!!!! */ 查看日志情况. show va ...
- centos 常用命令
查看centos版本:cat /etc/redhat-release
- javascript中document.appendChild和document.body.appendChild的问题
在IE7中 var conentDiv = document.createElement("div"); document .body .appendChild(conentDiv ...
- JavaIDL开发CORBA实例演示
转载 http://www.micmiu.com/opensource/corba/corba-javaidl-dev-demo/
- anomaly detection algorithm
anomaly detection algorithm 以上就是异常监测算法流程
- JAVA课程实验报告 实验三 敏捷开发与XP实践
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1353 姓名:韩玉琪 学号:20135317 成绩: 指导教师:娄嘉 ...