LA 2191 - Potentiometers

看题传送门


Fenwick树的应用~~~


#include <cstdio>
#include <cstring>
#include<algorithm>
using namespace std;
const int MAXN=200000+10;
int a[MAXN],c[MAXN],n;

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

void add(int x,int d)
{
	while(x<=n)
	{
		c[x]+=d;
		x+=lowbit(x);
	}
}

long long  sum(int L,int R)
{
	int x=R;
	long long ans=0;
	while(x>0)
	{
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
}

int main()
{
	int kase=1;
	while(scanf("%d",&n),n)
	{
		if(kase!=1)
			printf("\n");
		printf("Case %d:\n",kase++);
		memset(c,0,sizeof(c));
		for(int i=1;i<=n;i++)
		{
				scanf("%d",&a[i]);
				add(i,a[i]);
		}				
		char action[10];		
		while(scanf("%s",action),strcmp(action,"END"))
		{
			int x,y;
			scanf("%d%d",&x,&y);
			if(action[0]=='S')
			{
					add(x,y-a[x]);
					a[x]=y;
			}
			else 
			{ printf("%lld\n",sum(x,y)-sum(1,x-1));}
		}
	}
}


你可能感兴趣的:(LA 2191 - Potentiometers)