17年sdnu选拔赛第2场----树状数组+方差公式
来源:互联网 发布:yy老虎机软件 编辑:程序博客网 时间:2024/04/28 18:59
Description
An array with length n is given.
You should support 2 types of operations.
1.x y change the x-th element to y.
2.l r print the variance of the elements with indices l, l + 1, ... , r.
As the result may not be an integer, you need print the result after multiplying (r-l+1)^2.
The index of the array is from 1.
Input
The first line is two integer n, m(1 <= n, m <= 2^{16}), indicating the length of the array and the number of operations. The second line is n integers, indicating the array. The elements of the array 0<=ai <=10^4. The following m lines are the operations. It must be either 1 x y or 2 l r
Output
For each query, print the result.
Sample Input
4 4
1 2 3 4
2 1 4
1 2 4
2 1 4
2 2 4
Sample Output
20
24
2
主要考察方差的使用,方差
维护x和x^2树状数组即可
#include<iostream>#include <cstdio>#include <cstring>#define ll long longusing namespace std;ll c[65550];ll c2[65550];ll a[65550];int lowbit(int x){ return x&(-x);}void add(int x,int val){ while(x<=65550) { c[x]+=val; x+=lowbit(x); }}void add2(int x,ll val){ while(x<=65550) { c2[x]+=val; x+=lowbit(x); }}ll sum(int x){ ll ans=0; while(x>0) { ans+=c[x]; x-=lowbit(x); } return ans;}ll sum2(int x){ ll ans=0; while(x>0) { ans+=c2[x]; x-=lowbit(x); } return ans;}int main(){ int n,m; cin>>n>>m; memset(c,0,sizeof(c)); memset(c2,0,sizeof(c2)); for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); add(i,a[i]); add2(i,a[i]*a[i]); } while(m--) { int x; scanf("%d",&x); if(x==1) { int u; ll v; scanf("%d%lld",&u,&v); add(u,v-a[u]); add2(u,v*v-a[u]*a[u]); a[u]=v; } else { int l,r; scanf("%d%d",&l,&r); ll E2=(sum2(r)-sum2(l-1))*(r-l+1); ll E=sum(r)-sum(l-1); ll D=E2-E*E; printf("%lld\n",D);//在这里用cout在玲珑杯上超时,找了好久的原因 } } return 0;}
0 0
- 17年sdnu选拔赛第2场----树状数组+方差公式
- 【洛谷live】【线段树】【树状数组】【方差公式】【逆元】区间方差 题解
- sdnu第一场选拔赛--Miller_Rabin算法判断是否为素数
- 17暑期ACM俱乐部个人训练赛第2场 G Balanced Photo (树状数组)
- 方差公式
- 2017暑期ACM俱乐部个人训练赛第2场 G题Balanced Photo(树状数组求逆序对)
- 2017年集训队第四场选拔赛Trainsorting
- 【codevs 2342】[2007年省队选拔赛上海市队选拔赛] 园丁的烦恼(离散化+树状数组)
- jzoj 3873. 【NOIP2014八校联考第4场第2试10.20】乐曲创作(music) 构造题+树状数组+线段树
- 2014多校联合第9场1011题||hdu 4970 树状数组
- 09年3月选拔赛第2题
- 第二场选拔赛
- 第三场选拔赛
- 第四场选拔赛
- 树状数组 公式推导 poj 1990 MooFest
- 湖南中医药大学2017年集训队第二场选拔赛
- 2017年集训队第四场选拔赛 -string Game
- 湖南中医药大学2017年集训队第五场选拔赛
- 如何上传本地文件到Github
- 校园导航系统
- Spring boot 双主键注解配置
- 数据库基础(1)关系型数据库
- ListView无法执行getView()的几种可能问题
- 17年sdnu选拔赛第2场----树状数组+方差公式
- java获取一份文件的MD5值
- 集合的整数表示
- Android利用setLayoutParams在代码中调整布局(Margin和居中)
- 前端-JavaScript(二)
- 二叉树的后序遍历
- 扑克牌中的顺子问题
- 编译原理学习笔记(四)预测分析器(2)
- 在Windows平台上搭建Docker开发环境