char arr[MAXC][MAXL];
int eva[MAXC];
int idx, len;
int evaluate(int s, int l, int index) {
int opr = 1;
int res = 0;
for (int i = s; i <= l; i++) {
if (arr[index][i] == '+') opr = 1;
else if (arr[index][i] == '-') opr = -1;
else if (arr[index][i] >= '0' && arr[index][i] <= '9') {
int x = arr[index][i] - '0';
if (i + 1 <= l && arr[index][i + 1] >= '0' && arr[index][i + 1] <= '9') {
i++;
x = x 10 + (arr[index][i] - '0');
}
if (i + 1 <= l && arr[index][i + 1] == '') {
i += 2;
int y = arr[index][i] - '0';
if (i + 1 <= l && arr[index][i + 1] >= '0' && arr[index][i + 1] <= '9') {
i++;
y = y 10 + (arr[index][i] - '0');
}
x = y;
}
res += (x*opr);
}
}
return res;
}
void init(int mLen, char mSubexp[])
{
idx = 0;
len = mLen;
arr[0][0] = '+';
for (int i = 0; i < mLen; i++) {
arr[0][i + 1] = mSubexp[i];
}
eva[0] = evaluate(0, len, 0);
idx++;
}
int append(char mLink, char mSubexp[])
{
arr[idx][0] = mLink;
for (int i = 1; i <= len; i++) {
arr[idx][i] = mSubexp[i - 1];
}
eva[idx] = eva[idx - 1] + evaluate(0, len, idx);
return eva[idx++];
}
int erase(int mFrom)
{
int r = mFrom / (len + 1);
int c = mFrom % (len + 1);
for (int i = c; i <= len; i++) {
arr[r][i] = arr[r + 1][i];
}
for (int i = r + 2; i < idx; i++) {
for (int j = 0; j <= len; j++) {
arr[i - 1][j] = arr[i][j];
}
}
idx--;
if (r == 0) {
eva[0] = evaluate(0, len, 0);
r++;
}
for (int i = r; i < idx; i++) {
eva[i] = eva[i - 1] + evaluate(0, len, i);
}
return eva[idx - 1];
}
int select(int mFrom, int mTo)
{
int r1 = mFrom / (len + 1);
int c1 = mFrom % (len + 1);
int r2 = mTo / (len + 1);
int c2 = mTo % (len + 1);
int ans = eva[r2 - 1] - eva[r1];
ans += evaluate(c1, len, r1) + evaluate(0, c2, r2);
return ans;
}
define MAXC 1001
define MAXL 101
using namespace std;
char arr[MAXC][MAXL]; int eva[MAXC]; int idx, len;
int evaluate(int s, int l, int index) { int opr = 1; int res = 0; for (int i = s; i <= l; i++) { if (arr[index][i] == '+') opr = 1; else if (arr[index][i] == '-') opr = -1; else if (arr[index][i] >= '0' && arr[index][i] <= '9') { int x = arr[index][i] - '0'; if (i + 1 <= l && arr[index][i + 1] >= '0' && arr[index][i + 1] <= '9') { i++; x = x 10 + (arr[index][i] - '0'); } if (i + 1 <= l && arr[index][i + 1] == '') { i += 2; int y = arr[index][i] - '0'; if (i + 1 <= l && arr[index][i + 1] >= '0' && arr[index][i + 1] <= '9') { i++; y = y 10 + (arr[index][i] - '0'); } x = y; } res += (x*opr); } } return res; }
void init(int mLen, char mSubexp[]) { idx = 0; len = mLen; arr[0][0] = '+'; for (int i = 0; i < mLen; i++) { arr[0][i + 1] = mSubexp[i]; } eva[0] = evaluate(0, len, 0); idx++; }
int append(char mLink, char mSubexp[]) { arr[idx][0] = mLink; for (int i = 1; i <= len; i++) { arr[idx][i] = mSubexp[i - 1]; } eva[idx] = eva[idx - 1] + evaluate(0, len, idx); return eva[idx++]; }
int erase(int mFrom) { int r = mFrom / (len + 1); int c = mFrom % (len + 1); for (int i = c; i <= len; i++) { arr[r][i] = arr[r + 1][i]; } for (int i = r + 2; i < idx; i++) { for (int j = 0; j <= len; j++) { arr[i - 1][j] = arr[i][j]; } } idx--; if (r == 0) { eva[0] = evaluate(0, len, 0); r++; } for (int i = r; i < idx; i++) { eva[i] = eva[i - 1] + evaluate(0, len, i); } return eva[idx - 1]; }
int select(int mFrom, int mTo) { int r1 = mFrom / (len + 1); int c1 = mFrom % (len + 1); int r2 = mTo / (len + 1); int c2 = mTo % (len + 1); int ans = eva[r2 - 1] - eva[r1]; ans += evaluate(c1, len, r1) + evaluate(0, c2, r2); return ans; }