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],那么这道 ...
随机推荐
- 【bzoj1579】[Usaco2009 Feb]Revamping Trails 道路升级 分层图最短路
题目描述 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M<=50,000)条双向泥土道路,编号为1..M. 道路i连接牛棚P1_i和P2_i (1 < ...
- hdu 1150 Machine Schedule (二分匹配)
Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- CF961D Pair Of Lines
题目描述 You are given n n n points on Cartesian plane. Every point is a lattice point (i. e. both of it ...
- 【刷题】HDU 4405 Aeroplane chess
Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled fr ...
- [洛谷P2921][USACO08DEC]在农场万圣节Trick or Treat on the Farm
题目大意:给你一张有向图,每个点最多一条出边,问从每个开始,走多少步会到一个已经过的点 题解:$tarjan$缩点,然后建反图$DP$ 卡点:无 C++ Code: #include <cstd ...
- CF#508 1038E Maximum Matching
---题面--- 题解: 感觉还是比较妙的,复杂度看上去很高(其实也很高),但是因为n只有100,所以还是可以过的. 考虑一个很暴力的状态f[i][j][x][y]表示考虑取区间i ~ j的方格,左右 ...
- Java异常捕捉
相信你在处理异常的时候不是每次都把它 throws 掉就完事了,很多时候异常是需要我们自己来 catch 并针对所抛出的 Exception 做一些后续的处理工作. 直接上代码,先贴下面测试需要调用的 ...
- cf 442 div2 F. Ann and Books(莫队算法)
cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...
- POI 2018.10.27
[POI2015]LOG 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进 ...
- Java Web用Freemarker生成带图片的Word文档
步骤一:模板制作 用world2003做一个导出模板,如果有图片则加入一张图片占位,将world另存为xml,将xml中需要导出的内容用Freemarker标签表示,最后另存为.ftl结尾的模板: 步 ...