牛客多校第十场 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的最大子矩阵的面积 题解: 矩阵压缩的技巧 因为对于我们有用的信息只有这个矩阵内的最大值和最小值 所以我们可以将一 ...
随机推荐
- ansible批量管理服务 下
1 ansible-playbook 任务剧本 1.1 剧本文件概念 (1)playbook可以将多个批量操作模块功能整合,完成一件事情.(2)简化运维工作复杂度(3)playbook通过yaml语法 ...
- 面向对象---prototype、__proto__、实例化对象三者之间的关系
1.构造函数 a.什么是构造函数? 解释:通过关键字new 创建的函数叫做构造函数 作用:用来创建一个对象 废话少说直接上代码,首先我们还是创建一个构造函数人类 然后我们在创建两个实例,一个凡尘 一个 ...
- 二叉查找树(查找、插入、删除)——C语言
二叉查找树 二叉查找树(BST:Binary Search Tree)是一种特殊的二叉树,它改善了二叉树节点查找的效率.二叉查找树有以下性质: (1)若左子树不空,则左子树上所有节点的值均小于它的根节 ...
- 洛谷P1510 题解
前言: 其实这道题挺水的,但我居然把ta想成了 贪心 啪啪打脸 好了,废话不多说. 思路: step 1:先翻译以下题意,其实就是求出最多消耗多少体力能把东海填满,如果不能填满,就输出"Im ...
- 解决:Navicat连接不上MySQL 8.0
转载自 https://www.cnblogs.com/shiysin/p/shiysin.html Navicat连接不上,总是报错1251: 原因是MySQL8.0版本的加密方式和MySQL5.0 ...
- Java 8原生API也可以开发响应式代码?
前段时间工作上比较忙,这篇文章一直没来得及写,本文是阅读<Java8实战>的时候,了解到Java 8里已经提供了一个异步非阻塞的接口(CompletableFuture),可以实现简单的响 ...
- [实践]activemq安全设置 设置admin的用户名和密码
(1)打开/opt/app/amq/apache-activemq-5.9.0/conf/jetty.xml 找到 将property name为authenticate的属性value=" ...
- C++学习想法
今天是周一,今天做早操的时候舍友说准备买一本C++基础的书.我觉得这样的想法很好,突然想到自己最近几天因为自己私人原因事情很忙,蛋这不能成为我不学C++的理由.所以我在这规划了我这一周的学习进程.首先 ...
- XAMPP/LAMPP到底在哪里启用APACHE2的rewrite
XAMPP/LAMPP是一套我们在个人建站过程中非常便捷常用的集成环境.特别是对于学习PHP开发和建站非常便捷. 最近在使用CentOS7环境下的XAMPP过程中,遇到了一个问题,也就是apache2 ...
- postman-使用教程
postman postman是一款非常方便的API测试工具,可以帮我们快速的发起HTTP请求,下面记录一下postman的基本使用. postman安装 postman下载地址 下载安装打开之后就是 ...