FZU 2105 Digits Count
Problem 2105 Digits CountAccept: 444 Submit: 2139
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≤i<n).
Then m lines, each line must be one of the 4 operations above. (0≤opn≤15)
Output
Sample Input
Sample Output
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.
线段树,由于数组范围只有0到17,所以会有大量重复的块,所以直接线段树暴力来
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h> using namespace std;
typedef long long int LL;
const int maxn=1e6;
int num[maxn*4+5];
int sum[maxn*4+5];
int n,m;
void pushup(int node)
{
sum[node]=sum[node<<1]+sum[node<<1|1];
if(num[node<<1]==num[node<<1|1]&&num[node<<1]!=-1)
num[node]=num[node<<1];
else
num[node]=-1;
}
void build(int node,int l,int r)
{
if(l==r)
{
scanf("%d",&num[node]);
sum[node]=num[node];
return;
}
int mid=(l+r)>>1;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
pushup(node);
}
void update(int node,int l,int r,int L,int R,int tag,int flag)
{
if(L<=l&&r<=R)
{
if(num[node]!=-1)
{
if(tag==1) num[node]&=flag;
else if(tag==2) num[node]|=flag;
else num[node]^=flag;
sum[node]=num[node]*(r-l+1);
return;
}
int mid=(l+r)>>1;
if(L<=mid)
update(node<<1,l,mid,L,R,tag,flag);
if(R>mid)
update(node<<1|1,mid+1,r,L,R,tag,flag);
pushup(node);
return;
}
int mid=(l+r)>>1;
if(num[node]!=-1)
{
sum[node<<1]=num[node]*(mid-l+1);
sum[node<<1|1]=num[node]*(r-mid);
num[node<<1]=num[node<<1|1]=num[node]; } if(L<=mid) update(node<<1,l,mid,L,R,tag,flag);
if(R>mid) update(node<<1|1,mid+1,r,L,R,tag,flag);
pushup(node);
} int query(int node,int l,int r,int L,int R)
{
if(L<=l&&r<=R)
{
return sum[node];
}
int mid=(l+r)>>1;
if(num[node]!=-1)
{
sum[node<<1]=num[node]*(mid-l+1);
sum[node<<1|1]=num[node]*(r-mid);
num[node<<1]=num[node<<1|1]=num[node]; }
int ret=0;
if(L<=mid) ret+=query(node<<1,l,mid,L,R);
if(R>mid) ret+=query(node<<1|1,mid+1,r,L,R);
return ret;
}
int main()
{
int t;
scanf("%d",&t);
char a[10];
int x,y,z;
while(t--)
{
scanf("%d%d",&n,&m);
memset(num,-1,sizeof(num));
build(1,1,n);
for(int i=1;i<=m;i++)
{
scanf("%s",a);
if(a[0]=='A')
{
scanf("%d%d%d",&x,&y,&z);
update(1,1,n,y+1,z+1,1,x);
}
else if(a[0]=='O')
{
scanf("%d%d%d",&x,&y,&z);
update(1,1,n,y+1,z+1,2,x);
}
else if(a[0]=='X')
{
scanf("%d%d%d",&x,&y,&z);
update(1,1,n,y+1,z+1,3,x);
}
else
{
scanf("%d%d",&x,&y);
printf("%d\n",query(1,1,n,x+1,y+1));
}
} }
return 0;
}
FZU 2105 Digits Count的更多相关文章
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- FZU 2105 Digits Count(线段树)
Problem 2105 Digits Count Accept: 302 Submit: 1477 Time Limit: 10000 mSec Memory Limit : 262144 KB P ...
- FZU 2105 Digits Count(位数计算)
Description 题目描述 Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: Operation ...
- 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 ...
- FZU 2105 Digits Count(按位维护线段树)
[题目链接] http://acm.fzu.edu.cn/problem.php?pid=2105 [题目大意] 给出一个序列,数字均小于16,为正数,每次区间操作可以使得 1. [l,r]区间and ...
- FZU Problem 2105 Digits Count
Problem Description Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: Operati ...
- FOJ 2105 Digits Count
题意:对一串数字进行抑或某数,和某数,或某数,统计某区间和的操作. 思路:因为化成二进制就4位可以建4颗线段树,每颗代表一位二进制. and 如果该为是1 直接无视,是0则成段赋值为0: or 如 ...
- FZU 2105 (线段树)
Problem 2105 Digits Count Problem Description Given N integers A={A[0],A[1],...,A[N-1]}. Here we h ...
- FZU-2105 Digits Count (两种标记成段更新)
题目大意:给n个0~15之间的数,有3种更新操作,1种询问操作.3种更新操作是:1.让某个闭区间的所有数字与一个0~15之间的数字进行逻辑与运算:2.让某个闭区间的所有数字与一个0~15之间的数字进行 ...
随机推荐
- 【转】【C++】C++ 中的线程、锁和条件变量
线程 类std::thread代表一个可执行线程,使用时必须包含头文件<thread>.std::thread可以和普通函数,匿名函数和仿函数(一个实现了operator()函数的类)一同 ...
- Java String 学习
String, 首先,String有字面值常量的概念,这个字面值常量是在编译期确定下来的,类加载时直接存入常量池(注意,常量池是类的常量池,类与类之间隔离). 而运行时生成的字符串,是不在常量池中的. ...
- e553. 作为浏览器访问URL
// See also e551 精简的Applet try { URL url = new URL(getDocumentBase(), "http://hostname.com/page ...
- Zookeeper CLI
ZooKeeper命令行界面(CLI)用于与ZooKeeper集合进行交互以进行开发.它有助于调试和解决不同的选项. 要执行ZooKeeper CLI操作,首先打开ZooKeeper服务器(“bin/ ...
- 使用_snscanf_s转换十六进制时引起的内存越界
//将Hex编码转换为指定编码格式的字符串 string Encoding::DecodeHexString(const string &strSrc, UINT code_page ) { ...
- python PIL 库处理文件
通过PIL库提供的API接口可以很方便的处理图像,功能十分强大: 最近有一个替换png背景色的需求,替换背景色的同时又不能够覆盖原来的文字,之前利用perl 的CD 模块一直没能够正确处理,最终用PI ...
- sublime text全局搜索,查找对应类插件
windows平台下的操作. 1.你必须先安装package controller 否则请先安装 2. 图1 一.如果Preferences > Browse Packages菜单下没有Pa ...
- OpenCV学习:OpenCV介绍
OpenCV全称是:Open Source Computer Vision Library(开源计算机视觉库). 于1999年由Intel建立,如今由Willow Garage提供支持. 跨平台,可以 ...
- 如何创建 SVN 服务器,并搭建自己的 SVN 仓库
SVN是一个广泛使用的版本控制系统,在日常工作中有广泛的应用空间,比如文档管理,我们就可以搭建一个 SVN 仓库,日常需要归类保存的文档统统上传到仓库中,那如何创建 SVN 服务器, 又如何搭建 SV ...
- android:json解析的两个工具:Gson和Jackson的使用小样例
1.简单介绍 json是android与server通信过程中经常使用的数据格式,比如.例如以下是一个json格式的字符串: {"address":"Nanjing&qu ...