1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#define _CRTSECURE_NOWARNINGS #pragma warning(disable:4996) #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cctype> #include<algorithm> #include<iostream> #include<queue> #include<stack> #include<vector> #include<map> #include<set> #include<list> using namespace std;
const int MAX_LEN = 1000016; int n, m, q;
struct SEGT { int arr[MAX_LEN], tree[MAX_LEN << 2]; void build_tree(int node, int start, int end) { if (start == end) { tree[node] = arr[start]; return; }
int mid = (start + end) >> 1; int left_node = node << 1; int right_node = node << 1 | 1;
build_tree(left_node, start, mid); build_tree(right_node, mid + 1, end);
tree[node] = tree[left_node] + tree[right_node]; }
void update_tree(int node, int start, int end, int idx, int val) { if (start == end) { arr[idx] = val; tree[node] = arr[idx]; return; }
int mid = (start + end) >> 1; int left_node = node << 1; int right_node = node << 1 | 1;
if (idx <= mid) update_tree(left_node, start, mid, idx, val); else update_tree(right_node, mid + 1, end, idx, val);
tree[node] = tree[left_node] + tree[right_node]; }
int query_tree(int node, int start, int end, int l, int r) { if (r < start || l > end) return 0; if (l <= start && r >= end) return tree[node]; if (start == end) return tree[node];
int mid = (start + end) >> 1; int left_node = node << 1; int right_node = node << 1 | 1; int sum_left = query_tree(left_node, start, mid, l, r); int sum_right = query_tree(right_node, mid + 1, end, l, r);
return sum_left + sum_right; } };
SEGT segt;
int main() { while (scanf("%d", &n) != EOF) { for (int i = 1; i <= n; i++) scanf("%d", &segt.arr[i]); segt.build_tree(1, 1, n); scanf("%d", &m); while (m--) { int idx, val; scanf("%d%d", &idx, &val); segt.update_tree(1, 1, n, idx, val); } scanf("%d", &q); while (q--) { int l, r; scanf("%d%d", &l, &r); printf("%d\n", segt.query_tree(1, 1, n, l, r)); } }
return 0;
}
|