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. Apache+Tomcat负载均衡集群搭建

    1.所需软件 apache_2.2.4-win32-x86-no_ssl.apacheserver mod_jk-apache-2.2.4连接器,连接apache和tomcat apache-tomc ...

  2. python 读帧和绘图的区别

    capture = cv2.VideoCapture(0) while True: #img = cv.QueryFrame(capture) ret, frame = capture.read() ...

  3. vue 数组 新增元素 响应式原理 7种方法

    1.问题 思考一个问题,以下代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  4. iphone openssh

    安装openssh 用户名:默认是 root 密码:默认是 alpine 修改登陆密码:passwd

  5. import 如何工作

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #import 如何工作 #程序第一次导入指定文件时,会执行三个步骤 #1)找到模块文件 #2)编译成位码(需 ...

  6. memcached 下载安装

    wget http://memcached.org/latest tar -zxvf memcached-1.x.x.tar.gz cd memcached-1.x.x ./configure &am ...

  7. 各大主流.Net的IOC框架

    Autofac下载地址:http://code.google.com/p/autofac/ Castle Windsor下载地址:http://sourceforge.net/projects/cas ...

  8. 深入PHP内核之参数

    1.看一下一个扩展中的简单代码 ZEND_BEGIN_ARG_INFO(params_add_arginfo, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) Z ...

  9. HDUOJ---2955 Robberies

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  10. 给maven仓库添加镜像

    用maven下载好慢啊,快试试阿里云镜像吧!修改$MAVEN_HOME/conf/settings.xml <!-- 阿里云仓库 --> <mirror> <id> ...