HZAU 异或问题

1104: Sum and XOR

Time Limit: 5 Sec   Memory Limit: 128 MB
Submit: 994   Solved: 56
[ Submit][ Status][ Web Board]

Description

     Dr. Zeng is good at calculating sum of some numbers . Today , Yifan want to give a difficult work to Dr.Zeng .So,he told Dr.Zeng that he will get N numbers a1…an, and M options .When the option is 1 , Dr.Zeng should calculate the sum of the numbers . And the when the option is 2 , he will give Dr.Zeng a number D , then , change every ai into (ai xor d).

    Dr.Zeng is too lazy to calculate it ,so could you please help him ?

Input

    First line contains two integers N,M(0<N<5000000 , 0<M<100000)
    The second line contains N integer a1…an .
    Then the following M line . For each line , first a number op , if op == 2 ,there will be a number D.
    All the number is less then 2^25 .

Output

For each option 1 ,output a number ,the sum of a1…an.

Sample Input

2 3
1 2
1
2 5
1

Sample Output

3
11

HINT

Source

题意:告诉你n个数,现在做m个操作,包括以下两个操作:

1、求n个数的和

2、给一个数d,用d去异或n个数

一开始想用线段树来做。。真是智障啊。。。因为是对点的更新,线段树还是太费时了。

异或是对位进行操作的。。。所以遇到异或问题的时候一定要想想分解到位的时候是否能搞。。。

把n个数分解成二进制,然后统计每一位上有几个1(自己写的时候以为零的个数也要统计,后来发现是不用的,改变操作只关心1的个数),求总和的时候因为都知道每个位上有多少个1,所以总和很好算。

当给了d要求用其异或每个数的时候,也把d换成二进制,d的二进制表示上,仅当该位是1时,要将我们n个数统计出来的每一位1的个数和零的个数交换。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 5000010;
int maxLen;
long long two[30];
void init(){
    int i;
    long long ans = 1;
    for(i=0;i<=30;i++){
        two[i] = ans;
        ans *= 2;
    }
}
long long a[maxn];
int in[30],one[30];
void change(long long a,int index){
    int p = -1;
    if(index==-1){
        while (a!=0) {
            in[++p] = a%2;
            a /= 2;
        }
        return;
    }
    while(a!=0){
        ++p;
        if(a%2){
            one[p]++;
        }
        a /= 2;
    }
}
int n,m;
void myXOR(int len){
    int i;
    for(i=0;i<=len;i++){
        if(in[i]==1){
            one[i] = n-one[i];
        }
    }
}
long long getSum(){
    long long ans = 0;
    int i;
    for(i=0;i<=maxLen;i++){
        ans += one[i]*two[i];
    }
    return ans;
}
int main(){
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int i,j;
    init();
    while (~scanf("%d%d",&n,&m)) {
        long long now = 0;
        long long sum = 0;
        for(i=1;i<=n;i++){
            scanf("%lld",&a[i]);
            sum += a[i];
            now = max(a[i],now);
        }
        for(i=0;i<=30;i++){
            if(two[i]>=now){
                maxLen = i;
                break;
            }
        }
        memset(one, 0, sizeof(one));
        for(i=1;i<=n;i++){
            change(a[i], i);
        }
        int op,nowLen;
        long long z;
        for(i=1;i<=m;i++){
            scanf("%d",&op);
            if(op==1){
                printf("%lld\n",getSum());
            }else{
                scanf("%lld",&z);
                memset(in, 0, sizeof(in));
                change(z, -1);
                for(j=0;j<=30;j++){
                    if(two[j]>=z){
                        nowLen = j;
                        break;
                    }
                }
                maxLen = max(maxLen,nowLen);
                myXOR(nowLen);
            }
        }
    }
    return 0;
}




先转载存着

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值