“玲珑杯”ACM比赛 Round #5 H -- Variance 简单树状数组
可以把每个公式都化简,然后得到要维护的东西就是平方和,和前缀和,两个bit即可
不能cin,超时。IOS后都不行。
scanf用lld
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
#define MY "H:/CodeBlocks/project/CompareTwoFile/DataMy.txt", "w", stdout
#define ANS "H:/CodeBlocks/project/CompareTwoFile/DataAns.txt", "w", stdout #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e6 + ;
int n, m;
LL c_pow[maxn];
LL c[maxn];
LL a[maxn];
int lowbit(int x) {
return x & (-x);
}
void upDate_pow(int pos, LL val) {
while (pos <= n) {
c_pow[pos] += val;
pos += lowbit(pos);
}
}
void upDate(int pos, LL val) {
while (pos <= n) {
c[pos] += val;
pos += lowbit(pos);
}
}
LL query_pow(int pos) {
LL ans = ;
while (pos > ) {
ans += c_pow[pos];
pos -= lowbit(pos);
}
return ans;
}
LL query(int pos) {
LL ans = ;
// if (pos < 0) arrest
while (pos > ) {
ans += c[pos];
pos -= lowbit(pos);
}
return ans;
}
void work() {
scanf("%d%d", &n, &m);
for (int i = ; i <= n; ++i) {
scanf("%lld", &a[i]);
upDate(i, a[i]);
upDate_pow(i, a[i] * a[i]);
}
bool flag1 = false;
for (int i = ; i <= m; ++i) {
int flag;
scanf("%d", &flag);
if (flag == ) {
int x, y;
scanf("%d%d", &x, &y);
LL now = query(x) - query(x - );
upDate(x, -now);
upDate(x, y); now = query_pow(x) - query_pow(x - );
upDate_pow(x, -now);
upDate_pow(x, y * y);
} else {
int L, R;
scanf("%d%d", &L, &R);
LL t1 = (query_pow(R) - query_pow(L - )) * (R - L + );
LL haha = query(R) - query(L - );
LL t2 = - * haha * haha;
LL t3 = t2 / -;
printf("%lld\n", t1 + t2 + t3);
}
}
}
int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}
“玲珑杯”ACM比赛 Round #5 H -- Variance 简单树状数组的更多相关文章
- 【“玲珑杯”ACM比赛 Round #20 H】康娜的数学课
[链接]http://www.ifrog.cc/acm/problem/1161 [题意] 在这里写题意 [题解] 首先x<l肯定无解; 然后,肯定是要选其中的一些数字的. 而且这些数字肯定是大 ...
- “玲珑杯”ACM比赛 Round #12题解&源码
我能说我比较傻么!就只能做一道签到题,没办法,我就先写下A题的题解&源码吧,日后补上剩余题的题解&源码吧! A ...
- “玲珑杯”ACM比赛 Round #19题解&源码【A,规律,B,二分,C,牛顿迭代法,D,平衡树,E,概率dp】
A -- simple math problem Time Limit:2s Memory Limit:128MByte Submissions:1599Solved:270 SAMPLE INPUT ...
- “玲珑杯”ACM比赛 Round #19 B -- Buildings (RMQ + 二分)
“玲珑杯”ACM比赛 Round #19 Start Time:2017-07-29 14:00:00 End Time:2017-07-29 16:30:00 Refresh Time:2017-0 ...
- “玲珑杯”ACM比赛 Round #1
Start Time:2016-08-20 13:00:00 End Time:2016-08-20 18:00:00 Refresh Time:2017-11-12 19:51:52 Public ...
- “玲珑杯”ACM比赛 Round #18
“玲珑杯”ACM比赛 Round #18 Start Time:2017-07-15 12:00:00 End Time:2017-07-15 15:46:00 A -- 计算几何你瞎暴力 Time ...
- Codeforces Round #261 (Div. 2) D 树状数组应用
看着题意:[1,i]中等于a[i]的个数要大于[,jn]中等于a[j]的个数 且i<j,求有多少对这种(i,j) ,i<j可是 i前面的合法个数 要大于j后面的 看起来非常像逆序数的样子 ...
- Codeforces Round #365 (Div. 2) D 树状数组+离线处理
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
- Codeforces Gym 100114 H. Milestones 离线树状数组
H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...
随机推荐
- 关于sh,bash和dash
1 debian下shell脚本的执行过程 当sh xxx.sh,或则./xxx.sh时,默认是sh解释器来执行这个shell脚本的,但是sh是到bash的软连接,所以本质上还是bash来解析这she ...
- 微信小程序存放视频文件到阿里云用到算法js脚本文件
peterhuang007/weixinFileToaliyun: 微信小程序存放视频文件到阿里云用到算法js脚本文件 https://github.com/peterhuang007/ ...
- 创建Material Design风格的Android应用--应用主题
本人全部文章首先公布于个人博客,欢迎关注,地址:http://blog.isming.me 昨天正式公布了android 5,同一时候android developer站点也更新了,添加了创建Mate ...
- jquery 获取radio被选中的值
<html> <head> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"& ...
- I.MX6 sdio 设备注册及识别
/************************************************************************* * I.MX6 sdio 设备注册及识别 * 说明 ...
- [Selenium] 应对使用 Internet Explorer Driver 多个实例时的 cookie 共享问题
在使用 IEDriverServer 可执行文件时,从理论上来说是可通过它来创建并使用多个同时存在的 Internet Explorer Driver 实例的.但在实际使用过程中,总是会碰到与 coo ...
- [POI2007] 大都市
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1103 [算法] 树链剖分 时间复杂度 : O(NlogN ^ 2) [代码] #in ...
- ORA-01843: not a valid month 错误
转自:https://blog.csdn.net/jetluning/article/details/48785599 insert into ......的场合发生ORA-01843: not a ...
- 装饰器模式(Decorator) C++
装饰器模式是比较常用的一种设计模式,Python中就内置了对于装饰器的支持. 具体来说,装饰器模式是用来给对象增加某些特性或者对被装饰对象进行某些修改. 如上图所示,需要被装饰的对象在最上方,它自身可 ...
- emacs配置文件的基础知识 (转载)
转自:http://blog.csdn.net/schumyxp/article/details/2278268 emacs的配置文件,叫作.emacs,是个隐藏文件,存在于当前用户的根目录下面,也就 ...