# 1 题目描述

（此处给定的 index = queries[i][1] 是从 0 开始的索引，每次查询都会永久修改数组 A。）

# 3 解题提示

1 <= A.length <= 10000
-10000 <= A[i] <= 10000
1 <= queries.length <= 10000
-10000 <= queries[i][0] <= 10000
0 <= queries[i][1] < A.length

# 4 源码详解（C++）

class Solution {
public:
vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) {
vector<int> res ;
int sum = 0 ;
for( int i = 0 ; i < A.size() ; i ++ )
{
if( A[i] % 2 == 0)
{
sum += A[i];
}
}
for(int i=0;i<queries.size();i++)
{
if( A[queries[i][1]] % 2 == 0 )
{
sum -= A[queries[i][1]] ;
}
A[queries[i][1]] += queries[i][0] ;
if( A[queries[i][1]] % 2 == 0 )
{
sum += A[queries[i][1]] ;
}
res.push_back(sum) ;
}
return res ;
}
};


# 5 超时代码一（C++）

class Solution {
public:
int getDoubleSum(vector<int>& arr)
{
int res = 0 ;
for ( int i = 0 ; i < arr.size() ; i ++ )
{
if ( arr[i] % 2 == 0 )
{
res = res + arr[i] ;
}
}
return res ;
}

vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) {
vector<int> res ;
for ( int i = 0 ; i < queries.size() ; i ++ )
{
A[queries[i][1]] = A[queries[i][1]] + queries[i][0] ;
res.push_back(getDoubleSum(A)) ;
}
return res ;
}
};


# 6 超时代码二（C++）

class Solution {
public:
vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) {
vector<int> res ;
for ( int i = 0 ; i < queries.size() ; i ++ )
{
int sum = 0 ;
A[queries[i][1]] = A[queries[i][1]] + queries[i][0] ;
for ( int j = 0 ; j < A.size() ; j ++ )
{
if ( A[j] % 2 == 0 )
{
sum = sum + A[j] ;
}
}
res.push_back( sum ) ;
}
return res ;
}
};

03-03 85