题解 | #毒瘤xor#

毒瘤xor

https://ac.nowcoder.com/acm/problem/18979

//01的异或运算每一位都是独立的运算
//以及与和或都是按位进行运算,所以每一位都是独立的。
//在这里每一位的目的是要求每一位中得到结果是1的数字最后最好。
//所以如果某一位上1多就需要填0
//0多就需要填1
//如果0和1的数量相同则无所谓,但是题目上要求输出较小的解。所以取0。
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;

int a[100005][31];

int main() {
    IOS;
    int N, k;
    cin>>N;
    //计算一手计数1的前缀和
    for (int i=1;i<=N;i++) {
        cin>>k;
        for (int j=0;j<31;j++) {
            a[i][j] = a[i-1][j];
            if ((k>>j)&1) {
                a[i][j]++;
            }
        }
    }
    int q;
    cin>>q;
    for (int i=0;i<q;i++) {
        int l, r, ans=0;
        cin>>l>>r;
        for (int j=0;j<31;j++) {
            if ((a[r][j]-a[l-1][j])*2<(r-l+1)) {
                ans|=(1<<j);
            }
        }
        cout<<ans<<endl;
    }
    
    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务