社区讨论

萌新刚学树状数组,求找错

P2068统计和参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@locnp68p
此快照首次捕获于
2023/10/30 16:47
2 年前
此快照最后确认于
2023/11/05 03:48
2 年前
查看原帖
今天刚学了树状数组,兴冲冲地A了模板1、2之后,选标签找到这道题,却样例没过,去看了第一篇题解没看出什么错<@_@> 代码:
CPP
#include<bits/stdc++.h>
using namespace std;
int n,c[100005];
int lowbit(int x){return x&(-x);}
void update(int x,int v)
{
	while(x<=n)
	{
		c[x]+=v;
		x+=lowbit(x);
	}
	return;
}
int query(int x)
{
	int res=0;
	while(x>0)
	{
		res+=c[x];
		x-=lowbit(x);
	}
	return res;
}
int main()
{
	int n,w;
	scanf("%d%d",&n,&w);
	while(w--)
	{
		char op;
		int a,b;
		cin>>op;
		scanf("%d%d",&a,&b);
		if(op=='x')
			update(a,b);
		if(op=='y')
			printf("%d\n",query(b)-query(a-1));
	}
	return 0;
}
样例输出:
CPP
0
0

回复

3 条回复,欢迎继续交流。

正在加载回复...