ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2022-01-08 09:31:59  阅读:123  来源: 互联网

标签:return 进阶 树状 int res scanf d% 0x00 pos


清点人数

#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;
}

标签:return,进阶,树状,int,res,scanf,d%,0x00,pos
来源: https://blog.csdn.net/davidliule/article/details/122375988

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有