HDU 6186 CS Course (连续位运算)
CS Course
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 430 Accepted Submission(s): 222
Today he has learned bit-operations in Algorithm Lessons, and he got a problem as homework.
Here is the problem:
You are giving n non-negative integers , and some queries.
A query only contains a positive integer p, which means you
are asked to answer the result of bit-operations (and, or, xor) of all the integers except .
Each test case begins with two positive integers n and p
in a line, indicate the number of positive integers and the number of queries.
Then n non-negative integers follows in a line, for each i in range[1,n].
After that there are q positive integers in q lines, for each i in range[1,q].
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6186
题意:从连续的and/or/xor中去掉某一个。。
法一:保存所有元素的某一位bit的和,查找ap有没有因为自己的某一位而改变全部的or/and,如果有,就改回来
法二:三个位运算都有交换率律结合律,所以可以保存前缀连续and/or和后缀连续and/or,注except第一个或者最后一个元素的时候要特判一下。
另外,因为异或具有性质:a^b^b = a ,所以可以将直接将1~n的异或再异或要去掉的ap即可
法二代码
#include<cstdio>
#include<iostream>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = 100000 + 100;
int n,q;
int a[maxn];
int andl[maxn], andr[maxn], orl[maxn], orr[maxn], xorl[maxn], xorr[maxn];
int main(){
while(~scanf("%d %d", &n, &q)){
//memset(a,-1,sizeof(a));
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
}
andl[1] = orl[1] = xorl[1] = a[1];
andr[n] = orr[n] = a[n];
for(int i = 2; i <= n; i++){
andl[i] =andl[i-1]&a[i];
orl[i] = orl[i-1]|a[i];
xorl[i] = xorl[i-1]^a[i] ;
}
for(int i = n-1; i >= 1; i--){
andr[i] = andr[i+1]&a[i];
orr[i] = orr[i+1]|a[i];
}
for(int i = 0; i < q; i++){
int pp;
scanf("%d", &pp);
if(pp==1)printf("%d %d %d\n", andr[2], orr[2], xorl[n]^a[pp]);
else if(pp==n)printf("%d %d %d\n", andl[pp-1], orl[pp-1], xorl[n]^a[pp]);
else printf("%d %d %d\n", andl[pp-1]&andr[pp+1], orl[pp-1]|orr[pp+1], xorl[n]^a[pp]);
}
}
return 0;
}
HDU 6186 CS Course (连续位运算)的更多相关文章
- HDU 6186 CS Course【前后缀位运算枚举/线段树】
[前后缀枚举] #include<cstdio> #include<string> #include<cstdlib> #include<cmath> ...
- HDU 5014 Number Sequence(位运算)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 解题报告:西安网赛的题,当时想到一半,只想到从大的开始匹配,做异或运算得到对应的b[i],但是少 ...
- hdu 5491 The Next (位运算)
http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意:给定一个数D,它的二进制数中1的个数为L,求比D大的数的最小值x且x的二进制数中1的个数num满 ...
- HDU 6186 CS Course(前缀+后缀)
http://acm.hdu.edu.cn/showproblem.php?pid=6186 题意:给出n个数,共有n次询问,每次询问给出一个数p,求除去第p个数后的n-1个数的&.|.^值. ...
- HDU 6186 CS Course 前缀和,后缀和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6186 题意:给了n个数,然后有q个查询,每个查询要求我们删掉一个数,问删掉这个数后整个序列的与值,或值 ...
- HDU - 4810 - Wall Painting (位运算 + 数学)
题意: 从给出的颜料中选出天数个,第一天选一个,第二天选二个... 例如:第二天从4个中选出两个,把这两个进行异或运算(xor)计入结果 对于每一天输出所有异或的和 $\sum_{i=1}^nC_{n ...
- HDU 6186 CS Course
保存前缀后缀. 保存一下前缀和后缀,去掉第$i$个位置,就是$L[i-1]$和$R[i+1]$进行运算. #include<bits/stdc++.h> using namespace s ...
- HDU 3006 The Number of set(位运算 状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3006 题目大意:给定n个集合,每个集合都是由大于等于1小于等于m的数字组成,m最大为14.由给出的集合 ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
随机推荐
- C++装饰器模式
UML图: #include <iostream> #include <string> #include <windows.h> using namespace s ...
- WPF 添加提示动画
下面放一张效果图: 那么具体是怎么实现呢: 前端XAML中: <Image Source="/Images/tips.png" HorizontalAlignment=&qu ...
- js字符数组转化为数字数组
先谈一谈需求,我有一个字符串数组,数组内容是字符类型的数字,需要把字符类型的数字转换为数字类型,找了找资料,发现牛人真是多,把前辈的经验总结一下. 例子 ['1','2','3'] => [1, ...
- php5升php7代码修改整理
1. 为什么升级至php7 a.安全需要 过早以至于不维护的php版本难免会有未修补的漏洞,安全性要求较高的行业是要及时升级的. b.性能提升(开启opcache hugepage) 据说性能提升一倍 ...
- 一个由"2020年1月7日 京东出现的重大 Bug 漏洞"引起的思考...
2020年1月7日,京东由于优惠券设置错误,导致大量产品以0元或者超低价成交,并且发货.网传小家电被薅24万件,损失损失金额高达7000多万.很多网友表示收到货了,在网上晒出到货截图.下面为购买截图: ...
- springboot2 + grpc + k8s + istio
项目情况说明: ubuntu - 16.04 springboot - 2.2.2.RELEASE mysql - 5.7 mongodb - 4.0.14 redis - 3.0.6 grpc - ...
- python的range()
range() 是一个函数,用来生成一个自然数的序列 用list()显示具体内容 扩展 # range()是一个函数,可以用来生成一个自然数的序列 r = range(5) # 生成一个这样的序列 ...
- Excel大数据排查重复行内容方法,三步搞定!
首先第一步,我们找到一个空白列D输入公式“=A1&B1&C1”: 然后第二步,再选择下一空白列输入公式“=IF(COUNTIF(D:D,D1)>1,"重复", ...
- [DP][SA][可持久化线段树]黑红兔
源自 xyz32768 菜鸡的 FJ 省冬令营模拟赛题 原题 CF1063F Statement 给定一个长度为 \(n\) 的字符串 \(s\),仅包含小写英文字母 要从中从左往右选出若干段不相交的 ...
- Excel.Application使用手册
Excel.Application组件使用方法,适合应用于使用EXCEL组件做WEB应用开发. 转自http://bbs.xtjc.com/thread-376095-1-1.html 定制模块行为( ...