Problem 2105 Digits Count

Accept: 444    Submit: 2139

Time Limit: 10000 mSec    Memory Limit : 262144 KB

 Problem Description

Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations:

Operation 1: AND opn L R

Here opn, L and R are integers.

For L≤i≤R, we do A[i]=A[i] AND opn (here "AND" is bitwise operation).

Operation 2: OR opn L R

Here opn, L and R are integers.

For L≤i≤R, we do A[i]=A[i] OR opn (here "OR" is bitwise operation).

Operation 3: XOR opn L R

Here opn, L and R are integers.

For L≤i≤R, we do A[i]=A[i] XOR opn (here "XOR" is bitwise operation).

Operation 4: SUM L R

We want to know the result of A[L]+A[L+1]+...+A[R].

Now can you solve this easy problem?

 Input

The first line of the input contains an integer T, indicating the number of test cases. (T≤100)

Then T cases, for any case, the first line has two integers n and m (1≤n≤1,000,000, 1≤m≤100,000), indicating the number of elements in A and the number of operations.

Then one line follows n integers A[0], A[1], ..., A[n-1] (0≤A[i]<16,0≤i<n).

Then m lines, each line must be one of the 4 operations above. (0≤opn≤15)

 Output

For each test case and for each "SUM" operation, please output the result with a single line.

 Sample Input

14 41 2 4 7SUM 0 2XOR 5 0 0OR 6 0 3SUM 0 2

 Sample Output

718

 Hint

A = [1 2 4 7]

SUM 0 2, result=1+2+4=7;

XOR 5 0 0, A=[4 2 4 7];

OR 6 0 3, A=[6 6 6 7];

SUM 0 2, result=6+6+6=18.

线段树,由于数组范围只有0到17,所以会有大量重复的块,所以直接线段树暴力来

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h> using namespace std;
typedef long long int LL;
const int maxn=1e6;
int num[maxn*4+5];
int sum[maxn*4+5];
int n,m;
void pushup(int node)
{
sum[node]=sum[node<<1]+sum[node<<1|1];
if(num[node<<1]==num[node<<1|1]&&num[node<<1]!=-1)
num[node]=num[node<<1];
else
num[node]=-1;
}
void build(int node,int l,int r)
{
if(l==r)
{
scanf("%d",&num[node]);
sum[node]=num[node];
return;
}
int mid=(l+r)>>1;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
pushup(node);
}
void update(int node,int l,int r,int L,int R,int tag,int flag)
{
if(L<=l&&r<=R)
{
if(num[node]!=-1)
{
if(tag==1) num[node]&=flag;
else if(tag==2) num[node]|=flag;
else num[node]^=flag;
sum[node]=num[node]*(r-l+1);
return;
}
int mid=(l+r)>>1;
if(L<=mid)
update(node<<1,l,mid,L,R,tag,flag);
if(R>mid)
update(node<<1|1,mid+1,r,L,R,tag,flag);
pushup(node);
return;
}
int mid=(l+r)>>1;
if(num[node]!=-1)
{
sum[node<<1]=num[node]*(mid-l+1);
sum[node<<1|1]=num[node]*(r-mid);
num[node<<1]=num[node<<1|1]=num[node]; } if(L<=mid) update(node<<1,l,mid,L,R,tag,flag);
if(R>mid) update(node<<1|1,mid+1,r,L,R,tag,flag);
pushup(node);
} int query(int node,int l,int r,int L,int R)
{
if(L<=l&&r<=R)
{
return sum[node];
}
int mid=(l+r)>>1;
if(num[node]!=-1)
{
sum[node<<1]=num[node]*(mid-l+1);
sum[node<<1|1]=num[node]*(r-mid);
num[node<<1]=num[node<<1|1]=num[node]; }
int ret=0;
if(L<=mid) ret+=query(node<<1,l,mid,L,R);
if(R>mid) ret+=query(node<<1|1,mid+1,r,L,R);
return ret;
}
int main()
{
int t;
scanf("%d",&t);
char a[10];
int x,y,z;
while(t--)
{
scanf("%d%d",&n,&m);
memset(num,-1,sizeof(num));
build(1,1,n);
for(int i=1;i<=m;i++)
{
scanf("%s",a);
if(a[0]=='A')
{
scanf("%d%d%d",&x,&y,&z);
update(1,1,n,y+1,z+1,1,x);
}
else if(a[0]=='O')
{
scanf("%d%d%d",&x,&y,&z);
update(1,1,n,y+1,z+1,2,x);
}
else if(a[0]=='X')
{
scanf("%d%d%d",&x,&y,&z);
update(1,1,n,y+1,z+1,3,x);
}
else
{
scanf("%d%d",&x,&y);
printf("%d\n",query(1,1,n,x+1,y+1));
}
} }
return 0;
}

FZU 2105 Digits Count的更多相关文章

  1. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  2. FZU 2105 Digits Count(线段树)

    Problem 2105 Digits Count Accept: 302 Submit: 1477 Time Limit: 10000 mSec Memory Limit : 262144 KB P ...

  3. FZU 2105 Digits Count(位数计算)

    Description 题目描述 Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: Operation ...

  4. 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 ...

  5. FZU 2105 Digits Count(按位维护线段树)

    [题目链接] http://acm.fzu.edu.cn/problem.php?pid=2105 [题目大意] 给出一个序列,数字均小于16,为正数,每次区间操作可以使得 1. [l,r]区间and ...

  6. FZU Problem 2105 Digits Count

    Problem Description Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: Operati ...

  7. FOJ 2105 Digits Count

    题意:对一串数字进行抑或某数,和某数,或某数,统计某区间和的操作. 思路:因为化成二进制就4位可以建4颗线段树,每颗代表一位二进制. and 如果该为是1  直接无视,是0则成段赋值为0: or  如 ...

  8. FZU 2105 (线段树)

     Problem 2105 Digits Count  Problem Description Given N integers A={A[0],A[1],...,A[N-1]}. Here we h ...

  9. FZU-2105 Digits Count (两种标记成段更新)

    题目大意:给n个0~15之间的数,有3种更新操作,1种询问操作.3种更新操作是:1.让某个闭区间的所有数字与一个0~15之间的数字进行逻辑与运算:2.让某个闭区间的所有数字与一个0~15之间的数字进行 ...

随机推荐

  1. am335x mux配置

    /**************************************************************** * am335x mux配置 * * am335x的引脚复寄存器是C ...

  2. HTTP/1.1 学习

    发现对于HTTP协议不能脱口而出,故而怒翻资料,RFC2616 . 在其abstract中是这么说HTTP的,应用层协议,generic.无状态.其特点之一是 the typing and negot ...

  3. 安装cx_Oracle 遇到的杂项问题

    1. 解决方法: 将xc用户添加进sudousers 2.安装VMware Tools 更新 http://pubs.vmware.com/vsphere-50/index.jsp?topic=%2F ...

  4. vector deque list

    vector ,deque 和 list 顺序性容器: 向量 vector :   是一个线性顺序结构.相当于数组,但其大小可以不预先指定,并且自动扩展.它可以像数组一样被操作,由于它的特性我们完全可 ...

  5. pushlet 之 Pushlet使用手把手实例

      Pushlet(一种comet 架构的实现)是基于Servlet 机制,数据从server端的Java 对象直接推送(push) 到客户端浏览器的(动态)HTML 页面,而无需任何Java app ...

  6. select自定义小三角样式

    这段代码是网上大部分的解决办法,在这里总结一下: 让select透明,上面加一个span,来替换select框,可以自定义小三角样式,也可以做出select文字居中的效果. <div class ...

  7. unity3d游戏开发学习分享之表面着色器讲解

    一.三种着色器的书写格式: 1.surface shaders, 指的是表面着色器 2.vertex and fragment shaders and 指的是顶点和片段着色器 3.fixed func ...

  8. 【View】之【SimplePillarsView】可多色可圆角柱状图【demo】

    当前版本:SimplePillarsView_v1.0.20140613 直接看效果图. 下附图片的设置方法:myview.setPillars(15, 0xffff00ff, 15, 5); 下附图 ...

  9. [转]五分钟看懂UML类图与类的关系详解

    在画类图的时候,理清类和类之间的关系是重点.类的关系有泛化(Generalization).实现(Realization).依赖(Dependency)和关联(Association).其中关联又分为 ...

  10. html 输入框只允许输入数字

    要想限制文本框只能输入数字,你可以用Html5的标签就可以解决: 为input标签添加样式 type="number"即可. // 限制输入框的数字输入范围 var strPri ...