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. 设计模式——建造者模式(BuilderPattern)

    建造者模式(Builder):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. UML图: Builder: package com.cnblog.clarck; /** * ...

  2. 比較全的XML系列工具 能够轻松实现排版、转换和打印!

    XML系列 --------------------------------------------------------------------------------1.Xsl Formatte ...

  3. 作为PHP开发者请务必了解Composer

    Composer是一个非常流行的PHP包依赖管理工具,已经取代PEAR包管理器,对于PHP开发者来说掌握Composer是必须的. 对于使用者来说Composer非常的简单,通过简单的一条命令将需要的 ...

  4. genil层

    genil 层将底层的业务逻辑封装成一个接口(例如 get_dynamic_result这种),供ui层调用(ui点击 search dynamic result按钮,会调用 get_dynamic_ ...

  5. Android学习笔记_61_手机安全卫士知识点归纳(1)状态/形状图形 GPS 设备管理器DeviceAdminReceiver ImageView属性

    1.在做程序自动安装更新的时候 ,必须保证程序的签名和包名是相同.  C:\Documents and Settings\zehua\.android  \ debug.keystore  debug ...

  6. ORA-01950:对表空间 'USERS' 无权限

    在存储数据时出现ORA-01950:对表空间 'USERS' 无权限 错误,解决如下: 找到对象Users下的用户名,然后点编辑,角色权限添加dba 在oracle创建了一个新的表空间和一个新的用户, ...

  7. c语言描述的顺序表实现

    //顺序表的实现:(分配一段连续地址给顺序表,像数组一样去操作) #include<stdio.h> #include<stdlib.h> #define OK 1 #defi ...

  8. java 后台返回文件流到浏览器

    package com.springbootblog.controller; import io.swagger.annotations.ApiImplicitParam;import io.swag ...

  9. 解决model属性与系统重名

    .h .m + (NSDictionary *)replacedKeyFromPropertyName { return @{ @"detailId" : @"id&qu ...

  10. 初探css3

    属性选择器: 1.完全匹配的属性选择器. 就是完全匹配的字符串. [id=article]{ color:red; } 2.包含匹配选择器.包含有指定的字符串. 语法是:[attribute*=val ...