https://www.luogu.org/problemnew/show/P3374
单点修改,区间查询
#include
#include
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
#include
#include
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 ;
}
https://www.luogu.org/problemnew/show/P2068
#include
#include
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 ;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章