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

题目描述

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 in the template of BIT. After searching for some literature, Rikka realizes it is the implementation of the function .

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 is equal to 2k-1.

After getting some interesting properties of , 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 or , each with the probability of .

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 . (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 ≤ 10

5

). The second line contains n integers A

i

(1 ≤ A

i

 ≤ 10

8

). 

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 

. 

It is easy to find that w x 2

nm

 must be an integer.

输入例子:
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

输入

复制

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 分析:每次变化+-lowbit得到的期望是原结果,所以我们直接求一下区间和就可以
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
const ll mod = 998244353; ll qp(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll array1[maxn]; struct node1
{
ll sum,addmark;
} Node[maxn<<2];
void pushup(int node)
{
Node[node].sum=Node[node<<1].sum+Node[node<<1|1].sum;
}
void buildtree(int node,int left,int right)
{
Node[node].addmark=0;
if(left==right)
{
Node[node].sum=array1[left];
return;
}
int m=(left+right)>>1;
buildtree(node<<1,left,m);
buildtree(node<<1|1,m+1,right);
pushup(node);
}
void pushdown(int node,int left,int right)
{
if(Node[node].addmark)
{
int m=(left+right)>>1;
Node[node<<1].addmark+=Node[node].addmark;
Node[node<<1|1].addmark+=Node[node].addmark;
Node[node<<1].sum+=(m-left+1)*Node[node].addmark;
Node[node<<1|1].sum+=(right-m)*Node[node].addmark;
Node[node].addmark=0;
}
}
ll query(int node,int begin,int end,int left,int right)
{
if(left<=begin&&right>=end)
return Node[node].sum;
pushdown(node,begin,end);
int m=(begin+end)>>1;//小心!!
ll ans=0;
if(left<=m)
ans+=query(node<<1,begin,m,left,right);
if(right>m)
ans+=query(node<<1|1,m+1,end,left,right);
return ans;
}
void update(int node,int add,int begin,int end,int left,int right)
{
if(begin>=left&&end<=right)
{
Node[node].sum+=(end-begin+1)*add;
Node[node].addmark+=add;
return;
}
int m=(begin+end)>>1;//小心!!
pushdown(node,begin,end);
if(left<=m)
update(node<<1,add,begin,m,left,right);
if(right>m)
update(node<<1|1,add,m+1,end,left,right);
pushup(node);
} ll n,m; int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int i,j,k,t;
cin>>t;
while(t--)
{
cin>>n>>m;
for(i=1; i<=n; i++) cin>>array1[i];
buildtree(1,1,n);
for(i=1; i<=m; i++)
{
int a,b,c;
cin>>a>>b>>c;
if(a==2) cout<<query(1,1,n,b,c)%mod*qp(2,n*m)%mod<<endl;
}
}
return 0;
}

  

牛客多校第十场 A Rikka with Lowbit 线段树的更多相关文章

  1. 2019牛客多校第八场 F题 Flowers 计算几何+线段树

    2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...

  2. 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)

    题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...

  3. 牛客多校第十场-D- Rikka with Prefix Sum

    链接:https://www.nowcoder.com/acm/contest/148/D来源:牛客网 Prefix Sum is a useful trick in data structure p ...

  4. 牛客多校第十场 B Coffee Chicken 递归

    题意: 给你一个“斐波那契”字符串数列,第n项由第n-1项和第n-2项拼接而成,输出某项的某位及其后10位. 题解: 递归求解即可. #include<bits/stdc++.h> usi ...

  5. 牛客多校第十场 E Hilbert Sort 递归,排序

    题意: 给你一个方阵,再在方阵上给定一些点,按照希尔伯特曲线经过的先后顺序为这些点排序 题解: 定义好比较函数后直接调用排序算法即可. 希尔伯特曲线本来就是用于二维到一维的映射的,因此我们可以考虑对于 ...

  6. 牛客多校第十场 D Han Xin and His Troops 中国剩余定理

    题意: 韩信有若干个兵,给定你若干个模数和余数,再给你一个1e18以内的范围限制,求解同余方程组,如果无解,输出“他一定在撒谎”,如果最小解超出范围限制,输出“他可能在撒谎”,否则输出最小解 注意:不 ...

  7. 牛客多校第十场 H Stammering Chemists 判断图同构

    题意: 给出一个无向图,表示一种有机物质的结构式,问你这个有机物质是列表中的哪个. 题解: 判断图同构需要枚举全排列以对应点,但是此题中几乎只需要将点度数排序后一个一个比较,对于甲基位置再加个特判即可 ...

  8. 牛客多校第十场 F Popping Balloons 线段树维护稀疏矩阵

    题意: 给定一个稀疏矩阵,里面有若干个气球,让你横着开三枪,竖着开三枪,问最多能打爆多少气球,要求相同方向,相邻两枪必须间隔r. 题解: 横向记录每列有多少个气球,分别在哪行上. 然后把这个数据改造成 ...

  9. 牛客多校第三场 F Planting Trees

    牛客多校第三场 F Planting Trees 题意: 求矩阵内最大值减最小值大于k的最大子矩阵的面积 题解: 矩阵压缩的技巧 因为对于我们有用的信息只有这个矩阵内的最大值和最小值 所以我们可以将一 ...

随机推荐

  1. 【iOS】UITableViewDelegate 方法没有调用

    可能原因:没有调用 reloadData 方法. [self.tableView reloadData];

  2. 对ThreadLocal的一些理解

    ThreadLocal也是在面试过程中经常被问到的,本文主要从以下三个方面来谈对ThreadLocal的一些理解: ThreadLocal用在什么地方 ThreadLocal一些细节 ThreadLo ...

  3. poj 3253 Fence Repair(优先队列+huffman树)

    一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...

  4. UE4 游戏模块初始化顺序

    最近看教学,有个讲解UE4初始化顺序的,记录一下. 首先创建一个Actor,Character,GameInstance,GameMode,LevelScriptActor(关卡),PlayerCon ...

  5. 【POJ - 3258】River Hopscotch(二分)

    River Hopscotch 直接中文 Descriptions 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一块岩石跳到另一块岩石.这项激动人心的活动在一条长长的笔直河道中进行,在起点 ...

  6. java8-流的操作

    流的操作 流的使用一般包括三件事: 一个数据源来执行一个查询; 一个中间操作链,形成一条流的流水线; 一个终端操作,执行流水线,并能生成结果 中间操作 操作 类型 返回类型 操作参数 函数描述符 fi ...

  7. eclipse的下载安装配置

    1.在eclipse官网下载与你电脑版本相对应的安装包.链接:https://www.eclipse.org/downloads/eclipse-packages/ 2.下载与eclipse版本相对应 ...

  8. centos yum 安装 mariadb

    1. 在 /etc/yum.repos.d/ 下建立 MariaDB.repo,输入内容 [mariadb] name=MariaDB baseurl=http://yum.mariadb.org/1 ...

  9. 【Java例题】7.4 文件题1-学生成绩排序

    4.学生成绩排序.已有一个学生成绩文件,含有多位学生的成绩:读取这个文件中的每位学生的成绩,然后排序:最后将这些排好序的成绩写到另一个文件中. package chapter7; import jav ...

  10. Spark 系列(七)—— 基于 ZooKeeper 搭建 Spark 高可用集群

    一.集群规划 这里搭建一个 3 节点的 Spark 集群,其中三台主机上均部署 Worker 服务.同时为了保证高可用,除了在 hadoop001 上部署主 Master 服务外,还在 hadoop0 ...