萌新刚学树状数组,求找错
  • 板块P2068 统计和
  • 楼主E_D_ZY
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/2/3 23:29
  • 上次更新2023/11/5 03:48:29
查看原帖
萌新刚学树状数组,求找错
301766
E_D_ZY楼主2021/2/3 23:29

今天刚学了树状数组,兴冲冲地A了模板1、2之后,选标签找到这道题,却样例没过,去看了第一篇题解没看出什么错<@_@> 代码:

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

样例输出:

0
0
2021/2/3 23:29
加载中...