Problem 2105 Digits Count

Accept: 302 Submit: 1477

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≤in).

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.

由于数据特别多,但是数据的值不大(<16),所以必有大量重复的元素就可以进行区间合并

#include <set>
#include <map>
#include <list>
#include <stack>
#include <cmath>
#include <vector>
#include <queue>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define PI cos(-1.0)
#define RR freopen("input.txt","r",stdin)
using namespace std;
typedef long long LL;
const int MAX = 1000010;
int Seg[MAX*6];
int Arr[MAX];
int n,m;
int Oper(int num,int PN,int OP)//进行操作
{
switch(OP)
{
case 1:
return num&PN;
case 2:
return num|PN;
case 3:
return num^PN;
}
return 0;
} void Build(int L,int R,int site)//建立线段树
{
if(L==R)
{
Seg[site]=Arr[L];
return ;
}
int mid=(L+R)>>1;
Build(L,mid,site<<1);
Build(mid+1,R,site<<1|1);
if(Seg[site<<1]==Seg[site<<1|1]&&Seg[site<<1]!=1)//将值相同的区间进行合并,如果不相同则为-1.
{
Seg[site]=Seg[site<<1];
}
else
{
Seg[site]=-1;
}
}
void Update(int L,int R,int l,int r,int site,int PN,int OP)//更新操作
{
if(L==l&&R==r&&Seg[site]!=-1)//三种操作
{
Seg[site]=Oper(Seg[site],PN,OP);
return ;
}
int mid = (L+R)>>1;
if(Seg[site]!=-1)//向下更新,如过对已经合并的区间里面进行操作,则需要将区间先拆分,更行完以后再判断是否可以合并
{
Seg[site<<1]=Seg[site<<1|1]=Seg[site];
Seg[site]=-1;
}
if(r<=mid)
{
Update(L,mid,l,r,site<<1,PN,OP);
}
else if(l>mid)
{
Update(mid+1,R,l,r,site<<1|1,PN,OP);
}
else
{
Update(L,mid,l,mid,site<<1,PN,OP);
Update(mid+1,R,mid+1,r,site<<1|1,PN,OP);
}
if(Seg[site<<1]==Seg[site<<1|1]&&Seg[site<<1]!=1)//区间合并
{
Seg[site]=Seg[site<<1];
}
}
int Query(int L,int R,int l,int r,int site)//查询
{
if(L==l&&R==r&&Seg[site]!=-1)
{
return (R-L+1)*Seg[site];
}
if(Seg[site]!=-1)//查询的时候,如果要查询一个区间内的区间,则需要先将区间向下更新(想想为什么?);
{
Seg[site<<1]=Seg[site<<1|1]=Seg[site];
Seg[site]=-1;
}
int mid=(L+R)>>1;
if(r<=mid)
{
return Query(L,mid,l,r,site<<1);
}
else if(l>mid)
{
return Query(mid+1,R,l,r,site<<1|1);
}
else
{
return Query(L,mid,l,mid,site<<1)+Query(mid+1,R,mid+1,r,site<<1|1);
}
}
int main()
{
int T;
char str[15];
int l,r,PN;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&Arr[i]);
}
Build(0,n-1,1);
for(int i=1;i<=m;i++)
{
scanf("%s",str);
if(strcmp(str,"SUM")==0)
{
scanf("%d %d",&l,&r);
printf("%d\n",Query(0,n-1,l,r,1));
}
else
{
scanf("%d %d %d",&PN,&l,&r);
if(strcmp(str,"AND")==0)
{
Update(0,n-1,l,r,1,PN,1);
}
else if(strcmp(str,"OR")==0)
{
Update(0,n-1,l,r,1,PN,2);
}
else if(strcmp(str,"XOR")==0)
{
Update(0,n-1,l,r,1,PN,3);
}
}
}
}
return 0;
}

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

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

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

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

  3. FZU 2105 Digits Count

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

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

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

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

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

  6. HDU 6155 Subsequence Count 线段树维护矩阵

    Subsequence Count Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Oth ...

  7. FOJ 2105 Digits Count

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

  8. [HDU6155]Subsequence Count(线段树+矩阵)

    DP式很容易得到,发现是线性递推形式,于是可以矩阵加速.又由于是区间形式,所以用线段树维护. https://www.cnblogs.com/Miracevin/p/9124511.html 关键在于 ...

  9. FZU 2105Digits Count(线段树 + 成段更新)

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

随机推荐

  1. 【转】Tomcat总体结构(Tomcat源代码阅读系列之二)

    本文是Tomcat源代码阅读系列的第二篇文章,我们在本系列的第一篇文章:在IntelliJ IDEA 和 Eclipse运行tomcat 7源代码一文中介绍了如何在intelliJ IDEA 和 Ec ...

  2. "数学口袋精灵"bug的发现及单元测试

    1.项目内容: 团队项目:二次开发 至此,我们有了初步的与人合作经验,接下来投入到更大的团队中去. 也具备了一定的个人能力,能将自己的代码进行测试.接下来尝试在别人已有的基础上进行开发. 上一界51冯 ...

  3. linux第2天 信号 wait

    孤儿进程和僵尸进程 如果父进程先退出,子进程还没退出那么子进程的父进程将变为init进程.(注:任何一个进程都必须有父进程) 如果子进程先退出,父进程还没退出,那么子进程必须等到父进程捕获到了子进程的 ...

  4. js正则表达式进行格式校验

    今天做了个js正则表达式的练习,利用正则表达式进行注册信息格式验证,注册信息界面如下: 格式要求: 1.学号项不能为空,必须为纯数字,不能与数据库中的重复,正则表达式/^\d+$/g: 2.姓名项不能 ...

  5. phpredis 订阅者模式

    [TOC] 一.场景介绍 最近的一个项目需要用到发布/订阅的信息系统,以做到最新实时消息的通知.经查找后发现了redis pub/sub(发布/订阅的信息系统)可以满足我的开发需求,而且学习成本和使用 ...

  6. Aspose.cell处理Excel

    (一)从数据库中读取数据写入Excel中 方法1: 步骤:1.建立一个新的项目,引用动态链接库Aspose.dll 2.见下面的原代码 using System;using System.Collec ...

  7. 夺命雷公狗---node.js---9实现页面的跳转

    废话不多说,我们先来看看项目的文件结构,如下所示: 然后我们创建一个index.js的文件让他来做端口监听,代码如下所示: var http = require('http'); var fs = r ...

  8. 夺命雷公狗---Thinkphp----12之文章的增删改查(图片上传和关联查询)

    我们由于表分析的不够完善,所以我们来加多一个tid的字段,到时候主要目的是为了更好的遍历出文章是属于那个分类下的,表如下所示: 那么下一步我们就开始创建一个ArticleController.clas ...

  9. libSVM的数据格式

    首先介绍一下 libSVM的数据格式 Label 1:value 2:value -. Label:是类别的标识,比如上节train.model中提到的1 -1,你可以自己随意定,比如-10,0,15 ...

  10. 【RoR win32】新建rails项目找不到script/server的解决办法

    现象: D:\>rails new work/demo cd work/demo D:\work\demo>ruby script/server 这时显示出错: ruby: No such ...