时间限制:1秒 空间限制:131072K

题目描述

考虑维护一个这样的问题:
(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

对于二进制下每一位,我们单独算其在区间内的贡献,最后加起来就是查询答案。
我们对二进制下每一位(最多31位)都建一个树状数组bit[i],保存二进制下第i位为1的数字个数的前缀和。然后修改和增加这个无需赘言,修改要先把修改位置对应二进制下的1从bit中删掉再增加新的数字的bit。查询则是:若对应区间[i,j]在二进制下第k位有p个数字,那么贡献为(2p-1)*2k。每一位的贡献加起来就是答案。

 #include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define LL long long
#define mod 1000000007
using namespace std;
const int N=1e5+;
const int M=1e9+;
int num[N];
int bit[][N];
int n,m,k,u,v,op,t;
LL ans;
LL quick_pow(LL x,int n)
{
LL res=;
while(n)
{
if(n&) res=(res*x)%mod;
x=(x*x)%mod;
n>>=;
}
return res;
}
void add(int i,int x,int pos)
{
while(i<=n)
{
bit[pos][i]+=x;
i+=i&-i;
}
return ;
}
int sum(int i,int pos)
{
int s=;
while(i>)
{
s+=bit[pos][i];
i-=i&-i;
}
return s;
}
int main()
{
scanf("%d",&n);
clr(bit);
for(int i=;i<=n;i++)
{
scanf("%d",&num[i]);
k=;
t=num[i];
while(t)
{
if(t&) add(i,,k);
t>>=;
k++;
}
}
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&op,&u,&v);
if(op==)
{
k=;
t=num[u];
while(t)
{
if(t&) add(u,-,k);
t>>=;
k++;
}
num[u]=t=v;
k=;
while(t)
{
if(t&) add(u,,k);
t>>=;
k++;
}
}
if(op==)
{
ans=;
for(k=;k<=;k++)
{
ans=(ans+(quick_pow(,sum(v,k)-sum(u-,k))-)*quick_pow(,k)%mod)%mod;
}
printf("%lld\n",ans);
}
}
return ;
}

sum nowcode的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

随机推荐

  1. 边绘边理解prototype跟__proto__

    网上流传着一张讲解prototype跟__proto__关系的图,尽管他已经描绘的很清楚了,但对于初学者来说,江太公感觉还是过于纠结,于是起心重绘,让他们之间的关系更加明晰可理解,一方面出于分享目的, ...

  2. Centos 7 安装jdk1.7

    在linux中安装jdk是很平凡的事情了,刚学习linux给自己留下一笔记.刚安装centos其中可以会附带jdk,但是这并不影响,只要下载自己的jdk然后替换相对应的环境变量即可. 1.下载相对应的 ...

  3. DesignPattern

    目录

  4. 更新ubuntu15.10后触摸板点击功能消失

    问题描述: 昨天升级了ubuntu15.10,升级之后很多15.04让人不爽的东西消失了,大快人心,但是突然发现自己的触摸板不怎么好用了,原来可以点击,双指点击代表右键,三指点击代表鼠标中键的功能不见 ...

  5. selenium===selenium自动化添加日志(转)

    本文转自 selenium自动化添加日志 于logging日志的介绍,主要有两大功能,一个是控制台的输出,一个是保存到本地文件 先封装logging模块,保存到common文件夹命名为logger.p ...

  6. 微信小程序验证码获取倒计时

    wxml <button disabled='{{disabled}}' bindtap="goGetCode">{{code}}</button> js ...

  7. jquery - 实例1

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="text2.aspx.cs& ...

  8. centos7 安装 NVIDIA Docker

    安装环境: 1.centos7.3 2.NVIDIA Corporation GP106 [GeForce GTX 1060 6GB] 安装nvidia-docker a.安装docker 可参考ce ...

  9. inline-block,vertical-align:middle

    现在inline-block貌似可以替代float来实现多个item的排列分布吧 div是块级元素,如果不设置他的明确的宽度,那他就等于父元素的宽度,如果想让他其它随着子元素的变化而变化,需要改变他的 ...

  10. setTimeout(fn,0)

    我们都知道setTimeout是一个延迟执行的函数 console.log(); setTimeout(function(){console.log();},); console.log(); 会得到 ...