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. HTML中什么时候加px

    如:<img width="800" height="600" src="#"/> 不加后面的px;  #center{     ...

  2. sqlite迁移mysql(导入导出数据)

    第一步,将数据导出 进入sqlite3->.open [打开文件路径]->.cd [要保存的路径]->.output [导出文件名字.sql]->.dump 等待导出成功后,就 ...

  3. 深入 Struts2 的配置 - 处理多个请求-处理请求结果-模型驱动-异常机制

    转:http://www.java3z.com/cwbwebhome/article/article2/2938.html?id=1631 本部分主要介绍struts.xml的常用配置. 1.1.   ...

  4. TridentState分析

    public class TridentState { TridentTopology _topology; Node _node; protected TridentState(TridentTop ...

  5. iOS开发中用到的第三方库概览

    前言:记录一下使用过和接触过的第三方库,重要程度与顺序无关 网络请求: AFNetworking:AFNetworking is a delightful networking library for ...

  6. Oracle单行函数用法

    单行函数分为五种类型:字符函数.数值函数.日期函数.转换函数.通用函数. 1.字符函数: 对于输入的字符转换为需要转为的字符或数值. upper()大写 --小写字母转为大写字母 --对于表指定的字符 ...

  7. iOS 从0到1搭建高可用App框架

    iOS 从0到1搭建高可用App框架 最近在搭建新项目的iOS框架,一直在思考如何才能搭建出高可用App框架,能否避免后期因为代码质量问题的重构.以前接手过许多“烂代码”,架构松散,底层混乱,缺少规范 ...

  8. django-模板层基础2

    1.模板的导入 {% include 模板名%} 首先在你的的项目中,需要很多地方用到同一个组件(相对于头部,你进行每个页面的切换,网页最上面的头 部不需要改变),那么这样我们可以把那个头部重新写在一 ...

  9. css3 RGBA 红色R+绿色G+蓝色B+Alpha通道

    语法: R:红色值.正整数 | 百分数 G:绿色值.正整数 | 百分数 B:蓝色值.正整数| 百分数 A:透明度.取值0~1之间 取值: <length>:Hue(色调).0(或360)表 ...

  10. Win10 VirtualBox虚拟机搭建lnmp环境

    之前用的是vagrant+VirtualBox搭建的环境,因为是windows系统动不动就报错,打不开环境,所以还是老老实实换了虚拟机哎.... 版本: VirtualBox 5.1.34   xsh ...