FZU Problem 2105 Digits Count
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.
代码如下:
#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的更多相关文章
- 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
Problem 2105 Digits Count Accept: 444 Submit: 2139 Time Limit: 10000 mSec Memory Limit : 2621 ...
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- 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 ...
- FOJ 2105 Digits Count
题意:对一串数字进行抑或某数,和某数,或某数,统计某区间和的操作. 思路:因为化成二进制就4位可以建4颗线段树,每颗代表一位二进制. and 如果该为是1 直接无视,是0则成段赋值为0: or 如 ...
- FZu Problem 2233 ~APTX4869 (并查集 + sort)
题目链接: FZu Problem 2233 ~APTX4869 题目描述: 给一个n*n的矩阵,(i, j)表示第 i 种材料 和 第 j 种材料的影响值,这个矩阵代表这n个物品之间的影响值.当把这 ...
- FZu Problem 2236 第十四个目标 (线段树 + dp)
题目链接: FZu Problem 2236 第十四个目标 题目描述: 给出一个n个数的序列,问这个序列内严格递增序列有多少个?不要求连续 解题思路: 又遇到了用线段树来优化dp的题目,线段树节点里 ...
随机推荐
- 4、Android Activity的生命周期 Activity的生命周期
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXV4aWt1b18x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- 网站中超链接方式直接添加QQ好友
使用情景: 在图中点击图片,会弹出添加qq好友的窗口进行好友添加. 链接如下: tencent://AddContact/?fromId=45&fromSubId=1&subcmd=a ...
- MySQL 数据库几种类型
关系数据库(MySQL.Oracle.SQL Server.DB2.Postgres)键-值存储数据库(Riak和Redis)面向列的数据库(HBase) 面向文档的数据库(MongoDB 和Couc ...
- yml在线格式转换工具(properties)
分享一个在线properties 转 yml工具,也支持yml转properties, 域名非常简单好记,直接在地址栏里输入toyaml.com,地址:http://toyaml.com/ yml,即 ...
- Jsp和session、request.getSession()
request.getSession(false); 这段代码代表,如果没有和当前request关联的session则不创建session并且返回空 request.getSession(true); ...
- java中的Checked Exception和Unchecked Exception的区别
Java 定义了两种异常: - Checked exception: 继承自 Exception 类是 checked exception.代码需要处理 API 抛出的 checked excepti ...
- ExceptionLess 搭建到本地服务器
Exceptionless 是一个开源的实时的日志收集框架,它可以应用在基于 ASP.NET,ASP.NET Core,Web Api,Web Forms,WPF,Console,MVC 等技术栈的应 ...
- VMware Player 12.5.0 中文免费版
VMWare Player优点:– 免费的 VMWare Player 体积仅90多MB,相比7百多MB且还要付费的 VMware Workstation 无疑更适合个人用户使用– 主界面清爽简洁,几 ...
- iOS- 快速实现展示布局
概述 比较有规律的页面, 快速实现展示布局, 提高开发效率. 详细 代码下载:http://www.demodashi.com/demo/10713.html 看到这个界面,是不是觉得不像那种比较有规 ...
- Erlang中atom的实现
Erlang的原子(atom)在匹配中有着重要作用,它兼顾了可读性和运行效率. 通过atom,可以实现很多灵活高效的应用. atom可以看作是给字符串生成了一个ID,内部使用的是ID值,必要时可以取出 ...