POJ 2777 Count Color(段树)
职务地址: id=2777">POJ 2777
我去。。
延迟标记写错了。标记到了叶子节点上。。
。
。
这根本就没延迟嘛。。
。怪不得一直TLE。。。
这题就是利用二进制来标记颜色的种类。然后利用或|这个符号来统计每一个区间不同颜色种数。
代码例如以下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
int sum[410000];
int lazy[410000];
int pow1[40];
void PushUp(int rt)
{
sum[rt]=sum[rt<<1]|sum[rt<<1|1];
}
void PushDown(int rt)
{
if(lazy[rt])
{
lazy[rt<<1]=lazy[rt];
lazy[rt<<1|1]=lazy[rt];
sum[rt<<1]=lazy[rt];
sum[rt<<1|1]=lazy[rt];
lazy[rt]=0;
}
}
void update(int ll, int rr, int c, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
sum[rt]=c;
lazy[rt]=c;
return ;
}
PushDown(rt);
int mid=l+r>>1;
if(ll<=mid) update(ll,rr,c,lson);
if(rr>mid) update(ll,rr,c,rson);
PushUp(rt);
}
int query(int ll, int rr, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
return sum[rt];
}
PushDown(rt);
int ans=0;
int mid=l+r>>1;
if(ll<=mid) ans=ans|query(ll,rr,lson);
if(rr>mid) ans=ans|query(ll,rr,rson);
return ans;
}
int get(int x)
{
int ans=0, y;
while(x)
{
y=x%2;
if(y)
ans++;
x/=2;
}
return ans;
}
int main()
{
int n, t, q, a, b, c, i;
char ch;
pow1[0]=1;
for(i=1; i<=30; i++)
{
pow1[i]=pow1[i-1]*2;
}
scanf("%d%d%d",&n,&t,&q);
memset(lazy,0,sizeof(lazy));
for(i=1;i<=3*n;i++)
{
sum[i]=1;
}
while(q--)
{
getchar();
scanf("%c",&ch);
if(ch=='C')
{
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
int tt=a;
a=b;
b=tt;
}
update(a,b,pow1[c-1],1,n,1);
}
else
{
scanf("%d%d",&a,&b);
if(a>b)
{
int tt=a;
a=b;
b=tt;
}
int ans=get(query(a,b,1,n,1));
printf("%d\n",ans);
}
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
POJ 2777 Count Color(段树)的更多相关文章
- 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(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- POJ 2777.Count Color-线段树(区间染色+区间查询颜色数量二进制状态压缩)-若干年之前的一道题目。。。
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53312 Accepted: 16050 Des ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- 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 Count Color Description Chosen Problem Solving and Program desig ...
- POJ 2777 Count Color(线段树染色,二进制优化)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42940 Accepted: 13011 Des ...
随机推荐
- android升级软件版本号,您安装后的新版本号,成功安装画面没有出现,或直接回到桌面
Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //an ...
- 修改系统启动项 grub2配置的方法 ubuntu[转]
在 早期的Ubuntu中,使用Grub作为系统的启动引导程序,想修改系统启动项非常简单,只要用gedit打开系统菜单设定文件( sudo gedit /boot/grub/menu.lst ),修改该 ...
- Dom4j分解xml
package cn.com.guju.util; import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingExc ...
- hidden change事件
原文:hidden change事件 对于隐藏域hidden无法触发onchange的解决方法:在更改此隐藏域的时候,调用下它的onchange方法,使用jquery的话, 就直接加上 $(" ...
- 升级 树莓派的gcc/g++编译器到4.8
首先,更新你的树莓派(避免编译出来版本不对导致不能insert)sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade s ...
- IOS本地化应用
BK项目已完成7788,在项目的后期需要被翻译成多国语言版.为了适应全球多个国家使用多个存储. 应用本地化是分别对字符串.图片和 xib 或 storyboard 文件本地化,而传统的做法是对 xib ...
- DV工作流
- How to install PL/SQL developer on linux (转)
PL/SQL developer 在linux上的安装方法工欲善其事必先利其器,PL/SQL和toad对于ORACLE从业人员来说都是很重要的工具,但这些工具都没有linux的发行版,如果要在linu ...
- [LeetCode290]Word Pattern
题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
- .NET Framework 4.0 以上版本 下载地址
https://msdn.microsoft.com/zh-cn/library/5a4x27ek(v=vs.110).aspx