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

1
4 4
1 2 4 7
SUM 0 2
XOR 5 0 0
OR 6 0 3
SUM 0 2

Sample Output

7
18

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.

代码如下:

#include <stdio.h>
#include <string.h>
const int N = ; int c, res, a[N*][], flag[N*][], len[N*]; void pushDown( int rt )
{
int ls = * rt; int rs = ls | ;
for( int i = ; i < ; ++i )
{
if( flag[rt][i] == - ) continue;
if( flag[rt][i] == || flag[rt][i] == )
{
a[ls][i] = ( flag[ls][i] = flag[rt][i] ) * len[ls];
a[rs][i] = ( flag[rs][i] = flag[rt][i] ) * len[rs];
flag[rt][i] = -;
}
else if( flag[rt][i] == )
{
flag[rt][i] = -;
}
else
{
a[ls][i] = len[ls] - a[ls][i];
if( flag[ls][i] == - ) flag[ls][i] = ;
else flag[ls][i] ^= ;
a[rs][i] = len[rs] - a[rs][i];
if( flag[rs][i] == - ) flag[rs][i] = ;
else flag[rs][i] ^= ;
flag[rt][i] = -;
} }
} void pushUp( int rt )
{
int ls = * rt; int rs = ls | ;
for( int i = ; i < ; ++i )
a[rt][i] = a[ls][i] + a[rs][i];
} void build( int l, int r, int rt )
{
len[rt] = r - l + ;
if( l == r )
{
scanf("%d", &c);
for( int i = ; i < ; ++i )
a[rt][i] = ( & (c>>i) );
}
else
{
int mid = ( l + r ) / ;
build(l, mid, * rt);
build(mid + , r, * rt + );
pushUp( rt );
}
} void OR( int rt, int opn )
{
for( int i = ; i < ; ++i )
{
if( !( (opn>>i) & ) ) continue;
flag[rt][i] = ;
a[rt][i] = len[rt];
}
} void ADD( int rt, int opn )
{
for( int i = ; i < ; ++i )
{
if( (opn>>i) & ) continue;
flag[rt][i] = ;
a[rt][i] = ;
}
} void XOR( int rt, int opn )
{
for( int i = ; i < ; ++i )
{
if( !( (opn>>i) & ) ) continue;
if( flag[rt][i] == - ) flag[rt][i] = ;
else flag[rt][i] ^= ;
a[rt][i] = len[rt] - a[rt][i];
}
} void query( int l, int r, int rt, const int aa, const int bb )
{
if( aa <= l && r <= bb )
{
res = res + a[rt][] + a[rt][] * + a[rt][] * + a[rt][] * ;
return;
}
pushDown( rt );
int mid = ( l + r ) / ;
int ls = * rt; int rs = ls | ;
if( mid >= aa ) query( l, mid, ls, aa, bb );
if( mid < bb ) query( mid + , r, rs, aa, bb );
} void update( int l, int r, int rt, const int aa, const int bb, const int opn, const int f )
{
if( aa <= l && r <= bb )
{
switch( f )
{
case : OR( rt, opn ); break;
case : ADD( rt, opn ); break;
case : XOR( rt, opn ); break;
}
return;
}
pushDown( rt );
int mid = ( l + r ) / ;
int ls = * rt; int rs = ls | ;
if( mid >= aa ) update( l, mid, ls, aa, bb, opn, f );
if( mid < bb ) update( mid + , r, rs, aa, bb, opn, f );
pushUp( rt );
} int main()
{
int t, n, m, aa, bb, opn; char cmd[];
scanf("%d", &t);
while( t-- )
{
memset( flag, -, sizeof(flag) );
scanf("%d%d", &n, &m);
build(, n-, );
while( m-- )
{
scanf("%s", cmd);
if( cmd[] == 'S' )
{
scanf("%d%d", &aa, &bb);
res = ;
query( , n - , , aa, bb );
printf("%d\n", res);
}
else
{
scanf("%d%d%d", &opn, &aa, &bb);
switch( cmd[] )
{
case 'O': update(, n-, , aa, bb, opn, ); break;
case 'A': update(, n-, , aa, bb, opn, ); break;
case 'X': update(, n-, , aa, bb, opn, ); break;
default : break;
}
}
}
}
return ;
}

FZU Problem 2105 Digits Count的更多相关文章

  1. FZU 2105 Digits Count(线段树)

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

  2. FZU 2105 Digits Count

     Problem 2105 Digits Count Accept: 444    Submit: 2139 Time Limit: 10000 mSec    Memory Limit : 2621 ...

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

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

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

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

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

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

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

  7. FOJ 2105 Digits Count

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

  8. FZu Problem 2233 ~APTX4869 (并查集 + sort)

    题目链接: FZu Problem 2233 ~APTX4869 题目描述: 给一个n*n的矩阵,(i, j)表示第 i 种材料 和 第 j 种材料的影响值,这个矩阵代表这n个物品之间的影响值.当把这 ...

  9. FZu Problem 2236 第十四个目标 (线段树 + dp)

    题目链接: FZu  Problem 2236 第十四个目标 题目描述: 给出一个n个数的序列,问这个序列内严格递增序列有多少个?不要求连续 解题思路: 又遇到了用线段树来优化dp的题目,线段树节点里 ...

随机推荐

  1. ViewPager切换动画效果改动

    比方我们点击向右button,希望左边的view移动过来,有个平移效果,可是用系统默认的ViewPager切换的时候,会一闪而过. 这是为什么呢? 由于viewpager外面事实上有个scrollvi ...

  2. Windows注意目录

    win7: c:\windows\tracing%windir%\system32\logfiles\C:\Users\Administrator\AppData\Roaming\softC:\Use ...

  3. ibatis 批量更新(一)

      1.4.2.3 批量修改 支持单个动态更新.批量动态更新 <update id="updateCONSULT_SCHEDULEDynamic" parameterClas ...

  4. eclipse 修改maven项目的jdk版本

      eclipse 修改maven项目的jdk版本 CreationTime--2018年6月8日10点29分 Author:Marydon 1.情景展示 jdk版本太低,如何修改 2.错误方式 第一 ...

  5. 〖Linux〗Ubuntu13.04解决Chrome的flash中文乱码的问题。

    1. 安装flash sudo aptitude install flashplugin-installer 2. 禁用chrome自带的flash插件 在chrome浏览器中输入 chrome:// ...

  6. DIY手机万能红外遥控器

    DIY手机万能红外遥控器 1.手机安装软件:遥控精灵 2.红外二极管两个(旧的遥控器里面可以拆) 3.耳机的插头一个 步骤:首选讲两个二极管的正负相接(即a二极管的正极连接b二极管的负极,a二极管的负 ...

  7. 13、java中8中基本类型

    一.基本类型介绍 关键字 数据类型 占用字节数 取值范围 默认值 byte 字节型 1个字节 -128~127 0 char 字符型 2个字节 Unicode0~Unicode215-1 \u0000 ...

  8. python核心编程--笔记(不定时跟新)(转)

    的解释器options: 1.1 –d   提供调试输出 1.2 –O   生成优化的字节码(生成.pyo文件) 1.3 –S   不导入site模块以在启动时查找python路径 1.4 –v   ...

  9. HttpClient 解释

    HttpClient:是一个接口 首先需要先创建一个DefaultHttpClient的实例 HttpClient httpClient=new DefaultHttpClient(); 发送GET请 ...

  10. 连表查询都用Left Join吧 以Windows服务方式运行.NET Core程序 HTTP和HTTPS的区别 ASP.NET SignalR介绍 asp.net—WebApi跨域 asp.net—自定义轻量级ORM C#之23中设计模式

    连表查询都用Left Join吧   最近看同事的代码,SQL连表查询的时候很多时候用的是Inner Join,而我觉得对我们的业务而言,99.9%都应该使用Left Join(还有0.1%我不知道在 ...