luogu3374 3368树状数组
来源:互联网 发布:c语言linuxsleep 编辑:程序博客网 时间:2024/06/05 05:51
引人入胜的3374
#include <iostream>#include <cstdio>using namespace std;int c[500005], n, m, u, v, w, k;inline int lowbit(int x){return x & -x;}void add(int pos, int num){while(pos<=n){c[pos] += num;pos += lowbit(pos);}}int sum(int pos){int temp=0;while(pos){temp += c[pos];pos -= lowbit(pos);}return temp;}int main(){cin>>n>>m;for(int i=1; i<=n; i++){scanf("%d", &k);add(i, k);}for(int i=1; i<=m; i++){scanf("%d %d %d", &w, &u, &v);if(w&1)add(u, v);elseprintf("%d\n", sum(v)-sum(u-1));}return 0;}当然,上述模板谁都会,那么3368就非常日怪了。
先讲查分。有原数组a,差分数组b:
b[i]=a[i]-a[i-1]
这有什么用?比如:对位置从2到5的数组加2得:
事实上,把数组从pos1到pos2加上数k,只需将差分数组b[pos1]加k,b[pos2+1]减k即可
赞美差分!
#include <iostream>#include <cstdio>using namespace std;int x, lastx=0, n, m, c[1000005], u, v, w;inline int lb(int x){return x & -x;}void add(int pos, int num){while(pos<=n){c[pos] += num;pos += lb(pos);}}int sum(int pos){int re=0;while(pos){re += c[pos];pos -= lb(pos);}return re;}int main(){cin>>n>>m;for(int i=1; i<=n; i++){scanf("%d", &x);add(i, x-lastx);lastx = x;}for(int i=1; i<=m; i++){scanf("%d", &x);if(x&1){scanf("%d %d %d", &u, &v, &w);add(u, w);add(v+1, -w);}else{scanf("%d", &u);printf("%d\n", sum(u));}}return 0;}
阅读全文
0 0
- luogu3374 3368树状数组
- luogu3374 树状数组
- 【洛谷3368】树状数组 2 树状数组+差分
- 洛谷3368树状数组2
- Luogu 3368(树状数组)
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- C语言初步-第31讲:循环控制结构及流程图(次数不定的循环的流程图)
- keras + LSTM
- 愤怒的奶牛
- 适合四季畅饮的饮料 体动果味苏打水
- VMWare 虚拟机安装CentOS 并配置网络连接
- luogu3374 3368树状数组
- tEST 2 for NOIP
- phpize安装扩展
- 机器视觉学习笔记(4)——单目摄像机标定参数说明
- 1308:Is It A Tree?
- 建立和配置Mavenweb 项目方法
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛b题Coin(矩阵快速幂)
- 08. 无脚本的JSP
- 1.卷2(进程间通信)---简介