HDU 6186 CS Course(前缀+后缀)

http://acm.hdu.edu.cn/showproblem.php?pid=6186

题意:
给出n个数,共有n次询问,每次询问给出一个数p,求除去第p个数后的n-1个数的&、|、^值。

思路:
分别计算出&、|、^的前缀和后缀,将前缀和后缀相计算即可。

 #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e5+; int n, q;
int a[maxn], prea[maxn], suffa[maxn], preo[maxn], suffo[maxn], prex[maxn], suffx[maxn]; int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&q))
{
scanf("%d",&a[]);
prea[] = preo[] = prex[] = a[];
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
prea[i] = a[i] & prea[i-];
preo[i] = a[i] | preo[i-];
prex[i] = a[i] ^ prex[i-];
}
suffa[n] = suffo[n] = suffx[n] = a[n];
for(int i=n-;i>=;i--)
{
suffa[i] = a[i] & suffa[i+];
suffo[i] = a[i] | suffo[i+];
suffx[i] = a[i] ^ suffx[i+];
}
while(q--)
{
int pos; scanf("%d",&pos);
int ans1 = prea[pos-] & suffa[pos+];
int ans2 = preo[pos-] | suffo[pos+];
int ans3 = prex[pos-] ^ suffx[pos+];
if(pos==)
{
ans1 = suffa[pos+];
ans2 = suffo[pos+];
ans3 = suffx[pos+];
}
if(pos==n)
{
ans1 = prea[pos-];
ans2 = preo[pos-];
ans3 = prex[pos-];
}
printf("%d %d %d\n", ans1, ans2, ans3);
}
}
return ;
}
上一篇:全球部署的分布式数据库 YugabyteDB,了解一下?


下一篇:初识Anrdiod SDK