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. C++ cin不支持录入空格

    如果在C++中,用cin>>str;这种方法来接收字符串那么录入的str不能包含空格,否则它会按照空格将整个字符串切分成若干段.如果你要是想输入带空格的字符串那就要用到getline()这 ...

  2. linux下安装jdk和配置环境变量

    参考博文:http://www.cnblogs.com/samcn/archive/2011/03/16/1986248.html 系统环境:linux centos 6.4_x64 软件版本:jdk ...

  3. log4net 配置完成后发现不能输出日志的解决方法

    配置好log4net后发现日志不能输出,打开调试看一下几个属性都是false,(比如isdebugenable =false)这其实是项目的启动时候没有加入一行声明代码导致的,可以在程序的Assemb ...

  4. Power Designer逆向工程连接数据库创建pdm-oracle

    1.进入菜单文件-Reverse Engineer-Database... 2.打开窗口,选择数据库版本,点击[确定] 3.打开窗口,选择Usering a data source: 4.如果已经有d ...

  5. ExtJS4 ajax请求同步异步问题

    今天在写代码过程中遇到一个奇怪的问题.事情是这种,我写了一个简单的页面用来删除选中的用户,请看以下: 由于后台的servlet的代码实现了依据用户名来删除一条记录.所以我在前台的ExtJS代码里面用了 ...

  6. Uva 11600 期望DP

    题意:n个城市,相互可达(有n(n-1)/2条边),其中有一些道路上面有妖怪,现在,从1号城市出发,随机挑取一个城市走去,这个道路上的妖怪就会被消灭,求: 在平均情况下,需要走多少步,使得任意两个城市 ...

  7. POJ 1330 Nearest Common Ancestors 【LCA模板题】

    任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000 ...

  8. (第七场)A Minimum Cost Perfect Matching 【位运算】

    题目链接:https://www.nowcoder.com/acm/contest/145/A A.Minimum Cost Perfect Matching You have a complete ...

  9. HashMap对HashCode碰撞的处理

    先说Java之外的,什么是拉链法?怎么解决冲突的: 拉链法解决冲突的做法是:将所有关键字为同义词的结点链接在同一个单链表中. 若选定的散列表长度为m,则可将散列表定义为一个由m个头指针组成的指针数组t ...

  10. (转)理解YOLOv2训练过程中输出参数含义

    最近有人问起在YOLOv2训练过程中输出在终端的不同的参数分别代表什么含义,如何去理解这些参数?本篇文章中我将尝试着去回答这个有趣的问题. 刚好现在我正在训练一个YOLOv2模型,拿这个真实的例子来讨 ...