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 API 连接MySQL及批量插入
CMySQLMgr.h: #ifndef _CMYSQLMGR_H_ #define _CMYSQLMGR_H_ #include <iostream> #include "my ...
- c++ 的vector、array和数组的比较
ref: http://blog.csdn.net/haust_wang/article/details/49848169
- centos7命令总结
1,查看cpu信息 lscpu 2,网络配置 ip route 查看路由 nmcli nmcli connection show 显示所有连接 nmcli connection show ...
- SPP-Net
R-CNN -> SPP-Net -> Fast-RCNN
- windows上SVN图标不显示
症状1:项目左侧导航栏表不能正常显示图标 方法:windows->preferences->General->Appearance->Label Decorations ...
- mysql表无权限访问
当网页出现以上问题时的解决方法: 今天在两台服务器间转移网站,最后把域名解释设置好后等待...然后CMD查看DNS解释情况..解释成功-输入网址-却出现如上信息,首先用#ls -l查看mysql下的v ...
- ios开发之--pop到指定页面
1 推出到根视图控制器 [self.navigationController popToRootViewControllerAnimated:YES]; 2 推出到指定的视图控制器 for (UIVi ...
- Python 流程控制:if
语法: if 判断条件1: # 如果判断条件1成立,就执行语句1 语句1... if 判断条件1: # 如果判断条件1成立,就执行语句1,否则执行语句2 语句1... else: 语句2... if ...
- PHP Web 木马扫描器代码
scanner.php:<?php/**************PHP Web木马扫描器************************//* [+] 作者: alibaba *//* [+] ...
- WinForm软件开机自动启动详细方法
现在正在制作一个物资公司的管理软件,把自己掌握的学到的一点点细细的讲给喜欢C#的同仁们,互相交流. 想要给你制作的应用程序做一个开机启动,很方便,你可以让用户选择,在你的工具栏中的某个下拉菜单里添加一 ...