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. C++ 模板类友元之输出流操作符重载

    几个关键点: 需要前置声明!--奇怪的是别人告诉我也可以不需要,但我这里不行! 友元函数的函数名后面的<>,必须要有. #include <stdio.h> #include ...

  2. 第二百九十六节,python操作redis缓存-Hash哈希类型,可以理解为字典类型

    第二百九十六节,python操作redis缓存-Hash哈希类型,可以理解为字典类型 Hash操作,redis中Hash在内存中的存储格式如下图: hset(name, key, value)name ...

  3. 下面属于javascript对象的有:( )

    下面属于javascript对象的有:( ) A. Window B. Document C. Form D. String E. Navigator 解答:ACE

  4. centos7 修改主机名的方法(在centos7有效)

    参考链接:http://www.centoscn.com/CentOS/config/2014/1031/4039.html 在CentOS或RHEL中,有三种定义的主机名:a.静态的(static) ...

  5. jQuery实现鼠标经过时高亮,同时其他同级元素变暗的效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. OpenCV学习:体验ImageWatch

    Image Watch是在VS2012及以上版本上使用的一款OpenCV插件工具,能够实时显示图像和矩阵Mat的内容,跟Matlab很像,方便程序调试,相当好用. 1)安装Visual Studio ...

  7. Shell脚本中$0、$?、$!、$$、$*、$#、$@等的意义

    http://blog.csdn.net/slovyz/article/details/47400107

  8. 制作SD卡启动自己编译的uboot.bin

    README for FriendlyARM Tiny4412 -----------------------------------------------------1. Build uboot ...

  9. BaiduMap 鼠标绘制矩形选框四个顶角坐标的获取

    雪影工作室版权全部.转载请注明[http://blog.csdn.net/lina791211] 1.博文产生原因 在使用百度Map开放API进行开发的时候,遇到了一个需求,非常easy的一个需求. ...

  10. Cocos2d-x3.0终于版Mac以及Win系统相关环境部署

    因个人原因此博客停止更新,其它更新博文将在该博客继续更新. http://blog.csdn.net/xiaohan_aimti/article/details/24653831 就在前几天,2014 ...