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 read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for (;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; #define Root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1 LL sum[N<<],tag[N<<],a[N],s[N];
void pushup(int rt) {
sum[rt] = sum[rt<<] + sum[rt<<|];
}
void pushdown(int rt,int len) {
if (tag[rt]) {
tag[rt<<] += tag[rt]; sum[rt<<] += tag[rt] * 1ll * (len-len/);
tag[rt<<|] += tag[rt]; sum[rt<<|] += tag[rt] * 1ll * (len/);
tag[rt] = ; //--忘记1
}
}
void build(int l,int r,int rt) {
if (l == r) {
sum[rt] = s[l];
return ;
}
int mid = (l + r) >> ;
build(lson);
build(rson);
pushup(rt);
}
void update(int l,int r,int rt,int L,int R,LL val) {
if (L <= l && r <= R) {
sum[rt] += val*1ll*(r-l+);tag[rt] += val;
return ;
}
int mid = (l + r) >> ;
pushdown(rt,r-l+);
if (L <= mid) update(lson,L,R,val);
if (R > mid) update(rson,L,R,val);
pushup(rt);
}
LL query(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) {
return sum[rt];
}
int mid = (l + r) >> ;
LL res = ;
pushdown(rt,r-l+);
if (L <= mid) res += query(lson,L,R);
if (R > mid) res += query(rson,L,R);
return res;//--忘记2
}
int main() {
int n = read(),m = read();
for (int t,i=; i<=n; ++i) {
a[i] = read();
s[i] = s[i-] + a[i];
}
build(Root);
char opt[];
while (m--) {
scanf("%s",opt);
if (opt[]=='Q') {
int p = read();
printf("%lld\n",query(Root,,p));
}
else {
int p = read(); LL v = read();
update(Root,p,n,v-a[p]);
a[p] = v;
}
}
return ;
}

树状数组392ms

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for (;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
LL a[N]; struct BIT{
int n;LL c1[N],c2[N];
void update(int p,LL v) {
for (int i=p; i<=n; i+=(i&(-i))) c1[i] += v,c2[i] += 1ll*p*v; // long long
}
LL query(int p) {
LL ans = ;
for (int i=p; i; i-=(i&(-i))) ans += 1ll*(p+)*c1[i]-c2[i];
return ans;
}
}bit; int main() {
int n = read(),m = read();
bit.n = n; // ---
for (int i=; i<=n; ++i) {
a[i] = read();
bit.update(i,a[i]);
}
char opt[];
while (m--) {
scanf("%s",opt);
if (opt[]=='Q') {
int p = read();
printf("%lld\n",bit.query(p));
}
else {
int p = read(); LL v = read();
bit.update(p,v-a[p]);
a[p] = v;
}
}
return ;
}

3155: Preprefix sum的更多相关文章

  1. BZOJ 3155: Preprefix sum( 线段树 )

    刷刷水题... 前缀和的前缀和...显然树状数组可以写...然而我不会, 只能写线段树了 把改变成加, 然后线段树维护前缀和, 某点p加, 会影响前缀和pre(x)(p≤x≤n), 对[p, n]这段 ...

  2. BZOJ 3155: Preprefix sum

    大意:给一个数组,先求出SUM[I],然后动态的求出1-I的SUM[I]的和, 这题得化公式: 树状数组维护两个和:SUM(A[I])(1<=I<=X); SUM(A[I]*(N-I+1) ...

  3. [bzoj3155]Preprefix sum(树状数组)

    3155: Preprefix sum Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 1183  Solved: 546[Submit][Status] ...

  4. 树状数组【bzoj3155】: Preprefix sum

    3155: Preprefix sum 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3155 把给出的a_i当成查分数组d_i做就可以了 ...

  5. Preprefix sum BZOJ 3155 树状数组

    题目描述 前缀和(prefix sum)Si=∑k=1iaiS_i=\sum_{k=1}^i a_iSi​=∑k=1i​ai​. 前前缀和(preprefix sum) 则把SiS_iSi​作为原序列 ...

  6. 差分+树状数组【p4868】Preprefix sum

    Description 前缀和(prefix sum)\(S_i=\sum_{k=1}^i a_i\). 前前缀和(preprefix sum) 则把\(S_i\)作为原序列再进行前缀和.记再次求得前 ...

  7. 2021.08.09 P4868 Preprefix sum(树状数组)

    2021.08.09 P4868 Preprefix sum(树状数组) P4868 Preprefix sum - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 前缀和(pr ...

  8. BZOJ3155: Preprefix sum

    题解: 写过树状数组搞区间修改和区间求和的就可以秒出吧... 代码: #include<cstdio> #include<cstdlib> #include<cmath& ...

  9. BZOJ3155:Preprefix sum——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3155 最朴素的想法是两棵树状数组,一个记录前缀和,一个记录前缀前缀和,但是第二个我们非常不好修改 ...

随机推荐

  1. python3乱码问题:接口返回数据中文乱码问题解决

    昨天测试接口出现有一个接口中文乱码问题,现象: 1 浏览器请求返回显示正常 2 用代码请求接口返回数据中文显示乱码 3 使用的python3,python3默认unicode编码,中文都是可以正常显示 ...

  2. 动态规划(DP),压缩状态,插入字符构成回文字符串

    题目链接:http://poj.org/problem?id=1159 解题报告: 1.LCS的状态转移方程为 if(str[i-1]==str[j-1]) dp[i][j]=dp[i-1][j-1] ...

  3. 关于message pack as3 版本的一些修改。

    玩个人项目的时候,根据前辈的推荐开始用messagepack,分别是py版和as3版: 然后在github上找到了demonsters的as3模块,于是颠屁颠屁地拿来用了,用的时候发现了一些小地方过不 ...

  4. 【luogu P1993 小K的农场】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1993 1.差分约束: 对于a - b <= c 有一条 b-->a 权值为c 对于a - b & ...

  5. 【luoguP1238】【NOIP2014】生活大爆炸版剪刀石头布

               生活大爆炸版剪刀石头布                      ——[传送门] 这道题可以原原本本地说得上是一道水题了,通过判断两人的出拳不同给分然后统计输出.就是对于游戏得分 ...

  6. yarn默认配置

    name value description yarn.ipc.client.factory.class   Factory to create client IPC classes. yarn.ip ...

  7. Ajax,Json数据格式

    同步和异步 同步现象:客户端发送请求到服务器端,当服务器返回响应之前,客户端都处于等待     卡死状态 异步现象:客户端发送请求到服务器端,无论服务器是否返回响应,客户端都可以随     意做其他事 ...

  8. Struts2 第五讲 -- Struts2与Servlet的API解耦

    为了避免与 Servlet API 耦合在一起, 方便 Action 做单元测试, Struts2 对 HttpServletRequest, HttpSession 和 ServletContext ...

  9. web常用软件

    编辑器: VSCode HBuilder WebStorm NotePad++ Eclipse Atom 常用插件: SwitchyOmega Vue-Tools server类: tomcat Ng ...

  10. ubuntu系统下的docker

    官网:https://www.docker.com/ 相关资料:1.Docker入门教程 http://dockone.io/article/1112.Docker_百度百科 http://baike ...