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 ...
随机推荐
- HotSpot关联规则算法(2)-- 挖掘连续型和离散型数据
本篇代码可在 http://download.csdn.net/detail/fansy1990/8502323下载. 前篇<HotSpot关联规则算法(1)-- 挖掘离散型数据>分析了离 ...
- unity3d 数学的数学基础和辅助类
转载注明smartdot:http://my.oschina.net/u/243648/blog/67193 1. 数学(点乘/叉乘)/unity3d的数学辅助类 2. 坐标系统(本地/世界/屏幕 ...
- poj 1789 Truck History(kruskal算法)
主题链接:http://poj.org/problem?id=1789 思维:一个一个点,每两行之间不懂得字符个数就看做是权值.然后用kruskal算法计算出最小生成树 我写了两个代码一个是用优先队列 ...
- HDOJ 2665 Kth number
静态区间第K小....划分树裸题 Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- silverlight与wcf双向通讯 例子
本文将建立一个silverlight与wcf双向通讯的简单实例,以下是详细步骤: 新建Silverlight应用程序,名称WCFtest.解决方案中添加WCF服务应用程序,名称WcfServiceTe ...
- GIT用服务器上的版本强制覆盖掉本地的版本(转)
以前在机子上使用git下载了linux git版本库上的源代码,由于在阅读源代码的过程中,难免会对本地的文件进行修改,这样就导致本地文件与服务器上的文件版本不一致,我想做的就是用服务器上的版本强制覆盖 ...
- Hibernate4.3.9Final常见问题汇总
hibernate4下一个可用的hibernate.properties: jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql ...
- 同步特定源代码到 omni_rom源代码目录里面
#!/bin/bash base_path="/Volumes/Android/omnirom_5.0" #此目录是我存放源代码的目录 xiaomi_device="de ...
- JSP+Ajax站点开发小知识
一.JSP基础 1.<select name="love" size="3">当中的size属性指定了列表框显示选项的条数.假设全部选项多于这个 ...
- android数据访问模式:档、SharedPreferences
android数据访问模式:档.SharedPreferences.SQLite 数据库.Content provider 文件流: 使用java IO流对文件进行读写操作,文件权限默认. 指定文件权 ...