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. python manage.py startapp app 时候报错No module named _sqlite3

    python manage.py startapp app 报错如下: File "manage.py", line 10, in <module>    execut ...

  2. python解析HTML之:PyQuery库的介绍与使用

    本篇大部分转载于https://www.jianshu.com/p/c07f7cd1b548 先放自已自己解析techweb一个网站图片的代码 from pyquery import PyQuery ...

  3. react中性能优化的点

    react提升代码性能的点 1.绑定如果改变作用域点话放在constructor里面做,这样可以保证整个程序的作用域绑定操作只会执行一次,而且避免子组件的无谓渲染. 2.内置的setState是个异步 ...

  4. SignalR集成Autofac

    SignalR SignalR集成需要 Autofac.SignalR NuGet 包. SignalR 集成提供SignalR 集线器的依赖集成.由于 SignalR 是内部构件,所以不支持Sign ...

  5. Kinect骨架数据

  6. JavaScript:直接写入 HTML 输出流

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  7. mobileeye

    if a human can drive a car based on vision alone – so can a computer. 但是目前哪家能做到?

  8. Vue nodejs商城-地址模块

    一.地址列表渲染 ,则不可以点击. src/views/Cart.vue <a class="btn btn--red" v-bind:class="{'btn-- ...

  9. 11.多线程&&并发

    11.1 操作系统中线程和进程的概念 一些常见的概念: 程序:指令和数据的byte序列,eg:qq.exe;a2. 进程:正在运行的程序(如QQ);a3.一个进程中可能有一到多个线程. 线程的概念:T ...

  10. Python基础—06-函数基础

    函数基础 函数简介 定义:就是具有特定功能的一段代码 优点: 解决代码的重复书写 可以将功能的实现着和使用者分开,提高开发效率 分类: 库函数:print.input.abs等 自定义:用户自己封装的 ...