CS Course

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 430    Accepted Submission(s): 222

Problem Description
Little A has come to college and majored in Computer and Science.

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 .

 
Input
There are no more than 15 test cases.

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

 
Output
For each query p, output three non-negative integers indicates the result of bit-operations(and, or, xor) of all non-negative integers except  in a line.
 
Sample Input

3 3 
1 1 1
3
 
Sample Output

1 1 0 
1 1 0 
1 1 0
 
Source
 
Recommend
liuyiding

题目链接: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 (连续位运算)的更多相关文章

  1. HDU 6186 CS Course【前后缀位运算枚举/线段树】

    [前后缀枚举] #include<cstdio> #include<string> #include<cstdlib> #include<cmath> ...

  2. HDU 5014 Number Sequence(位运算)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 解题报告:西安网赛的题,当时想到一半,只想到从大的开始匹配,做异或运算得到对应的b[i],但是少 ...

  3. hdu 5491 The Next (位运算)

    http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意:给定一个数D,它的二进制数中1的个数为L,求比D大的数的最小值x且x的二进制数中1的个数num满 ...

  4. HDU 6186 CS Course(前缀+后缀)

    http://acm.hdu.edu.cn/showproblem.php?pid=6186 题意:给出n个数,共有n次询问,每次询问给出一个数p,求除去第p个数后的n-1个数的&.|.^值. ...

  5. HDU 6186 CS Course 前缀和,后缀和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6186 题意:给了n个数,然后有q个查询,每个查询要求我们删掉一个数,问删掉这个数后整个序列的与值,或值 ...

  6. HDU - 4810 - Wall Painting (位运算 + 数学)

    题意: 从给出的颜料中选出天数个,第一天选一个,第二天选二个... 例如:第二天从4个中选出两个,把这两个进行异或运算(xor)计入结果 对于每一天输出所有异或的和 $\sum_{i=1}^nC_{n ...

  7. HDU 6186 CS Course

    保存前缀后缀. 保存一下前缀和后缀,去掉第$i$个位置,就是$L[i-1]$和$R[i+1]$进行运算. #include<bits/stdc++.h> using namespace s ...

  8. HDU 3006 The Number of set(位运算 状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3006 题目大意:给定n个集合,每个集合都是由大于等于1小于等于m的数字组成,m最大为14.由给出的集合 ...

  9. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

随机推荐

  1. 一个简单的spring boot程序

    搭建一个spring boot项目十分的方便,网上也有许多,可以参考 https://www.cnblogs.com/ityouknow/p/5662753.html 进行项目的搭建.在此我就不详细介 ...

  2. 《图解机器学习-杉山将著》读书笔记---CH1

    CH1 什么是机器学习 重点提炼 机器学习的种类: 常分为:监督学习.无监督学习.强化学习等 监督学习是学生从老师那获得知识,老师提供对错指示 无监督学习是在没有老师的情况下,学生自习 强化学习是在没 ...

  3. ASP.NET Core Web程序托管到Windows 服务

    前言 在 .NET Core 3.1和WorkerServices构建Windows服务 我们也看到了,如何将workerservices构建成服务,那么本篇文章我们再来看看如何将web应用程序托管到 ...

  4. PHP实现取得HTTP请求的原文【转】

    本文实例讲述了PHP实现取得HTTP请求的原文的方法,具体步骤如下: 1. 取得请求行:Method.URI.协议 可以从超级变量$_SERVER中获得,三个变量的值如下: $_SERVER['REQ ...

  5. JUnit 5和Selenium基础(二)

    使用Selenium内置的PageFactory实现页面对象模式 在这一部分中,将通过Selenium的内置PageFactory支持类来介绍Page Object模式的实现.PageFactory提 ...

  6. floj 2265 【lxs Contest #141】航海舰队

    首先抠出包围了阵形的最小矩形. 将地图拉伸成一条链,即将第一行.第二行.第三行按顺序连接.阵形也可以用同样的方法处理. 那么问题转化为,给定两个 01 串 S 和 T,问每个 S 中长度为 |T| 的 ...

  7. Bonny手机APP试用体验

    在上周四(即6月13日)下午,应王建民老师的邀请,我参观了学长学姐们的软件设计评比以及专业交流的活动,看到了形形色色学长学姐设计出的软件我觉得非常有趣,并对学长学姐们设计的软件的种类与功能感到由衷的钦 ...

  8. 三、Spring Cloud之软负载均衡 Ribbon

    前言 上一节我们已经学习了Eureka 注册中心,其实我们也使用到了Ribbon ,只是当时我们没有细讲,所以我们现在一起来学习一下Ribbon. 什么是Ribbon 之前接触到的负载均衡都是硬负载均 ...

  9. must appear in the GROUP BY clause or be used in an aggregate function

    今天在分组统计的时候pgsql报错 must appear in the GROUP BY clause or be used in an aggregate function,在mysql里面是可以 ...

  10. Spring Boot2 系列教程(二十) | SpringBoot 是如何实现日志的?

    微信公众号:一个优秀的废人.如有问题,请后台留言,反正我也不会听. 前言 休息日闲着无聊看了下 SpringBoot 中的日志实现,把我的理解跟大家说下. 门面模式 说到日志框架不得不说门面模式.门面 ...