链接:https://ac.nowcoder.com/acm/contest/148/A
来源:牛客网

Rikka with Lowbit
时间限制:C/C++ 5秒,其他语言10秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
Today, Rikka is going to learn how to use BIT to solve some simple data structure tasks. While studying, She finds there is a magic expression x & (-x)x&(−x) in the template of BIT. After searching for some literature, Rikka realizes it is the implementation of the function \text{lowbit(x)}lowbit(x).

\text{lowbit}(x)lowbit(x) is defined on all positive integers. Let a1...am be the binary representation of x while a1 is the least significant digit, k be the smallest index which satisfies ak = 1. The value of \text{lowbit}(x)lowbit(x) is equal to 2k-1.

After getting some interesting properties of \text{lowbit}(x)lowbit(x), Rikka sets a simple data structure task for you:

At first, Rikka defines an operator f(x), it takes a non-negative integer x. If x is equal to 0, it will return 0. Otherwise it will return x-\text{lowbit}(x)x−lowbit(x) or x+\text{lowbit}(x)x+lowbit(x), each with the probability of \frac{1}{2}
2
1

.

Then, Rikka shows a positive integer array A of length n, and she makes m operations on it.

There are two types of operations:

  1. 1 L R, for each index i ∈ [L,R], change Ai to f(Ai).
  2. 2 L R, query for the expectation value of \sum_{i=L}^R A_i∑
    i=L
    R

    A
    i

    . (You may assume that each time Rikka calls f, the random variable used by f is independent with others.)
    输入描述:
    The first line contains a single integer t(1 ≤ t ≤ 3), the number of the testcases.

The first line of each testcase contains two integers n,m(1 ≤ n,m ≤ 105). The second line contains n integers Ai(1 ≤ Ai ≤ 108).

And then m lines follow, each line contains three integers t,L,R(t ∈ {1,2}, 1 ≤ L ≤ R ≤ n).
输出描述:
For each query, let w be the expectation value of the interval sum, you need to output (w \times 2^{nm}) \mod 998244353(w×2
nm
)mod998244353.

It is easy to find that w x 2nm must be an integer.
示例1
输入
复制
1
3 6
1 2 3
1 3 3
2 1 3
1 3 3
2 1 3
1 1 3
2 1 3
输出
复制
1572864
1572864
1572864

题意:

思路:

分析会发现操作1对数字的期望根本就不会改变,于是就是一个裸的区间查询(用前缀和都可以做)(记得取模)。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=100010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/ll tree[maxn];ll a[maxn];int n,m;
int lowbit(int x)
{return x&(-x);
}
const ll mod=998244353ll;
void add(int id,ll x)
{while(id<=n){tree[id]=(tree[id]+x)%mod;id+=lowbit(id);}
}
ll ask(int id)
{ll res=0ll;while(id){res=(res+tree[id])%mod;id-=lowbit(id);}return res;
}
int main()
{//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);gbtb;int t;cin>>t;while(t--){MS0(tree);cin>>n>>m;repd(i,1,n){cin>>a[i];add(i,a[i]);}ll ans;int op,l,r;repd(i,1,m){cin>>op>>l>>r;if(op==1){continue;}ans=(ask(r)-ask(l-1)+mod)%mod;ans=(ans*powmod(2ll,1ll*n*m,mod))%mod;cout<<ans<<endl;}}return 0;
}inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}}
}

转载于:https://www.cnblogs.com/qieqiemin/p/11261667.html

2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)相关推荐

  1. 牛客网暑期ACM多校训练营(第二场)J farm (二维树状数组)

    题目链接: https://www.nowcoder.com/acm/contest/140/J 思路: 都写在代码注释里了,非常好懂.. for_each函数可以去看一下,遍历起vector数组比较 ...

  2. 牛客网暑期ACM多校训练营(第九场)

    牛客网暑期ACM多校训练营(第九场) A. Circulant Matrix 做法:看到下标 \(xor\) 这种情况就想 \(FWT\),可是半天没思路,于是放弃了..其实这个 \(n\) 疯狂暗示 ...

  3. 牛客网暑期ACM多校训练营(第一场)

    牛客网暑期ACM多校训练营(第一场) A. Monotonic Matrix 考虑0和1的分界线,1和2的分界线,发现问题可以转化为两条不互相穿过的路径的方案数(可重叠),题解的做法就是把一条路径斜着 ...

  4. 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)

    链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...

  5. 牛客网暑期ACM多校训练营(第二场): H. travel(树形线头DP)

    链接:https://ac.nowcoder.com/acm/contest/140/H 来源:牛客网 题目描述 White Cloud has a tree with n nodes.The roo ...

  6. 牛客网暑期ACM多校训练营(第三场): E. Sort String(KMP)

    链接:https://www.nowcoder.com/acm/contest/141/E 来源:牛客网 题目描述 Eddy likes to play with string which is a ...

  7. 牛客网暑期ACM多校训练营(第三场): C. Shuffle Cards(splay)

    链接:https://www.nowcoder.com/acm/contest/141/C 来源:牛客网 题目描述 Eddy likes to play cards game since there ...

  8. 牛客网暑期ACM多校训练营(第二场)A .run

    链接:https://www.nowcoder.com/acm/contest/140/A 来源:牛客网 题目描述 White Cloud is exercising in the playgroun ...

  9. 2018牛客网暑期ACM多校训练营第二场 D - money(贪心)

    题目链接 https://www.nowcoder.com/acm/contest/140#question [题目描述] White Cloud is exercising in the playg ...

最新文章

  1. creat_caltab生成六边形标定板(黑底白点)
  2. leetcode算法题--二叉树中和为某一值的路径
  3. 如何设计一门语言(八)——异步编程和CPS变换
  4. linux算法平台,Linux实时调度算法与测试平台的研究与实现
  5. 金融数据分析与挖掘实战1.5.2-1.5.3
  6. AI学习笔记(十八)NLP常见场景之情感分析
  7. java使用kaptcha生成图片验证码
  8. uniapp Android离线打包Activity class {com.xxx.yyy/io.dcloud.PandoraEntry} does not exist.
  9. POJ 2502 Subway dij
  10. 山大网络计算机基础知识模拟,山大网络教育计算机系统结构模拟试卷1
  11. Luogu5788 【模板】单调栈
  12. django book学习笔记
  13. Java方法 (含计算器代码)
  14. BUUCTF:FLAG
  15. keras交通信号识别(分类)
  16. USB电路EMC设计标准电路详解
  17. 你即将拥有HDMI2.1,纯光纤HDMI最高支持72Gbps试用体验
  18. c语言打印数组中的汉字
  19. 125 · 背包问题(二)Backpack II
  20. 关于在校专利软著申请一二事

热门文章

  1. How to resolve warning message Access restriction -The type Resource is not accessible
  2. AOP原理学习之How is JdkDynamicAopProxy generated
  3. 如何使用Chrome开发者工具调试web socket应用
  4. SAP CRM产品主数据无法根据产品描述字段进行搜索的原因
  5. redis 队列_Redis系列5实现简单消息队列
  6. 单位内部一个计算机系统属于,2012年计算机一级MsOffice第五十九套练习题及答案解析...
  7. 德 梅齐里亚克的砝码问题matlab,德梅齐里亚克砝码问题之解
  8. c语言赫夫曼树的编码与译码,哈夫曼树与编码译码实现
  9. ROS2 on android,ROS2 通过Debian安装ROS2
  10. 高压电是以交流,还是直流方式输送?