UVa12086 - Potentiometers(树状数组即Fenwick树)

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class Main 
{
	public static final boolean DEBUG = false;
	public StreamTokenizer tokenizer;
	public BufferedReader cin;
	public PrintWriter cout;
	public int n;
	public int[] C, arr;
	public int cas = 0;
	
	public void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}
			cout = new PrintWriter(new OutputStreamWriter(System.out));
			tokenizer = new StreamTokenizer(cin);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String next()
	{
		try {
			tokenizer.nextToken();
			if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
			else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) return String.valueOf((int)tokenizer.nval);
			else if (tokenizer.ttype == StreamTokenizer.TT_WORD) return tokenizer.sval;
			else return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public int lowbit(int x)
	{
		return x & -x;
	}
	
	public void add(int x, int d)
	{
		while (x <= n) {
			C[x] += d;
			x += lowbit(x);
		}
	}
	
	public int sum(int x)
	{
		int ans = 0;
		
		while (x > 0) {
			ans += C[x];
			x -= lowbit(x);
		}
		
		return ans;
	}
	
	public boolean input() 
	{
		n = Integer.parseInt(next());
		if (n == 0) return false;
		
		C = new int[n + 1];
		arr = new int[n + 1];
		
		for (int i = 1; i <= n; i++) {
			int d = Integer.parseInt(next());
			arr[i] = d;
			add(i, d);
		}
		cas++;
		if (cas != 1) cout.println();
		
		return true;
	}

	
	public void solve() 
	{
		cout.println("Case " + cas + ":");
		while (true) {
			String s = next();
			if (s.compareTo("END") == 0) {
				cout.flush();
				break;
			}
			
			if (s.compareTo("S") == 0) {
				int x = Integer.parseInt(next());
				int d = Integer.parseInt(next());
				add(x, d - arr[x]);
				arr[x] = d;
			} else if (s.compareTo("M") == 0) {
				int l = Integer.parseInt(next());
				int h = Integer.parseInt(next());
				int ans = sum(h) - sum(l - 1);
				cout.println(ans);
			}
		}
		
	}

	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();
		
		while (solver.input()) {
			solver.solve();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值