0
点赞
收藏
分享

微信扫一扫

UVa 12086 Potentiometers (树状数组&点修改)


http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=502&page=show_problem&problem=3238


更新语句改为update(x, y - query(x, x));即可。

/*0.259s*/

const int mx = 200005;
int N, tree[mx];

void update(int p, int m)
{
	while (p <= N)
	{
		tree[p] += m;
		p += p & -p;
	}
}

int sum(int n)
{
	int sum = 0;
	while (n)
	{
		sum += tree[n];
		n -= n & -n;
	}
	return sum;
}

int query(int sta, int end) {return sum(end) - sum(sta - 1);}

#include<cstdio>
#include<cstring>

int main()
{
	//freopen("in.txt", "r", stdin);
	char ch, str[10];
	int i, x, y, tmpx, cas = 0;
	bool ok = false;
	while (scanf("%d", &N), N)
	{
		if (ok) putchar(10);
		else ok = true;
		printf("Case %d:\n", ++cas);
		memset(tree, 0, sizeof(tree));
		for (i = 1; i <= N; ++i)
		{
			scanf("%d", &x);
			update(i, x);
		}
		getchar();
		while ((ch = getchar()) != 'E')
		{
			scanf("%d%d", &x, &y);
			if (ch == 'S')
			{
				tmpx = query(x, x);
				update(x, y - tmpx);
			}
			else printf("%d\n", query(x, y));
			getchar();
		}
		gets(str);
	}
	return 0;
}



举报

相关推荐

0 条评论