时间限制: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. resultAPI示例

    什么是Restfull API Restfull API 从字面就可以知道,他是rest式的接口,所以就要先了解什么是rest rest 不是一个技术,也不是一个协议 rest 指的是一组架构约束条件 ...

  2. UIImageView与UIScrollView的关系图

        UIImageView与UIScrollView的关系图           https://www.evernote.com/shard/s227/sh/0af9f23c-08e6-4be6 ...

  3. 撩下Cookie和Session

    Cookie Cookie其实还可以用在一些方便用户的场景下,设想你某次登陆过一个网站,下次登录的时候不想再次输入账号了,怎么办?这个信息可以写到Cookie里面,访问网站的时候,网站页面的脚本可以读 ...

  4. 【洛谷 P4116】 Qtree3 (树链剖分)

    题目链接 树剖练手题,想复习下树剖. 第一次提交\(T\)成QQC 看我 ??? 看了数据范围的确挺恶心的,我的复杂度是\(O(Mlog^2N)\)的,数据范围有三段 For 1/3 of the t ...

  5. HDU 1070 Milk (模拟)

    题目链接 Problem Description Ignatius drinks milk everyday, now he is in the supermarket and he wants to ...

  6. 函数getopt()及其参数optind -- (转)

    getopt被用来解析命令行选项参数 #include <unistd.h>       extern char *optarg;  //选项的参数指针       extern int ...

  7. SpringCloud Feign重试详解

    摘要: 今天在生产环境发生了数据库进程卡死的现象,除了sql因为全量更新,没加索引的原因,最主要还是我们的接口的服务器端接口出现问题了.忽视了更新接口的幂等性,以及调用方feign client的重试 ...

  8. 关于EditText.setText()无法显示的问题

    将EditText在初始化后调用EditText.setSaveEnabled(false); 让Android 系统不保存值,这样就不会恢复了.

  9. supervisor error: <class 'socket.error'>, [Errno 110]

    supervisorctr status报错 error: <class 'socket.error'>, [Errno 110] Connection timed out: file: ...

  10. MySQL5.6.32源码安装

      . 安装好--安装MySQL需要的包 yum install -y autoconf automake imake libxml2-devel expat-devel cmake gcc gcc- ...