《算法竞赛进阶指南》0x00树状数组

清点人数

#include <iostream>
#include <cstdio>
using namespace std;

const int N = 5e5 + 10;
int n, k, c[N];		//c为原序列的树状数组

int lowbit(int x) { return x & -x; }

//将原序列下标为pos的元素值增加x,改变相应的c数组的值
void update(int pos, int x) {
	for (int i = pos; i <= n; i += lowbit(i)) c[i] += x;
}

//对原序列前pos项求和
int sum(int pos) {
	int res = 0;
	for (int i = pos; i > 0; i -= lowbit(i)) res += c[i];
	return res;
}

int main() {
	scanf("%d%d", &n, &k);
	
	while (k --) {
		char op[2];		//注意字符变量的输入方式
		int m, p;
		scanf("%s", &op);
		if (op[0] == 'A') scanf("%d", &m), printf("%d\n", sum(m));
		if (op[0] == 'B') scanf("%d%d", &m, &p), update(m, p);
		if (op[0] == 'C') scanf("%d%d", &m, &p), update(m, -p);		
	}

	return 0;
}

数列操作

#include <iostream>
#include <cstdio>
using namespace std;

const int N = 1e5 + 10;
int n, m, x, c[N];	//c为原序列的树状数组

int lowbit(int x) { return x & -x; }

//将原序列下标为pos的元素值增加x,改变相应的c数组的值
void update(int pos, int x) {
	for (int i = pos; i <= n; i += lowbit(i)) c[i] += x;
}

//对原序列前pos项求和
int sum(int pos) {
	int res = 0;
	for (int i = pos; i > 0; i -= lowbit(i)) res += c[i];
	return res;
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i ++) scanf("%d", &x), update(i, x);
	
	while (m --) {
		int k, a, b;
		scanf("%d%d%d", &k, &a, &b);
		//sum(b) - sum(a - 1)即为区间[a, b]的和
		if (k == 0) printf("%d\n", sum(b) - sum(a - 1));
		else update(a, b);
	}

	return 0;
}

简单题

#include <iostream>
#include <cstdio>
using namespace std;

const int N = 1e5 + 10;
int n, m, c[N];

int lowbit(int x) { return x & -x; }

void update(int pos, int x) {
	for (int i = pos; i <= n; i += lowbit(i)) c[i] += x;
}

int sum(int pos) {
	int res = 0;
	for (int i = pos; i > 0; i -= lowbit(i)) res += c[i];
	return res;
}

int main() {
	scanf("%d%d", &n, &m);
	
	while (m --) {
		int t, l, r;
		scanf("%d", &t);
		if (t == 1) scanf("%d%d", &l, &r), update(l, 1), update(r + 1, -1);
		else scanf("%d", &l), printf("%d\n", sum(l) % 2);
	}

	return 0;
}

数星星Stars

#include <iostream>
#include <cstdio>
using namespace std;

const int N = 2e4 + 10, MAXX = 32000 + 10;
int n, m, c[MAXX], h[N];

int lowbit(int x) { return x & -x; }
	
void update(int pos, int x) {
	for (int i = pos; i <= MAXX; i += lowbit(i)) c[i] += x;
}
	
int sum(int pos) {
	int res = 0;
	for (int i = pos; i > 0; i -= lowbit(i)) res += c[i];
	return res;
}

int main() {
	scanf("%d", &n);
	
	for (int i = 1; i <= n; i ++) {
		int x, y;
		scanf("%d%d", &x, &y);
		x ++;        //根据题意,x有可能为0
		
		int t = sum(x);
		h[t] ++;
		update(x, 1);
	}
	
	for (int i = 0; i < n; i ++) printf("%d\n", h[i]);

	return 0;
}

校门外的树

#include <iostream>
#include <cstdio>
using namespace std;

const int N = 5e4 + 10;
int n, m, c1[N], c2[N];

int lowbit(int x) { return x & -x; }

void update1(int pos, int x) {
	for (int i = pos; i <= n; i += lowbit(i)) c1[i] += x;
}

void update2(int pos, int x) {
	for (int i = pos; i <= n; i += lowbit(i)) c2[i] += x;
}

int sum1(int pos) {
	int res = 0;
	for (int i = pos; i > 0; i -= lowbit(i)) res += c1[i];
	return res;
}

int sum2(int pos) {
	int res = 0;
	for (int i = pos; i > 0; i -= lowbit(i)) res += c2[i];
	return res;
}

int main() {
	scanf("%d%d", &n, &m);
	
	while (m --) {
		int k, l, r;
		scanf("%d%d%d", &k, &l, &r);
		
		if (k == 1) update1(l, 1), update2(r, 1);
		else printf("%d\n", sum1(r) - sum2(l - 1));
	}

	return 0;
}
上一篇:一起玩转玩转LiteOS组件:Opus


下一篇:51单片机自学笔记(十)——8×8LED点阵