Friday, December 14, 2007

Potentiometers

ACCEPTED !! Another one from Dhaka regionals 2006/07
____________________________________________________________________
Question: Potentiometers
____________________________________________________________________

#include "iostream.h"
#include "math.h"

int main()
{

int t = 1;
while(1) {
int n;
cin >> n;
if(n == 0) break;
if(t > 1) cout << endl;
int parr[n];
int r = ceil(sqrt(n));
int c = sqrt(n);
int sparr[r];
for(int i = 0; i < r; i++) sparr[i] = 0;
for(int i = 0; i < n; i++) {
cin >> parr[i];
sparr[i/c] += parr[i];
}
cout << "Case " << t << ":\n";
while(1) {
char command[4];
cin >> command;
if(strcmp(command, "END") == 0) break;
if(strcmp(command, "S") == 0) {
int x, r;
cin >> x >> r;
x--;
sparr[x/c] += r - parr[x];
parr[x] = r;
}
else if(strcmp(command, "M") == 0) {
int x, y;
cin >> x >> y;
x--;
y--;
int sr = x/c;
int er = y/c;
int res = 0;
for(int i = sr; i < er; i++) res += sparr[i];
for(int i = sr*c; i < x; i++) res -= parr[i];
for(int i = er*c; i <= y; i++) res += parr[i];
cout << res << endl;
}
}
t++;
}
return 0;
}

____________________________________________________________________

No comments:

Contributors