牛客多校第十场 A Rikka with Lowbit 线段树
链接:https://www.nowcoder.com/acm/contest/148/A
来源:牛客网
题目描述
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
-->
输出
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 线段树的更多相关文章
- 2019牛客多校第八场 F题 Flowers 计算几何+线段树
2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...
- 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)
题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...
- 牛客多校第十场-D- Rikka with Prefix Sum
链接:https://www.nowcoder.com/acm/contest/148/D来源:牛客网 Prefix Sum is a useful trick in data structure p ...
- 牛客多校第十场 B Coffee Chicken 递归
题意: 给你一个“斐波那契”字符串数列,第n项由第n-1项和第n-2项拼接而成,输出某项的某位及其后10位. 题解: 递归求解即可. #include<bits/stdc++.h> usi ...
- 牛客多校第十场 E Hilbert Sort 递归,排序
题意: 给你一个方阵,再在方阵上给定一些点,按照希尔伯特曲线经过的先后顺序为这些点排序 题解: 定义好比较函数后直接调用排序算法即可. 希尔伯特曲线本来就是用于二维到一维的映射的,因此我们可以考虑对于 ...
- 牛客多校第十场 D Han Xin and His Troops 中国剩余定理
题意: 韩信有若干个兵,给定你若干个模数和余数,再给你一个1e18以内的范围限制,求解同余方程组,如果无解,输出“他一定在撒谎”,如果最小解超出范围限制,输出“他可能在撒谎”,否则输出最小解 注意:不 ...
- 牛客多校第十场 H Stammering Chemists 判断图同构
题意: 给出一个无向图,表示一种有机物质的结构式,问你这个有机物质是列表中的哪个. 题解: 判断图同构需要枚举全排列以对应点,但是此题中几乎只需要将点度数排序后一个一个比较,对于甲基位置再加个特判即可 ...
- 牛客多校第十场 F Popping Balloons 线段树维护稀疏矩阵
题意: 给定一个稀疏矩阵,里面有若干个气球,让你横着开三枪,竖着开三枪,问最多能打爆多少气球,要求相同方向,相邻两枪必须间隔r. 题解: 横向记录每列有多少个气球,分别在哪行上. 然后把这个数据改造成 ...
- 牛客多校第三场 F Planting Trees
牛客多校第三场 F Planting Trees 题意: 求矩阵内最大值减最小值大于k的最大子矩阵的面积 题解: 矩阵压缩的技巧 因为对于我们有用的信息只有这个矩阵内的最大值和最小值 所以我们可以将一 ...
随机推荐
- javaweb入门-----jsp概念
jsp是什么? JSP:Java Server Pages java服务器端页面 *可以理解为 一个特殊的页面,其中既可以直接定义html标签,又可以定义java代码 *用于简化书写 <% %& ...
- Jquery 实现添加删除,checkbok 的全选,反全选,但是批量删除没有实现
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title& ...
- Java 通过反射改变私有变量的值
直接上代码 import java.lang.reflect.Field; public class Main { public static void main(String[] args ...
- java并发编程(十八)----(线程池)java线程池框架Fork-Join
还记得我们在初始介绍线程池的时候提到了Executor框架的体系,到现在为止我们只有一个没有介绍,与ThreadPoolExecutor一样继承与AbstractExecutorService的For ...
- Linux常用命令之ftp
FTP是Internet用户使用最频繁的文件上传.下载的命令之一.linux ftp用命令的方式来控制在本机和远程ftp服务器之间传送文件.ftp中的命令包括上传文件(单个.多个),下载文件(单个.多 ...
- android ——后台下载
这次的这个demo想要实现一个后台下载文件的功能,下载的时候会有一个告知进度的通知, 使用的依赖库就一个: compile 'com.squareup.okhttp3:okhttp:3.9.0' 大体 ...
- org.apache.spark.logging类报错
一,1 在使用spark读取kafka数据时,当spark升级到2.0之后,出现如上问题:之前遇到了,当时在工程里面添加了org.apache.spark.Logging类,能够运行. 但是在后期使用 ...
- 原生JavaScript(js)手把手教你写轮播图插件(banner)
---恢复内容开始--- 1.轮播图插件 1.什么是插件: 为已有的程序增加功能 2.插件的特点(为什么要做成一个插件)与注意事项: 1.通用性,可移植性强 2.兼容性:不会对其他代码产生影响 3.创 ...
- Linux系统上安装OpenOffice
项目需求需要在linux上安装openOffice,本以为很简单,现在看来还是入了很多坑.理清楚就好了. 官网地址 http://download.openoffice.org/other.html ...
- scrapy xpath用法
一.实验环境 1.Windows7x64_SP1 2.anaconda3 + python3.7.3(anaconda集成,不需单独安装) 3.scrapy1.6.0 二.用法举例 1.开启scrap ...