BZOJ3155:Preprefix sum——题解】的更多相关文章

3155: Preprefix sum Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 1183  Solved: 546[Submit][Status][Discuss] Description   Input 第一行给出两个整数N,M.分别表示序列长度和操作个数 接下来一行有N个数,即给定的序列a1,a2,....an 接下来M行,每行对应一个操作,格式见题目描述 Output 对于每个询问操作,输出一行,表示所询问的SSi的值. Sample…
https://www.lydsy.com/JudgeOnline/problem.php?id=3155 最朴素的想法是两棵树状数组,一个记录前缀和,一个记录前缀前缀和,但是第二个我们非常不好修改 但其实我们发现$SS_i=i*a1+(i-1)*a2+…+ai$,我们可以试图构造这样的“类等差”数列,这样我们就可以通过加加减减就能做了. 为了照顾最后一位的查询,我们就维护$(n-i+1)*ai$吧! 于是我们修改就变成了两个单点修改了,查询也就是很简单了,$qry(x,1)-qry(x,0)*…
题解: 写过树状数组搞区间修改和区间求和的就可以秒出吧... 代码: #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<map> #include<set> #include<q…
Description Input 第一行给出两个整数N,M.分别表示序列长度和操作个数 接下来一行有N个数,即给定的序列a1,a2,....an 接下来M行,每行对应一个操作,格式见题目描述 Output 对于每个询问操作,输出一行,表示所询问的SSi的值. Sample Input 5 3 1 2 3 4 5 Query 5 Modify 3 2 Query 5 Sample Output 35 32 HINT 1<=N,M<=100000,且在任意时刻0<=Ai<=10000…
3155: Preprefix sum 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3155 把给出的a_i当成查分数组d_i做就可以了. XJB搞一搞就过了. code: #include <iostream> #include <cstdio> using namespace std; #define int long long const int wx=200017; inline int read(){ int…
刷刷水题... 前缀和的前缀和...显然树状数组可以写...然而我不会, 只能写线段树了 把改变成加, 然后线段树维护前缀和, 某点p加, 会影响前缀和pre(x)(p≤x≤n), 对[p, n]这段区间加即可, 然后query就求[1, p]的和即可 --------------------------------------------------------------------------------- #include<bits/stdc++.h>   using namespac…
题目描述 前缀和(prefix sum)Si=∑k=1iaiS_i=\sum_{k=1}^i a_iSi​=∑k=1i​ai​. 前前缀和(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​ 输入输出格式 输入…
3155: Preprefix sum https://www.lydsy.com/JudgeOnline/problem.php?id=3155 分析: 区间修改,区间查询,线段树就好了. 然后,这题有树状数组! 代码: 线段树620ms /* 一个数修改影响后面的数,使后面的数都增加或者减少多少,所以线段树维护区间和,区间修改 */ #include<bits/stdc++.h> using namespace std; typedef long long LL; inline int r…
Description 前缀和(prefix sum)\(S_i=\sum_{k=1}^i a_i\). 前前缀和(preprefix sum) 则把\(S_i\)作为原序列再进行前缀和.记再次求得前缀和第i个是\(SS_i\) 给一个长度n的序列\(a_1, a_2, \cdots, a_n\)有两种操作: Modify i x:把\(a_i\)改成\(x\): Query i:查询\(SS_i\) Input 第一行给出两个整数N,M.分别表示序列长度和操作个数 接下来一行有N个数,即给定的…
目录 Ural 1248 Sequence Sum 题解 题意 题解 程序 Ural 1248 Sequence Sum 题解 题意 给定\(n\)个用科学计数法表示的实数\((10^{-100}\sim10^{100})\),输出它们的和. Tip: 一个实数可以用科学计数法表示为\(x\times10^y\),其中\(1\le x<10\) \(x\)为实数,\(y\)是整数.输入时表示为\(xey\).保证输入的实数有\(19\)位有效数字.输出时用科学计数法,必须包括\(19\)位正确数…
2021.08.09 P4868 Preprefix sum(树状数组) P4868 Preprefix sum - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 前缀和(prefix sum): \[S_i=\sum_{k=1}^i a_k \] 前前缀和(preprefix sum) 则把S_i作为原序列再进行前缀和.记再次求得前缀和第i个是SS_i 给一个长度n的序列a1,a2,⋯,a**n*,有两种操作: Modify i x:把a_i改成x: Query i:查…
文章目录 题意 思路 特殊情况k=0 Source Code 1 Source Code 2 题意 给定一个数组和一个整数k,返回是否存在一个长度至少为2的连续子数组的和为k的倍数. 思路 和上一篇博客的思路基本一致. LeetCode subarray-sum-equals-k题解 所不同的是,子数组至少长度为2.因此需要一个缓冲区,延缓往Hash表中加数的操作. 另外,因为是和变成是k的倍数.利用同余的知识易得我们维护的前缀和是群ZkZ_kZk​中的和. ps.这里我犯了一个错误,没有考虑k…
大意:给一个数组,先求出SUM[I],然后动态的求出1-I的SUM[I]的和, 这题得化公式: 树状数组维护两个和:SUM(A[I])(1<=I<=X); SUM(A[I]*(N-I+1)) (1<=I<=X); 答案就是:SUM(A[I]*(N-I+1))-SUM[A[I]]*(N-X) (1<=I<=X); #include<stdio.h> #include<string.h> #include<algorithm> #incl…
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains…
Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note:…
Three Sum: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. 这是一道LeetCode中标记为Medium的…
Tow Sum 原题概述: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums =…
题目链接 题意简介 将一个长度为 2n 的数列平均分为两个子数列 p 和 q 后,p 按从小到大排序,q 按从大到小排序. 排序后,记 p 为 \(\{x_i\}\) ,q 为 \(\{y_i\}\) ,对每种划分方式,有 \(f(p,q) = \sum |x_i - y_i|\) 现在我们想要知道对所有的划分方案 \((p,q)\) ,\(\sum f(p,q)\) 是多少. 数据范围:\(1 \leq n \leq 150000\) 答案对 998244353 取模. Two partiti…
https://www.luogu.org/problemnew/show/P4868 题目大意 单点修改,查询前缀前缀和. 分析 遇到了单点修改,前缀和,很明显是要树状数组维护解决问题. 请看以下我的数列的转换 \[s1+s2+s3+ \cdots +sn\] \[a1+a1+a2+a1+a2+a3+ \cdots +an\] \[a1*n+a2*(n-1)+a3*(n-2)+...an*1\] \[(a1+a2+a3+...+an) \times N - (a2+a3^2+a4^3+...+…
http://acm.hdu.edu.cn/showproblem.php?pid=4825 Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大.Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助.你能证明人类的智慧…
https://www.luogu.org/problemnew/show/P4868 线段树上加等差数列,基础区间修改单点查询 等差数列具有可加性,当在同一段区间内时,首项相加公差相加即可 #include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &f) { f = 0; T fu = 1; char c = getchar(); while(c <…
类似于树状数组维护区间的方法. 每一次询问要求$\sum_{i = 1}^{n}\sum_{j = 1}^{i}a_j$. 展开一下: $\sum_{i = 1}^{n}\sum_{j = 1}^{i}a_j = \sum_{i = 1}^{n}a_i * (n - i + 1) = (n + 1)\sum_{i = 1}^{n}a_i - \sum_{i = 1}^{n}a_i * i$ 用两个树状数组分别维护一下$\sum_{i = 1}^{n}a_i$和$\sum_{i = 1}^{n}a…
题面 挺有意思的. 设f[i]表示gcd(i,j)=i的个数,g[i]表示k|gcd(i,j)的个数; g[i]=(n/i)*(n/i); g[i]=f[i]+f[2i]+f[3i]+...; 所以f[i]=g[i]-f[2i]-f[3i]-f[4i]-...... #include <bits/stdc++.h> #define int long long using namespace std; ]; signed main() { int n; cin>>n; ; ;i--)…
传送门 挺显然的一题?单点修改,前缀和数组前缀查询 树状数组就可以维护了 考虑每个位置对应询问的贡献,设询问的位置为 $x$,对于原数组 $a[i]$ 的某个位置 $i$,它会贡献 $(x-i+1)*a[i]$ 即 $x*a[i]-(i-1)*a[i]$,直接对两个部分搞两个树状数组分别维护即可 具体就是搞个 $BIT_1$ 维护 $a[i]$ ,$BIT_2$ 维护 $a[i]*(i-1)$ ,对于询问 $x$ 答案就是 $BIT_1.query(x)*x - BIT_2.query(x)$…
原题链接 简要题意: 你可以无限次的把该数组的一个前缀和后缀 \(\times -1\),问最终的最大序列和. 这题盲目WA了数次才知道本质 这题89个数据吊打std CF真好啊,发现一个错后面就不测了 下面,就以我艰辛的思维历程来构造本篇博客. 算法一 盲猜:所有数都可以变成正数. 然后绝对值相加. 连样例也没测. 然后,\(\frac{2}{89} pts\). 只过了前两个样例,第三个就死了. #pragma GCC optimize(2) #include<bits/stdc++.h>…
题目连接:传送门 分析 这道题只看题目中给的样例是找不出规律的 所以我们可以打一下表 1, 2, 4, 5, 8, 10, 13, 14, 18 如果你还是没有看出什么规律的话,我们可以从OEIS上搜索一下 网址:传送门 把这一个数列输入到搜索框,然后就会出现下面的页面 传送门 第一个就是我们想要的数列 大家可以看一下上面的证明和递推式(都是英文) 所以我们可以得到\(f[x]=f[x/2]+f[(x-1)/2]+f[(x-2)/2]\) 证明摘自洛谷: 代码 #include<cstdio>…
codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) 的等比数列. 当公比为 \(1\) 时,不能用等比数列求和公式. 什么时候公比为 \(1\) ? 当 \(a=b\) 时,\(a^{-1}b=1(mod\ p)\) 当 \(a=p-b\) 时,\(a^{-1}b=(p-b)^{-1}b=-1(mod\ p)\),如果此时 \(k\) 是偶数,公比就…
Sum 系列题解 Two Sum题解 题目来源:https://leetcode.com/problems/two-sum/description/ Description Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one sol…
Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Description Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its…
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5648 题意:给你n,m 让你求出 for(1-n)for(1-m)gcd(i&j,i|j)的sum 题解:这题正解是枚举子集或者Claris的数位DP,不过这些我都不会,还是觉得简单粗暴的方法比较适合我,那就是打表,,数据范围是15000,那么就以250为一块,分60*60块出来,把60*60块的表打出来,然后不够的补上就行了.时间复杂度为O(250*max(n,m)) #include<cst…