树状数组

单点加&区间和

Luogu - P3374 【模板】树状数组 1

//Lougu P3374 树状数组
#include<bits/stdc++.h>
#define N 500005
#define lowbit(x) ((x) & (-x))
using namespace std;
int n, m, a, b, op;
int tree[N];
void update(int i, int x) {
	for(int pos = i; pos <= n; pos += lowbit(pos)) {
		tree[pos] += x;
	}
}
int query(int i) {
	int res = 0;
	for(int pos = i; pos; pos -= lowbit(pos)) {
		res += tree[pos];
	}
	return res;
}
int query(int a, int b) {
	return query(b) - query(a - 1);
}
int main() {
	cin >> n >> m;
	for(int i = 1; i <= n; ++i) {
		cin >> a;
		update(i, a);
	}
	for(int i = 1; i <= m; ++i) {
		cin >> op >> a >> b;
		if(op == 1) {
			update(a, b);
		} else {
			cout << query(a, b) << endl;
		}
	}
	return 0;
}

区间加&单点和

使用差分
Luogu - P3368 【模板】树状数组 2

//Lougu P3368 树状数组
#include<bits/stdc++.h>
#define N 500005
#define lowbit(x) ((x) & (-x))
using namespace std;
int n, m, x, y, k, op;
int a[N], tree[N];
void update(int i, int x) {
	for(int pos = i; pos <= n; pos += lowbit(pos)) {
		tree[pos] += x;
	}
}
int query(int i) {
	int res = 0;
	for(int pos = i; pos; pos -= lowbit(pos)) {
		res += tree[pos];
	}
	return res;
}
int query(int a, int b) {
	return query(b) - query(a - 1);
}
int main() {
	cin >> n >> m;
	for(int i = 1; i <= n; ++i) {
		cin >> a[i];
		update(i, a[i] - a[i - 1]);
	}
	for(int i = 1; i <= m; ++i) {
		cin >> op;
		if(op == 1) {
			cin >> x >> y >> k;
			update(x, k);
			update(y + 1, -k);
		} else {
			cin >> x;
			cout << query(x) << endl;
		}
	}
	return 0;
}

区间加&区间和

Luogu - P3372 【模板】线段树 1

//Luogu P3372 树状数组 区间加 + 区间和
#include<bits/stdc++.h>
#define N 100005
#define int long long
#define lowbit(x) ((x)&(-x))
using namespace std;
int n, m, op;
int x, y, k;
int a[N], ta[N], tb[N];
void adda(int x, int k) {
	for(int i = x; i <= n; i += lowbit(i)) {
		ta[i] += k;
	}
}
void addb(int x, int k) {
	for(int i = x; i <= n; i += lowbit(i)) {
		tb[i] += k;
	}
}
int querya(int x) {
	int res = 0;
	for(int i = x; i; i -= lowbit(i)) {
		res += ta[i];
	}
	return res;
}
int queryb(int x) {
	int res = 0;
	for(int i = x; i; i -= lowbit(i)) {
		res += tb[i];
	}
	return res;
}
signed main() {
	scanf("%lld%lld", &n, &m);
	for(int i = 1; i <= n; ++i) {
		scanf("%lld", &a[i]);
	}
	for(int i = 1; i <= n; ++i) {
		adda(i, a[i] - a[i - 1]);
		addb(i, (a[i] - a[i - 1]) * (i - 1));
	}
	for(int i = 1; i <= m; ++i) {
		scanf("%lld", &op);
		if(op == 1) {
			scanf("%lld%lld%lld", &x, &y, &k);
			adda(x, k);
			adda(y + 1, -k);
			addb(x, k * (x - 1));
			addb(y + 1, -k * y);
		} else if(op == 2) {
			scanf("%lld%lld", &x, &y);
			printf("%lld\n", (y * querya(y) - (x - 1) * querya(x - 1) - (queryb(y) - queryb(x - 1))));
		}
	}
	return 0;
}

例题

求逆序对

Luogu - P1908 逆序对

#include<bits/stdc++.h>
#define N 500005
#define ll long long
#define lowbit(x) ((x)&(-x))
using namespace std;
int n, rank[N];
ll ans;
struct Node
{
    int val, id;
} a[N];
bool cmp(Node x, Node y) {
    if(x.val == y.val) return x.id < y.id;
    return x.val < y.val;
}
int tree[N];
void add(int x, int k) {
    for(int i = x; i <= n; i += lowbit(i)) {
        tree[i] += k;
    }
}
int query(int x) {
    int res = 0;
    for(int i = x; i; i -= lowbit(i)) {
        res += tree[i];
    }
    return res;
}
int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &a[i].val);
        a[i].id = i;
    }
    sort(a + 1, a + n + 1, cmp);
    for(int i = 1; i <= n; ++i) {
        rank[a[i].id] = i;
    }
    for(int i = n; i >= 1; --i) {
        ans += query(rank[i]);
        add(rank[i], 1);
    }
    printf("%lld\n", ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值