hdu 6186 CS Course (前后缀求区间问题)

题意:  给你一组序列,然后给你一个查询p,让你查询出这组序列除了a[p]之外的 |,&,^的值是多少?

思路: 区间除了p以外的话吗,我们可以维护一个前后缀就好了,然后每次p的时候 ,就让a[p-1] 和 a[p+1] 进行操作就好了,

(这么裸,我特么竟然还在找规律,,,真是菜)上代码吧:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define mes(a) memset(a,0,sizeof(a));
using namespace std;
const int maxn = 1e5+10;
int a[maxn];
int Or[maxn],And[maxn],Xor[maxn];
int rOr[maxn],rAnd[maxn],rXor[maxn];
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		mes(a);
		mes(Or);mes(Xor);mes(And);
		mes(rOr);mes(rXor);mes(rAnd);
		scanf("%d",&a[1]);
		Or[1]=Xor[1]=And[1]=a[1];
		for(int i = 2 ; i <= n;i++)
		{
			scanf("%d",&a[i]);
			 Or[i] =  Or[i-1] | a[i];
			Xor[i] = Xor[i-1] ^ a[i];
			And[i] = And[i-1] & a[i];
		}
		rOr[n] = rAnd[n] = rXor[n] = a[n]; 
		for(int i = n - 1 ; i>=1 ;i--)
		{
			rOr[i] = rOr[i+1]|a[i];
			rXor[i] = rXor[i+1]^a[i];
			rAnd[i] = rAnd[i+1]&a[i];
		}
		while(m--)
		{
			int p;
			scanf("%d",&p);
			if(p == 1)
			{
				printf("%d %d %d\n",rAnd[p+1],rOr[p+1],rXor[p+1]);
			}
			else if(p == n)
			{
				printf("%d %d %d\n",And[p-1],Or[p-1],Xor[p-1]);
			}
			else 
			{
				printf("%d %d %d\n",And[p-1]&rAnd[p+1],Or[p-1]|rOr[p+1],Xor[p-1]^rXor[p+1]);
			//	printf("%d  %d\n",Xor[p-1],rXor[p+1]);
			}
			
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值