多说几句话,省的摘要里都是没格式的代码。。。
线段树的单点更新。
模板题。
重新整理了一下 0.0
#include <stdio.h> #define N (50005) #define LSON (idx << 1) #define RSON ((idx << 1) | 1) #define MID ((l + r) >> 1) int tree[N << 2]; void build (int l, int r, int idx) { if (l == r) { scanf ("%d", &tree[idx]); return ; } build (l, MID, LSON); build (MID + 1, r, RSON); tree[idx] = tree[LSON] + tree[RSON]; } int query (int L, int R, int l, int r, int idx) { if (L <= l && r <= R) return tree[idx]; if (MID + 1 > R) return query(L, R, l, MID, LSON); if (MID < L) return query (L, R, MID + 1, r, RSON); int a = query (L, R, l, MID, LSON); int b = query (L, R, MID + 1, r, RSON); return a + b; } void update (int A, int del, int l, int r, int idx) { if (r < A || A < l) return; if (l == r) { if (l == A) tree[idx] += del; return; } update (A, del, l, MID, LSON); update (A, del, MID + 1, r, RSON); tree[idx] = tree[LSON] + tree[RSON]; } int main() { int n_case; scanf ("%d", &n_case); for (int i_case = 1; i_case <= n_case; i_case++) { int n; scanf ("%d", &n); build (1, n, 1); printf ("Case %d:\n", i_case); for (char in[10]; ; ) { scanf ("%s", in); switch (in[0]) { case 'Q': { int l, r; scanf ("%d%d", &l, &r); printf ("%d\n", query (l, r, 1, n, 1)); } break; case 'E': break; case 'A': { int a, add; scanf ("%d%d", &a, &add); update (a, add, 1, n, 1); } break; case 'S': { int a, sub; scanf ("%d%d", &a, &sub); update (a, -sub, 1, n, 1); } break; default: break; } } } return 0; }