FZU-2105 Digits Count (两种标记成段更新)
题目大意:给n个0~15之间的数,有3种更新操作,1种询问操作。3种更新操作是:1、让某个闭区间的所有数字与一个0~15之间的数字进行逻辑与运算;2、让某个闭区间的所有数字与一个0~15之间的数字进行逻辑或运算;3、让某个闭区间的所有数字与一个0~15之间的数字进行异或运算。一种询问操作是询问某个闭区间的所有数字之和。
题目分析:所有的输入数字都是在0~15之间,可以二进制中的每一位建立线段树,建立四棵。3种更新操作实际上只有两种,即区间置位和区间反转。用两个懒惰标记,当进行区间置位更新的时候,要清除已经存在的反转标记,这样就能保证标记下传时两种标记下传的先后次序。
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std; # define LL long long const int N=1000000; int n,m;
int a[N+5];
int tr[4][N*4+5];
int lazy[4][N*4+5];
int lazy_xor[4][N*4+5];
char com[5]; void pushDown(int id,int rt,int l,int r)
{
int mid=l+(r-l)/2;
if(lazy[id][rt]!=-1){
lazy_xor[id][rt<<1]=lazy_xor[id][rt<<1|1]=0;
lazy[id][rt<<1]=lazy[id][rt<<1|1]=lazy[id][rt];
tr[id][rt<<1]=lazy[id][rt]*(mid-l+1);
tr[id][rt<<1|1]=lazy[id][rt]*(r-mid);
lazy[id][rt]=-1;
}
int &q=lazy_xor[id][rt];
if(q==1){
lazy_xor[id][rt<<1]^=1;
lazy_xor[id][rt<<1|1]^=1;
tr[id][rt<<1]=mid-l+1-tr[id][rt<<1];
tr[id][rt<<1|1]=r-mid-tr[id][rt<<1|1];
q=0;
}
} void pushUp(int id,int rt)
{
tr[id][rt]=tr[id][rt<<1]+tr[id][rt<<1|1];
} void build(int id,int rt,int l,int r)
{
lazy[id][rt]=-1;
lazy_xor[id][rt]=0;
if(l==r){
if((a[r]&(1<<id))>0) tr[id][rt]=1;
else tr[id][rt]=0;
}else{
int mid=l+(r-l)/2;
build(id,rt<<1,l,mid);
build(id,rt<<1|1,mid+1,r);
pushUp(id,rt);
}
} void update(int id,int rt,int l,int r,int L,int R,int x)
{
if(L<=l&&r<=R){
if(x==2){
if(lazy[id][rt]!=-1) lazy[id][rt]^=1;
else lazy_xor[id][rt]^=1;
tr[id][rt]=(r-l+1)-tr[id][rt];
}else{
lazy[id][rt]=x;
lazy_xor[id][rt]=0;
tr[id][rt]=x*(r-l+1);
}
}else{
pushDown(id,rt,l,r);
int mid=l+(r-l)/2;
if(L<=mid) update(id,rt<<1,l,mid,L,R,x);
if(R>mid) update(id,rt<<1|1,mid+1,r,L,R,x);
pushUp(id,rt);
}
} LL query(int id,int rt,int l,int r,int L,int R)
{
if(L<=l&&r<=R) return tr[id][rt];
pushDown(id,rt,l,r);
int mid=l+(r-l)/2;
LL res=0;
if(L<=mid) res+=query(id,rt<<1,l,mid,L,R);
if(R>mid) res+=query(id,rt<<1|1,mid+1,r,L,R);
return res;
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;++i) scanf("%d",a+i);
for(int i=0;i<4;++i)
build(i,1,0,n-1);
int o,b,c;
while(m--)
{
scanf("%s",com);
if(com[0]=='S'){
scanf("%d%d",&b,&c);
int ans=0;
for(int i=0;i<4;++i){
ans+=query(i,1,0,n-1,b,c)*(1<<i);
}
printf("%d\n",ans);
}else{
scanf("%d%d%d",&o,&b,&c);
if(com[0]=='X'){
for(int i=0;i<4;++i) if(o&(1<<i))
update(i,1,0,n-1,b,c,2);
}else if(com[0]=='O'){
for(int i=0;i<4;++i) if(o&(1<<i))
update(i,1,0,n-1,b,c,1);
}else if(com[0]=='A'){
for(int i=0;i<4;++i) if(!(o&(1<<i)))
update(i,1,0,n-1,b,c,0);
}
}
}
}
return 0;
}
FZU-2105 Digits Count (两种标记成段更新)的更多相关文章
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- FZU 2105 Digits Count(线段树)
Problem 2105 Digits Count Accept: 302 Submit: 1477 Time Limit: 10000 mSec Memory Limit : 262144 KB P ...
- FZU 2105 Digits Count
Problem 2105 Digits Count Accept: 444 Submit: 2139 Time Limit: 10000 mSec Memory Limit : 2621 ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- FZU 2105 Digits Count(位数计算)
Description 题目描述 Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: Operation ...
- fzu 2105 Digits Count ( 线段树 ) from 第三届福建省大学生程序设计竞赛
http://acm.fzu.edu.cn/problem.php?pid=2105 Problem Description Given N integers A={A[0],A[1],...,A[N ...
- FZU 2105 Digits Count(按位维护线段树)
[题目链接] http://acm.fzu.edu.cn/problem.php?pid=2105 [题目大意] 给出一个序列,数字均小于16,为正数,每次区间操作可以使得 1. [l,r]区间and ...
- mysql级联更新的两种方式:触发器更新和外键
1.mysql级联更新有两种方式:触发器更新和外键更新. 2.触发器更新和外键更新的目的都是为了保证数据完整性. 我们通常有这样的需求:删除表Table 1中记录,需要同时删除其它表中与Table 1 ...
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
随机推荐
- Windows的同步I/O和异步I/O
同步I/O操作 执行步骤 1. 程序通过FileStream对象来打开磁盘文件,然后调用Read方法(内部调用Win32 ReadFile函数),从文件中读取数据.这时,线程从托管代码转 ...
- jquery animate()方法使用的注意事项
当使用 animate() 时,必须使用 Camel 标记法书写所有的属性名,比如,必须使用 paddingLeft 而不是 padding-left,使用 marginRight 而不是 margi ...
- HDU 4627 E(Contest #3)
Description There are many unsolvable problem in the world.It could be about one or about zero.But t ...
- SQL Server 语句整理
1. 创建数据库 create database dbName 2. 删除数据库 drop database dbName 3. 备份sql server --- 创建 备份数据的 device US ...
- CSS练习一(模仿163邮箱登陆)
// '); code = code.replace(/&/g, '&'); return code; }; var runCode = function (code) { if (c ...
- activeMQ设置admin的用户名和密码
ActiveMQ使用的是jetty服务器, 打开conf/jetty.xml文件,找到 <bean id="securityConstraint" class="o ...
- Java(JVM运行时)各种内存区域详解及扩展
本文整理于 Java内存与垃圾回收调优 Java 堆内存 从几个sample来学习Java堆,方法区,Java栈和本地方法栈 首先来一张图让我们理清楚java运行时状态: 诚然,如上图所示:java ...
- php大力力 [035节] 先记录一些链接
[IT名人堂]专访百分点研发总监:不止于平台,大数据操作系统重磅来袭! [2015-8-11 14:17:04] [IT名人堂]专访1号店技术总监:大型电商网站的IT架构 [2015-8-25 15: ...
- BZOJ 2252 矩阵距离
BFS. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...
- BZOJ 1045 糖果传递
奇怪的式子.最后发现取中位数. #include<iostream> #include<cstdio> #include<cstring> #include< ...