时间限制: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. 【BZOJ】1726 [Usaco2006 Nov]Roadblocks第二短路

    [算法]最短路(spfa) 次短路 [题解] 正反跑两次SPFA,然后枚举每一条边,如果起点到一个端点的最短路+另一个端点到终点的最短路+长度 ≠ 最短路,则和答案比较,保存最小值. #include ...

  2. Java 扑克牌发牌

    今天看到这个算法题,http://www.cnblogs.com/xishuai/p/3392981.html ,忍不住自己用Java做了一个. 初始化很重要,所有的52张牌按顺序放入到容器里边,标志 ...

  3. 深入理解 JavaScript(四)

    前言 Bob 大叔提出并发扬了 S.O.L.I.D 五大原则,用来更好地进行面向对象编程,五大原则分别是: The Single Responsibility Principle(单一职责 SRP) ...

  4. vue装逼神器简述

    主要是分享下用vuejs开发项目过程中遇到的问题,vuejs开发的优势和需要注意的地方. 项目主要页面:主页,最新,分类,分类列表,详情页,结果页,斗图(列表,制作页) 效果图: 地址:https:/ ...

  5. hdu 1395 2^x mod n = 1(暴力题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1395 2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Oth ...

  6. Call Mode feature

    起源 user 在插著 充電器 打電話的狀況下, 為了安全起見, 避免 充電器在這時損害手機,間接造成 user 的傷害, 而有了這 feature, 在 battery voltage Vbat & ...

  7. SqlServer存储过程中使用事务,示例

    create proc pro_GetProTrans @GoodsId int, @Number int, @StockPrice money, @SupplierId int, @EmpId in ...

  8. 如何在datepicker滚动完毕后触发事件去获得日期

    本来以为这件事情应该需要借助datepicker的委托来处理的,但是并没有找到此空间的委托. 其实最最简单的做法就是在IB中将次控件connect到一个Action上. 经过测试,当datepicke ...

  9. 关于 拼接 url 连接 参数的问题(爬虫)。

    比如这里 我找的 后台请求的json的链接: 第一页: http://www.igoldenbeta.com:8080/cn-jsfund-server-mobile/bkt/api?appkey=1 ...

  10. ReentrantLock 分析

    带着疑问去分析 ReentrantLock是如何实现锁管理的. ReentrantLock是如何实现重入的. ReentrantLock是如何实现公平锁与非公平锁. ReentantLock的公平锁为 ...