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. 算法笔记_172:历届试题 波动数列(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 观察这个数列: 1 3 0 2 -1 1 -2 ... 这个数列中后一项总是比前一项增加2或者减少3. 栋栋对这种数列很好奇,他想知道长度 ...

  2. 详解 Spring 3.0 基于 Annotation 的依赖注入实现

    Spring 的依赖配置方式与 Spring 框架的内核自身是松耦合设计的.然而,直到 Spring 3.0 以前,使用 XML 进行依赖配置几乎是唯一的选择.Spring 3.0 的出现改变了这一状 ...

  3. 建立与读取.ini文件

    一般读写ini文件被读写Registry所取代,但我们还是可以透过 win31的传统方式读写ini文件,以存程式目前的相关设定,而於下一次程式执行时再 读回来.目前建议使用GetSetting Sav ...

  4. java面试第十七天

    5.0新特性: 泛型: 泛型的形式: <E> <E extends 类型> <E extends Numner&comparator>  类名&接口 ...

  5. PHP 在Win下的安装

    1:安装集成环境,Wamp或者Appserv.可以快速搭建测试环境. 2:分别下载安装 下载 PHP 从此处下载免费的 PHP:http://www.php.net/downloads.php 下载 ...

  6. 1z0-052 q209_9

    9: You are working on an instance started using the SPFILE. You want to move the Flash Recovery Area ...

  7. Spring Boot 中 Controller 使用

    1.属性配置 2.Controller使用 2.@PathVariable 与 @RequestParam 的区别 (1)@PathVariable (2)@RequestParam 3.@GetMa ...

  8. 拯救者14ISK添加ssd6记录

    说起为何,我要安装ssd,拯救者14isk配置也不低,我加了4Gddr4的内存,目前8G内存ddr4/2G独显ddr5显卡GT960/i5-6300处理器.每次开机响应慢,还会出现磁盘100%,很受不 ...

  9. 转:Windows消息机制要点

    Windows消息机制要点 1. 窗口过程     每个窗口会有一个称为窗口过程的回调函数(WndProc),它带有四个参数,分别为:窗口句柄(Window Handle),消息ID(Message ...

  10. 【微信小程序】详解wx:if elif else的用法(搭配view、block)

    1.搭配view <view wx:if="{{boolean==true}}"> <view class="bg_black">< ...