[Luogu] 树状数组
2024-09-05 03:29:28
https://www.luogu.org/problemnew/show/P3374
单点修改,区间查询
#include <iostream>
#include <cstdio> using namespace std;
const int N = 5e5 + ; #define yxy getchar() int T[N], n, Ty; inline int read() {
int x = , f = ; char c = yxy;
while(c < '' || c > '') {if(c == '-') f = -; c = yxy;}
while(c >= '' && c <= '') x = x * + c - '', c = yxy;
return x * f;
} int lowbit(int x) {return x & (- x);} inline void Add(int id, int num) {
while(id <= n) {
T[id] += num;
id += lowbit(id);
}
} inline int Sum(int x) {
int ret = ;
while(x) {
ret += T[x];
x -= lowbit(x);
} return ret;
} int main() {
n = read();
Ty = read();
for(int i = ; i <= n; i ++) {
int x = read();
Add(i, x);
}
while(Ty --) {
int opt = read(), x = read(), y = read();
if(opt == ) Add(x, y);
else cout << Sum(y) - Sum(x - ) << endl;
} return ;
}
https://www.luogu.org/problemnew/show/P3368
区间修改,单点查询
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
const int N = 5e5 + ; #define yxy getchar() int T[N], A[N];
int n, Ty; inline int read() {
int x = , f = ;
char c = yxy;
while(c < '' || c > '') {
if(c == '-') f = -;
c = yxy;
}
while(c >= '' && c <= '') x = x * + c - '', c = yxy;
return x * f;
} inline int lowbit(int x) {
return x & - x;
} inline void Add(int x, int num) {
while(x <= n) {
T[x] += num;
x += lowbit(x);
}
} inline int Sum(int x) {
int ret = ;
while(x) {
ret += T[x];
x -= lowbit(x);
} return ret;
} int main() {
n = read();
Ty = read();
for(int i = ; i <= n; i ++) A[i] = read();
while(Ty --) {
int opt = read();
if(opt == ) {
int x = read(), y = read(), k = read();
Add(x, k); Add(y + , -k);
} else {
int x = read();
cout << A[x] + Sum(x) << endl;
}
}
return ;
}
P2068 统计和
https://www.luogu.org/problemnew/show/P2068
#include <iostream>
#include <cstdio> using namespace std;
const int N = 1e5 + ; #define yxy getchar()
#define R freopen("gg.in", "r", stdin) int T[N], n, Ty; inline int read() {
int x = , f = ; char c = yxy;
while(c < '' || c > '') {if(c == '-') f = -; c = yxy;}
while(c >= '' && c <= '') x = x * + c - '', c = yxy;
return x * f;
} inline int lowbit(int x) {
return x & (-x);
} inline void Add(int x, int y) {
while(x <= n) {
T[x] += y;
x += lowbit(x);
}
} inline int Sum(int x){
int ret = ;
while(x) {
ret += T[x];
x -= lowbit(x);
} return ret;
} int main() {
n = read();
Ty = read();
while(Ty --) {
char c = yxy;
int x = read(), y = read();
if(c == 'x') Add(x, y);
else cout << Sum(y) - Sum(x - ) << endl;
}
return ;
}
最新文章
- 为MySQL选择合适的备份方式
- C#读写txt文件的两种方法介绍
- C++安装失败解决办法
- linux下的调试工具ltrace与strace
- PHP 内存的分布问题
- Java Calendar实现控制台日历
- WindowsServer2012 取消密码策略
- showModalDialog-父窗体子窗体
- Selenium 上传文件失败,解决办法一
- 网络通信 -->; 互联网协议(一)
- Tomcat常用的过滤器
- SparkCore | Rdd| 广播变量和累加器
- Unity配置安卓开发环境
- 【SoftwareTesting】Homework3
- Android开发 android沉浸式状态栏的适配(包含刘海屏)转载
- Python 事件驱动了解
- .net mvc数据库操作添加数据的几中方法
- mysql命令行导入结构化数据
- android设备不识别awk命令,缺少busybox
- jsp之radio取值与赋值
热门文章
- Neo4j基本使用及导入三元组
- 图像识别领域的一些code
- centos mysql数据库问题:ERROR 1044 (42000): Access denied for user &#39;&#39;@&#39;localhost&#39; to database &#39;mysql&#39;(转)
- /sockjs-node/info 报错问题
- # 机器学习算法总结-第九天(XGboost)
- 苹果发布app,上传ipa,不显示问题
- C#的预处理指令
- Data truncation: Incorrect datetime value: &#39;May 15, 2019 4:15:37 PM
- Tcp/IP协议详讲
- 基于栈的指令集与基于寄存器的指令集详细比对及JVM执行栈指令集实例剖析