Description
考虑维护一个这样的问题:
(1) 给出一个数组 A A A,标号为 1 1 1~ n n n
(2) 修改数组中的一个位置。
(3) 询问区间 [ l , r ] [l,r] [l,r]中所有子集的位运算 a n d and and之和 m o d ( 1 0 9 + 7 ) mod(10^9+7) mod(109+7)。
我们定义集合 S = { l , l + 1 , . . . , r − 1 , r } S=\{ l , l+1 , ... , r-1 , r\} S={l,l+1,...,r−1,r}
设 f ( T ) = A T 1 a n d A T 2 a n d . . . a n d A T k f(T)=A_{T_1}\ and\ A_{T_2}\ and\ ...\ and\ A_{T_k} f(T)=AT1 and AT2 and ... and ATk (设 k k k为 T T T集大小,若 k = 0 k=0 k=0则 f ( T ) = 0 f(T)=0 f(T)=0)
所有子集的位运算 a n d and and之和即为 ∑ T ⊂ S f ( T ) \sum\limits_{T\subset S}f(T) T⊂S∑f(T)
那么,现在问题来了。
Input
第一行,一个正整数 N N N
第二行, N N N个非负整数,为数组 A A A
第三行,一个正整数 M M M,为操作次数
接下来 M M M行格式如下
修改操作: 1 x y 1\ x\ y 1 x y,将 A x A_x Ax修改为 y y y
询问操作: 2 l r 2\ l\ r 2 l r,区间 [ l , r ] [l,r] [l,r]中所有子集的位运算 a n d and and之和 m o d ( 1 0 9 + 7 ) mod(10^9+7) mod(109+7)
( M , N ≤ 1 0 5 , A i ≤ 1 0 9 ) (M,N\le 10^5,A_i\le 10^9) (M,N≤105,Ai≤109)
Output
对于每次询问输出一行,为该次询问的答案 m o d ( 1 0 9 + 7 ) mod(10^9+7) mod(109+7)。
l o n g l o n g long\ long long long 请使用 l l d lld lld
Sample Input
3
1 2 3
6
2 1 3
1 1 2
2 1 3
2 2 3
1 2 5
2 1 3
Sample Output
9
15
7
13
Solution
按位考虑贡献,若查询区间的一个子集在第 i i i位与后为 1 1 1,那么该子集中每个元素在第 i i i位均为 1 1 1,假设该区间有 x i x_i xi个数字在第 i i i位为 1 1 1,那么第 i i i位对答案的贡献即为 2 i ⋅ ( 2 x i − 1 ) 2^i\cdot (2^{x_i}-1) 2i⋅(2xi−1),用线段树维护每个区间第 i i i位为 1 1 1的数字个数即可,时间复杂度 O ( 30 n l o g n ) O(30nlogn) O(30nlogn)
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100005;
#define mod 1000000007
int add(int x,int y)
{
x+=y;
if(x>=mod)x-=mod;
return x;
}
int mul(int x,int y)
{
ll z=1ll*x*y;
return z-z/mod*mod;
}
#define ls (t<<1)
#define rs ((t<<1)|1)
int Num[maxn<<2][30],ans[30];
void push_up(int t)
{
for(int i=0;i<30;i++)Num[t][i]=Num[ls][i]+Num[rs][i];
}
void modify(int x,int l,int r,int t,int val)
{
if(l==r)
{
for(int i=0;i<30;i++)
if((val>>i)&1)Num[t][i]=1;
else Num[t][i]=0;
return ;
}
int mid=(l+r)/2;
if(x<=mid)modify(x,l,mid,ls,val);
else modify(x,mid+1,r,rs,val);
push_up(t);
}
void query(int L,int R,int l,int r,int t)
{
if(L<=l&&r<=R)
{
for(int i=0;i<30;i++)ans[i]+=Num[t][i];
return ;
}
int mid=(l+r)/2;
if(L<=mid)query(L,R,l,mid,ls);
if(R>mid)query(L,R,mid+1,r,rs);
return ;
}
int f[maxn];
int main()
{
f[0]=0;
for(int i=1;i<=1e5;i++)f[i]=add(add(f[i-1],f[i-1]),1);
int n,q;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
modify(i,1,n,1,x);
}
scanf("%d",&q);
while(q--)
{
int op,l,r;
scanf("%d%d%d",&op,&l,&r);
if(op==1)modify(l,1,n,1,r);
else
{
memset(ans,0,sizeof(ans));
query(l,r,1,n,1);
int res=0;
for(int i=0;i<30;i++)res=add(res,mul(f[ans[i]],1<<i));
printf("%d\n",res);
}
}
return 0;
}