Preprefix sum BZOJ 3155 树状数组
题目描述
前缀和(prefix sum)Si=∑k=1iaiS_i=\sum_{k=1}^i a_iSi=∑k=1iai。
前前缀和(preprefix sum) 则把SiS_iSi作为原序列再进行前缀和。记再次求得前缀和第i个是SSiSS_iSSi
给一个长度n的序列a1,a2,⋯,ana_1, a_2, \cdots, a_na1,a2,⋯,an,有两种操作:
Modify i x:把aia_iai改成xxx;Query i:查询SSiSS_iSSi
输入输出格式
输入格式:
第一行给出两个整数N,M。分别表示序列长度和操作个数
接下来一行有N个数,即给定的序列a1,a2,....an
接下来M行,每行对应一个操作,格式见题目描述
输出格式:
对于每个询问操作,输出一行,表示所询问的SSi的值。
输入输出样例
说明
1<=N,M<=100000,且在任意时刻0<=Ai<=100000
维护两个数组:a[i], (n-i+1)a[i];
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-11
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll a[maxn];
ll b[2][maxn]; int n, m; void add(int x, ll val, int dx) {
while (x <= n) {
b[dx][x] += val; x += x & -x;
}
}
void upd(int pos, ll val) {
add(pos, -a[pos], 0); add(pos, -a[pos] * (n - pos + 1), 1);
a[pos] = val;
add(pos, a[pos], 0); add(pos, a[pos] * (n - pos + 1), 1);
}
ll query(int x) {
ll ans = 0;
int tmp = x;
while (tmp > 0) {
ans += b[1][tmp]; tmp -= tmp & -tmp;
}
tmp = x; ll res = 0;
while (tmp > 0) {
res += b[0][tmp]; tmp -= tmp & -tmp;
}
return ans - (n - x)*res;
} int main() {
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
n = rd(); m = rd();
for (int i = 1; i <= n; i++) {
rdllt(a[i]);
add(i, a[i], 0); add(i, (n - i + 1)*a[i], 1);
}
while (m--) {
char ch[10];
rdstr(ch);
if (ch[0] == 'M') {
int pos; ll val;
rdint(pos); rdllt(val);
upd(pos, val);
}
else {
int pos; pos = rd();
printf("%lld\n", query(pos));
}
}
return 0;
}
Preprefix sum BZOJ 3155 树状数组的更多相关文章
- bzoj 2743 树状数组离线查询
我们按照询问的右端点排序,然后对于每一个位置,记录同颜色 上一个出现的位置,每次将上上位置出现的+1,上次出现的-1,然后 用树状数组维护就好了 /************************** ...
- Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- Codeforces 703D Mishka and Interesting sum(离线 + 树状数组)
题目链接 Mishka and Interesting sum 题意 给定一个数列和$q$个询问,每次询问区间$[l, r]$中出现次数为偶数的所有数的异或和. 设区间$[l, r]$的异或和为$ ...
- leetcode 307. Range Sum Query - Mutable(树状数组)
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- HihoCoder1336 Matrix Sum(二维树状数组求和)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given an N × N matrix. At the beginning every element ...
- BZOJ 2743 树状数组
不能用分块. #include <bits/stdc++.h> using namespace std; ; struct Info{int l,r,Id;}Q[Maxn]; int a[ ...
- Gym 100960G (set+树状数组)
Problem Youngling Tournament 题目大意 给一个序列a[i],每次操作可以更改一个数,每次询问 将序列排序后有多少个数a[i]>=sum[i-1]. n<=10^ ...
- [洛谷P1198/BZOJ1012][JSOI2008] 最大数 - 树状数组/线段树?
其实已经学了树状数组和线段树,然而懒得做题,所以至今没写多少博客 Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数 ...
- poj3321 dfs序+树状数组单点更新 好题!
当初听郭炜老师讲时不是很懂,几个月内每次复习树状数组必看的题 树的dfs序映射在树状数组上进行单点修改,区间查询. /* 树状数组: lowbit[i] = i&-i C[i] = a[i-l ...
随机推荐
- Redis搭建(三):哨兵模式
一.sentinel介绍 Redis 2.8中提供了“哨兵”工具来实现自动化的系统监控和故障恢复功能. Redis 2.6 版也提供了哨兵工具,但此时的哨兵是1.0版,存在非常多的问题,任何情况下都不 ...
- 算法技巧讲解》关于对于递推形DP的前缀和优化
这是在2016在长沙集训的第三天,一位学长讲解了“前缀和优化”这一技巧,并且他这一方法用的很6,个人觉得很有学习的必要. 这一技巧能使线性递推形DP的速度有着飞跃性的提升,从O(N2)优化到O(N)也 ...
- java简单的测试方法执行了多少时间
(1)以毫秒为单位的 long startTime = System.currentTimeMillis(); // 获取开始时间 // doThing(); // 测试的代码段 long endTi ...
- 我的笔记,有关 PhotoShop,给自己的记忆宫殿
一直有心学习 PhotoShop ,各种教程也 download 了不少,什么祁连山.PS大师之路.Oeasy 等等.看了吗?丫蛋的只看了前面两集!还是在博客上写写坐下笔记,好记性不如烂笔头. 0.先 ...
- mysql 索引 笔记1
#不同的存储引擎支持的索引类型也不一样 InnoDB 支持事务,支持行级别锁定,支持 B-tree.Full-text 等索引,不支持 Hash 索引: MyISAM 不支持事务,支持表级别锁定,支持 ...
- java 解析xml(dom4j.jar)
先导入jar包 <?xml version="1.0" encoding="UTF-8"?> <companys> <compan ...
- numpy ndarray 返回 index 问题
经常遇到需要返回满足条件的index. python中没有which函数,但有列表推导式, 可以实现类似功能 y= np.array([3,2,5,20]) yOut[131]: array([ 3, ...
- Luogu 3594 [POI2015]WIL-Wilcze doły
简单题. 考虑没有修改数字的条件的限制,我们直接用双指针扫描就可以计算出答案了. 然后考虑加入修改数字的条件,只要用单调队列维护出当前两个指针表示的区间中长度为$d$的一段区间的最大值,用总和减掉这个 ...
- vue.js的生命周期 及其created和mounted的部分
网上很多人有所总结,转载自: https://segmentfault.com/a/1190000008570622 关于created和mounted的部分,也可以参考: https://blo ...
- Mybatis——Spring整合
一.引入依赖 Spring的相关jar包 mybatis-3.4.1.jar mybatis-spring-1.3.0.jar mysql-connector-java-5.1.37-bin.jar ...