#include <stdio.h> #include <stdlib.h> #define MAX_N 100005 // 线段树节点结构体 typedef struct TreeNode { int left; int right; long long sum; struct TreeNode* leftChild; struct TreeNode* rightChild; } TreeNode; // 构建线段树 TreeNode* buildTree(int arr[], int left, int right) { TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); node->left = left; node->right = right; if (left == right) { node->sum = arr[left]; node->leftChild = node->rightChild = NULL; } else { int mid = left + (right - left) / 2; node->leftChild = buildTree(arr, left, mid); node->rightChild = buildTree(arr, mid + 1, right); node->sum = node->leftChild->sum + node->rightChild->sum; } return node; } // 更新线段树中某个节点的值(用于单点修改操作) void update(TreeNode* node, int index, int val) { if (node->left == node->right) { node->sum += val; return; } int mid = node->left + (node->right - node->left) / 2; if (index <= mid) { update(node->leftChild, index, val); } else { update(node->rightChild, index, val); } node->sum = node->leftChild->sum + node->rightChild->sum; } // 查询区间和 long long query(TreeNode* node, int left, int right) { if (node->left >= left && node->right <= right) { return node->sum; } int mid = node->left + (node->right - node->left) / 2; long long sum = 0; if (left <= mid) { sum += query(node->leftChild, left, right); } if (right > mid) { sum += query(node->rightChild, left, right); } return sum; } // 释放线段树节点内存 void freeTree(TreeNode* node) { if (node == NULL) return; freeTree(node->leftChild); freeTree(node->rightChild); free(node); } int main() { int n, q; scanf("%d%d", &n, &q); int arr[MAX_N]; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } TreeNode* root = buildTree(arr, 0, n - 1); for (int i = 0; i < q; i++) { int op, l, r, index, val; scanf("%d", &op); if (op == 1) { scanf("%d%d", &index, &val); update(root, index - 1, val); } else { scanf("%d%d", &l, &r); long long result = query(root, l - 1, r - 1); printf("%lld\n", result); } } freeTree(root); return 0; }