HDU 6186 CS Course 前缀和,后缀和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6186
题意:给了n个数,然后有q个查询,每个查询要求我们删掉一个数,问删掉这个数后整个序列的与值,或值,异或值的和。
解法:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int n, m, a[maxn], sum1[maxn][2], sum2[maxn][2], sum3[maxn][2]; int main()
{
while(~scanf("%d %d", &n,&m))
{
for(int i=1; i<=n; i++) scanf("%d", &a[i]);
sum1[1][0] = sum2[1][0] = sum3[1][0] = a[1];
for(int i=2; i<=n; i++){
sum1[i][0] = sum1[i-1][0]&a[i];
sum2[i][0] = sum2[i-1][0]|a[i];
sum3[i][0] = sum3[i-1][0]^a[i];
}
sum1[n][1] = sum2[n][1] = sum3[n][1] = a[n];
for(int i=n-1; i>=1; i--){
sum1[i][1] = sum1[i+1][1]&a[i];
sum2[i][1] = sum2[i+1][1]|a[i];
sum3[i][1] = sum3[i+1][1]^a[i];
}
while(m--)
{
int idx;
scanf("%d", &idx);
int ans = INT_MAX;
if(idx > 1) ans &= sum1[idx-1][0];
if(idx < n) ans &= sum1[idx+1][1];
printf("%d ", ans);
ans = 0;
if(idx > 1) ans |= sum2[idx-1][0];
if(idx < n) ans |= sum2[idx+1][1];
printf("%d ", ans);
ans = 0;
if(idx > 1) ans ^= sum3[idx-1][0];
if(idx < n) ans ^= sum3[idx+1][1];
printf("%d\n", ans);
}
}
return 0;
}
HDU 6186 CS Course 前缀和,后缀和的更多相关文章
- HDU 6186 CS Course【前后缀位运算枚举/线段树】
[前后缀枚举] #include<cstdio> #include<string> #include<cstdlib> #include<cmath> ...
- HDU 6186 CS Course(前缀+后缀)
http://acm.hdu.edu.cn/showproblem.php?pid=6186 题意:给出n个数,共有n次询问,每次询问给出一个数p,求除去第p个数后的n-1个数的&.|.^值. ...
- HDU 6186 CS Course
保存前缀后缀. 保存一下前缀和后缀,去掉第$i$个位置,就是$L[i-1]$和$R[i+1]$进行运算. #include<bits/stdc++.h> using namespace s ...
- HDU 6186 CS Course (连续位运算)
CS Course Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- A + B for you again HDU - 1867(最大前缀&最大后缀的公共子缀&kmp删除法)
Problem Description Generally speaking, there are a lot of problems about strings processing. Now yo ...
- HDU6025 Coprime Sequence —— 前缀和 & 后缀和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6025 Coprime Sequence Time Limit: 2000/1000 MS (Java/ ...
- kmp(最长前缀与后缀)
http://acm.hdu.edu.cn/showproblem.php?pid=1358 Period Problem Description For each prefix of a given ...
- 递归算法(二)——前缀转后缀
源码:pretopost.cpp #include "stdafx.h" #include <stdio.h> #include <stack> /**** ...
- POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)
给一个字符串S,求出所有前缀,使得这个前缀也正好是S的后缀.升序输出所有情况前缀的长度.KMP中的next[i]的意义就是:前面长度为i的子串的前缀和后缀的最大匹配长度.明白了next[i],那么这道 ...
随机推荐
- 【bzoj1606】[Usaco2008 Dec]Hay For Sale 购买干草 背包dp
题目描述 约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为C(1≤C≤50000)个单位的马车,去顿因家买一些干草. 顿因有H(1≤H≤5000)包干草,每一包都有它的体 ...
- python函数入门到高级
函数的定义: def test(x): "The function definitions" x+=1 return x def:定义函数的关键字 test:函数名 ():内可定义 ...
- (转)java +libsvm 安装与测试:
libsvm 用SVM实现简单线性分类 (转自:http://www.cnblogs.com/freedomshe/archive/2012/10/09/2717356.html) 0. 下载lib ...
- Android ListView 中加入CheckBox/RadioButton 选择状态保持、全选、反选实现
最近在一个项目中,需要在ListView的item中加入CheckBox,但是遇到的一个问题是上下滑动的时候如果有选择了的CheckBox,就会出现选择项错误的问题,下面将个人的解决方法总结如下;先说 ...
- POJ3347:Kadj Squares——题解
http://poj.org/problem?id=3347 题目大意:给定一些正方形的边长,让他们尽可能向左以45°角排列(不能互相重合),求在上面看只能看到哪几个正方形. ———————————— ...
- BZOJ4650:[NOI2016]优秀的拆分——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4650 https://www.luogu.org/problemnew/show/P1117 如果 ...
- su对环境变量做了什么
服务器是ubuntu12.04 用一个账户app,使用su - app得到的环境变量和直接ssh登录的环境变量不同. 导致su - app,无法执行ifconfig su - app 的环境变量 /u ...
- bzoj1529 [POI2005]ska Piggy banks 并查集
[POI2005]ska Piggy banks Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1508 Solved: 763[Submit][Sta ...
- ACM3371超时问题
这的确也是个大坑: 其实在这是到很简单的最小生成树的题目,但是数据量却很大: 用G++提交会超时,用C++不会超时,而且速度超快: 又长见识了.可惜长得不是做题的能力,而是知道它到底有多坑. #inc ...
- ITEXT5 Font 'd:\SIMSUN.TTC' with 'Identity-H' is not recognized.
用 itextsharp 制作PDF文件的时候发生错误 Font 'd:\SIMSUN.TTC' with 'Identity-H' is not recognized. 原本是 BaseFont b ...