“玲珑杯”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 ...
随机推荐
- mac的终端窗口的工作组的使用
1.打开终端,打开多个tab,分别进入目录, 2.点击窗口,将窗口存储为组,弹窗如下图 可以勾选恢复所有命令,存储 3.下次使用时,点击窗口,打开工作组即可
- mysql----其他小技巧
小技巧: min/max优化 在表中,一般都是经过优化的. 如下地区表 id area pid 1 中国 0 2 北京 1 ... 3115 3113 我们查min(id), id是主键,查Min(i ...
- 关于animate的一些属性
animate() 方法执行 CSS 属性集的自定义动画.该方法通过CSS样式将元素从一个状态改变为另一个状态.CSS属性值是逐渐改变的,这样就可以创建动画效果.只有数字值可创建动画(比如 " ...
- POJ1651 Multiplication Puzzle —— DP 最优矩阵链乘 区间DP
题目链接:https://vjudge.net/problem/POJ-1651 Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65 ...
- HDU3709 Balanced Number —— 数位DP
题目链接:https://vjudge.net/problem/HDU-3709 Balanced Number Time Limit: 10000/5000 MS (Java/Others) ...
- MAC 开发 stm32 程序
资料:http://www.cnblogs.com/humaoxiao 大神技术.
- SKU多维属性状态判断算法
作者:周琪力,前端工程师,网络常用昵称「keelii」.在过去的4年里主要负责京东网站商品详情页的前端系统架构和开发,平时主要写 JavaScript 偶尔写点NodeJS,Python.琪力博客: ...
- springboot在idea实现热部署
1.在pom.xml引入热部署devtools依赖 <dependency> <groupId>org.springframework.boot</groupId> ...
- eclipse本地覆盖版本库
1,右键team,与资源库同步 2,选中冲突文件,右键“更新”,此时本地代码出现冲突 3,选中冲突文件,右键点击“标记为解决”,勾选第二项,以本地版本为准 4,冲突被解决,正常提交本地代码
- poj 2923 Relocation 解题报告
题目链接:http://poj.org/problem?id=2923 题目意思:给出两部卡车能装的最大容量,还有n件物品的分别的weight.问以最优方式装入,最少能运送的次数是多少. 二进制表示物 ...