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 <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int Max = + ;
int n, m;
struct Node
{
int l, r;
int num;
};
Node node[Max * ];
int A[Max];
void buildtree(int l, int r, int k)
{
node[k].l = l;
node[k].r = r;
node[k].num = -;
if (l == r)
{
node[k].num = A[l];
return;
}
int mid = (l + r) / ;
buildtree(l, mid, k * );
buildtree(mid + , r, k * + );
if (node[k * ].num >= && node[k * ].num == node[k * + ].num) // 如果左边区间和右边区间 num 相同,就要更改父节点
{
node[k].num = node[k * ].num;
}
}
int getopt(int num, int opn, int opt)
{
if (opt == )
return opn & num;
if (opt == )
return opn | num;
if (opt == )
return (opn ^ num);
return ;
}
void update(int l, int r, int k, int opn, int opt)
{
if (node[k].l == l && node[k].r == r && node[k].num >= )
{
// 区间【l, r】上的数是相同的,只需改一次就ok了
node[k].num = getopt(node[k].num, opn, opt);
return;
}
// 不相同的话就继续往左右两边改
if (node[k].num >= ) // 在改的过程中发现该点标记过,分给子节点,去掉自己的标记
{
node[k * ].num = node[k * + ].num = node[k].num;
node[k].num = -;
}
int mid = (node[k].l + node[k].r) / ;
if (r <= mid)
update(l, r, k * , opn, opt);
else if (mid < l)
{
update(l, r, k * + , opn, opt);
}
else
{
update(l, mid, k * , opn, opt);
update(mid + , r, k * + , opn, opt);
}
if (node[k * ].num >= && node[k * ].num == node[k * + ].num)
node[k].num = node[k * ].num;
}
LL querry(int l, int r, int k)
{
if (node[k].l == l && node[k].r == r && node[k].num >= )
{
return (LL) node[k].num * (LL) (node[k].r - node[k].l + );
}
if (node[k].num >= )
{
node[k * ].num = node[k * + ].num = node[k].num;
node[k].num = -;
}
int mid = (node[k].r + node[k].l) / ;
if (r <= mid)
{
return querry(l, r, k * );
}
else if (mid < l)
{
return querry(l, r, k * + );
}
else
return querry(l, mid, k * ) + querry(mid + , r, k * + );
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
scanf("%d", &A[i]);
buildtree(, n, );
while (m--)
{
char opt[];
int opn, a, b;
scanf("%s", opt);
if (opt[] == 'S')
{
scanf("%d%d", &a, &b);
printf("%I64d\n", querry(a + , b + , ));
}
else
{
scanf("%d%d%d", &opn, &a, &b);
if (opt[] == 'A')
{
update(a + , b + , , opn, );
}
else if (opt[] == 'O')
{
update(a + , b + , , opn, );
}
else
update(a + , b + , , opn, );
}
}
}
return ;
}

FZU 2105Digits Count(线段树 + 成段更新)的更多相关文章

  1. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  2. ZOJ 1610 Count the Colors (线段树成段更新)

    题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...

  3. ACM: Copying Data 线段树-成段更新-解题报告

    Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...

  4. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

  5. hdu 4747【线段树-成段更新】.cpp

    题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...

  6. HDU1698_Just a Hook(线段树/成段更新)

    解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...

  7. HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )

    线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...

  8. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  9. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

随机推荐

  1. jQuery动画与特效详解

    本文主要是讲解和学习jQuery的自动显隐,渐入渐出等. 1.显示和隐藏hide()和show() 对于动画来说,显示和隐藏是最基本的效果之一,本节简单介绍jQuery的显示和隐藏. 代码如下: &l ...

  2. webstorm官网最新版激活:

    2016.2.3版本的破解方式:目前最新的就是2.3版本,在打开的License Activation窗口中选择"activation code",在输入框输入下面的注册码:3B4 ...

  3. Dynamics CRM 2015-超大Solution导入问题

    我们在将比较大的solution导入CRM的时候,经常会遇到超时的问题,这是因为CRM的本身的优化限制导致的,那么如何解决呢? 官方已经有了解决方案了. 在浏览完两种解决方法之后,我们要知道的是: 1 ...

  4. Android View的滑动 动画

    [scrollTo/scrollBy] //控件内的文字会移动,但是控件本身不会移动,而且移动到控件之外之后,文字也就看不见了 if(v.equals(button2)){ button2.scrol ...

  5. 【搬砖】安卓入门(3)- Java开发编程基础--循环控制语句

    04.01_Java语言基础(循环结构概述和for语句的格式及其使用) A:循环结构的分类 for(初始化表达式;条件表达式;循环后的操作表达式) { 循环体; } 复制代码 B:循环结构for语句的 ...

  6. 3D坦克大战游戏源码

    3D坦克大战游戏源码,该游戏是基于xcode 4.3,ios sdk 5.1开发.在xcode4.3.3上完美无报错.兼容ios4.3-ios6.0 ,一款ios平台上难得的3D坦克大战游戏源码,有2 ...

  7. 【Swift】iOS UICollectionView 计算 Cell 大小的陷阱

    前言 API 不熟悉导致的问题,想当然的去理解果然会出问题,这里记录一下 UICollectionView 使用问题. 声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cn ...

  8. [原创]Linux-day1

    原创:转发务必注明出处http://www.cnblogs.com/0zcl/p/6077298.html 一.Linux的基本原则 由目的单一的小程序组成:组合小程序完成复杂任务 一切皆文件 尽量避 ...

  9. [Erlang 0109] From Elixir to Erlang Code

    Elixir代码最终编译成为erlang代码,这个过程是怎样的?本文通过一个小测试做下探索.         编译一旦完成,你就看到了真相   Elixir代码组织方式一方面和Erlang一样才用非常 ...

  10. Linux 6.5(oracle 11.2.0.4)单实例ASM安装

    Linux 6.5(oracle 11.2.0.4) 1.解析主机.配置网络等 /etc/hosts /etc/sysconfig/network /etc/init.d/NetworkManager ...