Nowcode Sum(Wannafly模拟赛4)

考虑维护一个这样的问题:
(1) 给出一个数组A,标号为1~n
(2) 修改数组中的一个位置。
(3) 询问区间[l,r]中所有子集的位运算and之和mod(109+7)。
位运算and即为“pascal中的and”和“C/C++中的&”
我们定义集合S={ l , l+1 , ... , r-1 , r}
若集合T,T ∩ S = T,则称T为S的子集
设f(T)=AT1 and AT2 and ... and ATk (设k为T集大小,若k=0则f(T)=0)
所有子集的位运算and之和即为∑f(T)
那么,现在问题来了。


输入描述:
第一行,一个正整数N
第二行,N个非负整数,为数组A
第三行,一个正整数M,为操作次数
接下来M行格式如下
修改操作: 1 x y,将Ax修改为y
询问操作: 2 l r,区间[l,r]中所有子集的位运算and之和 mod(109+7)

输出描述:
对于每次询问输出一行,为该次询问的答案mod(109+7)。
long long 请使用lld



示例1



输入

3
1 2 3
6
2 1 3
1 1 2
2 1 3
2 2 3
1 2 5
2 1 3


输出

9
15
7

13


打完比赛看大佬的代码没看懂,今天看大佬的博客:http://blog.csdn.net/qq_34287501/article/details/78304322

才理解了大佬们神奇的求的贡献方法。 确实  无论子集and值为多少,如果k位上为1的话,此时的贡献就为 1<<k,比如 100100 最后对答案的贡献不就是 100000 + 100 .。

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
const int maxn = 100000;
const int mod = (1e9+7);
typedef long long ll;
using namespace std;
int n,m,a[maxn+5],C[32][maxn+5];
int pw[maxn+5];
int lowbit(int x)
{
    return x&-x;
}
int sum(int k,int x)
{
    int ret = 0;
    while(x>0) ret+=C[k][x], x-= lowbit(x);
    return ret;
}
void add(int k,int x,int d)
{
    while(x<=n)
    {
        C[k][x]+=d,x+=lowbit(x);
    }
}
int main()
{
    scanf("%d",&n);
    pw[0] = 1;
    for(int i=1; i<=n; i++) pw[i] = 1ll*2*pw[i-1]%mod;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        for(int j=0; j<=30; j++) if(a[i]&(1<<j)) add(j,i,1);
    }
    scanf("%d",&m);
    while(m--)
    {
        int op,x,y;
        scanf("%d %d %d",&op,&x,&y);
        if(op==1)
        {
            for(int i=0; i<=30; i++) if(a[x]&(1<<i)) add(i,x,-1);
            a[x] = y;
            for(int i=0; i<=30; i++) if(a[x]&(1<<i)) add(i,x,1);
        }
        else
        {
            int ans = 0;
            for(int j=0; j<=30; j++)
            {
                int cnt = sum(j,y) - sum(j,x-1);// cnt :ax~ay 中在j位有为1有多少个
                ans = (ans+ 1ll*(pw[cnt]-1)*pw[j]%mod)%mod;// pw[cnt]-1 在j位为1 的子集个数 / pw[j] j未的贡献。
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值