HDU 6186 CS Course

本文介绍了一种使用位运算进行区间查询的方法。该方法通过预处理得到前后缀的与运算、或运算及异或运算结果,进而快速计算任意区间的与、或、异或操作。适用于需要频繁查询的数据结构问题。

保存前缀后缀。

保存一下前缀和后缀,去掉第$i$个位置,就是$L[i-1]$和$R[i+1]$进行运算。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 100000 + 10;
int Land[maxn], Rand[maxn];
int Lor[maxn], Ror[maxn];
int Xor;
int a[maxn];
int n, p;

int main() {
  while(~scanf("%d%d", &n, &p)) {
    Xor = 0;
    for(int i = 1; i <= n; i ++) {
      scanf("%d", &a[i]);
    }
    Land[1] = Lor[1] = Xor = a[1];
    for(int i = 2; i <= n; i ++) {
      Land[i] = Land[i - 1] & a[i];
      Lor[i] = Lor[i - 1] | a[i];
      Xor = Xor ^ a[i];
    }
    Rand[n] = Ror[n] = a[n];
    for(int i = n - 1; i >= 1; i --) {
      Rand[i] = Rand[i + 1] & a[i];
      Ror[i] = Ror[i + 1] | a[i];
    }
    for(int i = 1; i <= p; i ++) {
      int pos;
      scanf("%d", &pos);
      if(pos == 1) {
        printf("%d %d %d\n", Rand[2], Ror[2], Xor ^ a[1]);
      } else if(pos == n) {
        printf("%d %d %d\n", Land[n - 1], Lor[n - 1], Xor ^ a[n]);
      } else {
        printf("%d %d %d\n", Land[pos - 1] & Rand[pos + 1], Lor[pos - 1] | Ror[pos + 1], Xor ^ a[pos]);
      }
    }
  }
  return 0;
}

  

转载于:https://www.cnblogs.com/zufezzt/p/7492816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值