Nowcode Sum(Wannafly模拟赛4)
2017-10-22 22:24
375 查看
考虑维护一个这样的问题:
(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*(
b749
pw[cnt]-1)*pw[j]%mod)%mod;// pw[cnt]-1 在j位为1 的子集个数 / pw[j] j未的贡献。
}
printf("%d\n",ans);
}
}
return 0;
}
(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*(
b749
pw[cnt]-1)*pw[j]%mod)%mod;// pw[cnt]-1 在j位为1 的子集个数 / pw[j] j未的贡献。
}
printf("%d\n",ans);
}
}
return 0;
}
相关文章推荐
- Wannafly模拟赛4 C Sum(树状数组)
- Wannafly模拟赛4-C-sum(二进制拆分+树状数组)
- Wannafly模拟赛4 C Sum(线段树)
- sum nowcode
- [BZOJ4408&&BZOJ4299][FJOI2016 && Codechef]神秘数&&FRBSUM(主席树)
- Codeforces Round #277.5 (Div. 2)——C贪心—— Given Length and Sum of Digits
- 10.9wannafly模拟赛
- LintCode : Binary Tree Path Sum
- 【Leet Code】209. Minimum Size Subarray Sum---Medium
- Wannafly模拟赛4 B.Distance 最大曼哈顿距离
- Wannafly模拟赛3
- Wannafly模拟赛5 D Aria(全排列)
- BZOJ4299 Codechef FRBSUM(主席树)
- "Thinking in Code" Audio Interviews now Available
- Wannafly模拟赛 矩阵 [矩阵hash+二分]
- lintcode-medium-Continuous Subarray Sum
- lintcode:k Sum II
- Wannafly模拟赛3 题解
- LeeCode Combination Sum 更新动态规划法
- Educational Codeforces Round 37 (Rated for Div. 2)F. SUM and REPLACE