“玲珑杯”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 ...
随机推荐
- MapReduce算法形式四:mapjoin
案例四:mapjoin(对个map共同输入,一个reduce) 这个方法主要解决的是,几个表之间的比较,类似于数据库的内外连接,还有一些左右连接之类的,简而言之就是,A表没有的B表有,B表有的A没有或 ...
- Java读取UTF-8格式文件第一行出现乱码——问号“?”及解决 And Java读带有BOM的UTF-8文件乱码原因及解决方法
測试样例: Java读取UTF-8的txt文件第一行出现乱码"?"及解决 test.txt文件内容: 1 00:00:06,000 --> 00:00:06,010 < ...
- 北斗有 35 颗卫星,而 GPS 有 24 颗卫星,为什么二者数量不同?
作者:知乎用户链接:https://www.zhihu.com/question/21092045/answer/17164418来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- Java数据库操作类演示
只在mysql上测试过,不知道算不算好使1. [代码][Java]代码 package org.load.demo; import java.io.IOException;import ja ...
- android之View坐标系(view获取自身坐标的方法和点击事件中坐标的获取)
在做一个view背景特效的时候被坐标的各个获取方法搞晕了,几篇抄来抄去的博客也没弄很清楚. 现在把整个总结一下. 其实只要把下面这张图看明白就没问题了. 涉及到的方法一共有下面几个: view获取自身 ...
- html5--6-13 CSS3中的颜色表示方式
html5--6-13 CSS3中的颜色表示方式 学习要点 掌握选择器的优先级问题 掌握CSS3中新增的颜色表示方式 选择器的优先级问题 原则上:元素选择器<类选择器< ID选择器< ...
- driver, module以及怎么看他们
1. driver和module的区别 https://unix.stackexchange.com/questions/47208/what-is-the-difference-between-ke ...
- SPFA 最短路 带负权边的---- 粗了解
SPFA(Shortest Path Faster Algorithm)是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 算法大致流程是用一个队列来进行维护. 初始时将源加入队列 ...
- impdp+network link 跳过expdp直接导入目标库
impdp命令特殊用途,可以将数据库的一个用户迁移到另一台机器上的数据库的用户中.如果目标用户不存在,还可以对应的创建该用户. 快速的把A库上的用户迁移到B库上. 下面就来看一下命令格式: B库下执 ...
- Quartz2D绘图 及实例:下载进度
基础绘图: C语言coregraphics框架 绘制一条线:(不常用) UIBezierPath 路径画图 1.线段 线段2: 2.三角形 填充颜色 如果边框颜色和填充颜色都为红色:[[UICol ...