> For the complete documentation index, see [llms.txt](https://dfine.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dfine.gitbook.io/leetcode/985.sum_of_even_numbers_after_queries.md).

# 985.Sum of Even Numbers After Queries

难度:Easy

**985.Sum of Even Numbers After Queries**

> 给出一个整数数组 A 和一个查询数组 queries。 对于第 i 次查询，有 val = queries\[i]\[0], index = queries\[i]\[1]，我们会把 val 加到 A\[index] 上。然后，第 i 次查询的答案是 A 中偶数值的和。 （此处给定的 index = queries\[i]\[1] 是从 0 开始的索引，每次查询都会永久修改数组 A。） 返回所有查询的答案。你的答案应当以数组 answer 给出，answer\[i] 为第 i 次查询的答案。

示例：

```
输入：A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
输出：[8,6,2,4]
解释：
开始时，数组为 [1,2,3,4]。
将 1 加到 A[0] 上之后，数组为 [2,2,3,4]，偶数值之和为 2 + 2 + 4 = 8。
将 -3 加到 A[1] 上之后，数组为 [2,-1,3,4]，偶数值之和为 2 + 4 = 6。
将 -4 加到 A[0] 上之后，数组为 [-2,-1,3,4]，偶数值之和为 -2 + 4 = 2。
将 2 加到 A[3] 上之后，数组为 [-2,-1,3,6]，偶数值之和为 -2 + 6 = 4。
 
提示：
1 <= A.length <= 10000
-10000 <= A[i] <= 10000
1 <= queries.length <= 10000
-10000 <= queries[i][0] <= 10000
0 <= queries[i][1] < A.length
```

代码如下：

```
class Solution {
public:
    vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) {
        int sum=0;
        for(auto i:A)
            sum += i%2 ?0:i;
       // cout<<sum<<endl;
        vector<int>res;
        for(auto q:queries)
        {
            if(A[q[1]]%2)
                q[0]%2 ? res.push_back(sum+=A[q[1]]+q[0]) : res.push_back(sum);
            else
                q[0]%2 ?  res.push_back(sum-=A[q[1]]) :res.push_back(sum+=q[0]) ;
            A[q[1]]+=q[0];
            //cout<<q[0]<<A[q[1]]<<endl;
           //cout<<sum<<endl;
        }
        return res;
    }
};
```

> 执行用时 : 360 ms, 在Sum of Even Numbers After Queries的C++提交中击败了38.36% 的用户 内存消耗 : 33.1 MB, 在Sum of Even Numbers After Queries的C++提交中击败了100.00% 的用户


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://dfine.gitbook.io/leetcode/985.sum_of_even_numbers_after_queries.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
