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();
}
}
}
UVa12086 - Potentiometers(树状数组即Fenwick树)
最新推荐文章于 2024-01-22 05:45:00 发布