奇怪的代码增加了
小编原以为自己可以拿到50分的好成绩,结果只拿到了30分,小编当时很疑惑,考后看了很久也没看出来错在了哪里,你看出来了吗?
} else {
int last = 0, sum = 0;
for (register int i = 1; i <= m; i++) {
k[i] = read(), v[i] = read();
if (k[i] == 1) {
last = i;
sum = 0;
} else {
sum += v[i];
}
}
int C, K, V;
for (register int i = 1; i <= q; i++) {
C = read(), K = read(), V = read();
if (C < last) {///在左无影响
k[C] = K, v[C] = V;
printf ("%d\n", sum);
} else if (C == last && K == '1') {//不变
printf ("%d\n", sum);
} else if (C > last && K == '0') {//在右加答案
sum += (V - v[C]);
k[C] = 0, v[C] = V;
printf ("%d\n", sum);
} else if (C == last && K == '0') {//换切点
k[C] = 0, v[C] = V;
sum += V;
for (register int j = last - 1; j >= 1; j--) {
if (k[j] == '1') {
last = j;
break;
} else {
sum += v[j];
}
}
printf ("%d\n", sum);
} else if (C > last && K == '1') {//
sum = 0;
k[C] = 1, v[C] = V;
for (register int j = m; j >= 1; j--) {
if (k[j] == '1') {
last = j;
break;
} else {
sum += v[j];
}
}
printf ("%d\n", sum);
}
}
}
答案揭晓:
} else {
int last = 0, sum = 0;
for (register int i = 1; i <= m; i++) {
k[i] = read(), v[i] = read();
if (k[i] == 1) {
last = i;
sum = 0;
} else {
sum += v[i];
}
}
int C, K, V;
for (register int i = 1; i <= q; i++) {
C = read(), K = read(), V = read();
if (C < last) {///在左无影响
k[C] = K, v[C] = V;
printf ("%d\n", sum);
} else if (C == last && K == 1) {//不变
printf ("%d\n", sum);
} else if (C > last && K == 0) {//在右加答案
sum += (V - v[C]);
k[C] = 0, v[C] = V;
printf ("%d\n", sum);
} else if (C == last && K == 0) {//换切点
k[C] = 0, v[C] = V;
sum += V;
for (register int j = last - 1; j >= 1; j--) {
if (k[j] == 1) {
last = j;
break;
} else {
sum += v[j];
}
}
printf ("%d\n", sum);
} else if (C > last && K == 1) {//
sum = 0;
k[C] = 1, v[C] = V;
for (register int j = m; j >= 1; j--) {
if (k[j] == 1) {
last = j;
break;
} else {
sum += v[j];
}
}
printf ("%d\n", sum);
}
}
}
学到了吗? 如果学会了请把学废了打在评论区里23333