HDU - 6186

 

 

        因为这个and,or,xor都有区间的性质,因此个人感觉,可以线段树维护,查询log,查询 1 到p-1 ,然后p+1到n的这段区间得到答案,但是wa到死,有点难受。其实可以与预处理出所有的前缀和后缀,前k个and,or,xor的值,后k个这些值,在输入p的时候O(1) 查询即可。

代码如下

#include <bits/stdc++.h>
#define sc1(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
using namespace std;
typedef long long LL;
int gcd(int a,int b){if (b == 0) return a; return gcd(b , a%b);}
int lcm(int a, int b){ return a/gcd(a,b)*b;}
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48); ch=getchar();}
    return x*f;
}
const int maxn = 1e5 + 10;
int a[maxn];
LL pre_and[maxn],bac_and[maxn];
LL pre_or[maxn],bac_or[maxn];
LL pre_xor[maxn],bac_xor[maxn];
LL ans1,ans2,ans3;
int main(){
    int n,q;
    while(sc2(n, q) == 2){
        for (int i=1; i<=n; i++)
            sc1(a[i]);
        LL _and = a[1], _or = a[1], _xor = a[1];
        pre_and[1] = pre_or[1] = pre_xor[1] = a[1];
        for (int i=2; i<=n; i++) {
            _and &= a[i];
            _or |= a[i];
            _xor ^= a[i];
            pre_and[i] = _and;
            pre_or[i] = _or;
            pre_xor[i] = _xor;
        }
        bac_and[n] = bac_or[n] = bac_xor[n] = a[n];
        _and = a[n]; _or = a[n]; _xor = a[n];
        for (int i=n-1; i>=1; i--) {
            _and &= a[i];
            _or |= a[i];
            _xor ^= a[i];
            bac_and[i] = _and;
            bac_or[i] = _or;
            bac_xor[i] = _xor;
        }
//        for (int i=1; i<=n; i++) {
//            cout << pre_and[i] << ' ' << bac_and[i] << endl;
//        }
        int p;
        for (int i=0; i<q; i++) {
            sc1(p);
            ans1 = pre_and[p-1] & bac_and[p+1];
            ans2 = pre_or[p-1] | bac_or[p+1];
            ans3 = pre_xor[p-1] ^ bac_xor[p+1];
            if (p == 1){
                ans1 = bac_and[p+1];
                ans2 = bac_or[p+1];
                ans3 = bac_xor[p+1];
            }else if (p == n){
                ans1 = pre_and[p-1];
                ans2 = pre_or[p-1];
                ans3 = pre_xor[p-1];
            }
            printf("%lld %lld %lld\n",ans1,ans2,ans3);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值