-
Notifications
You must be signed in to change notification settings - Fork 359
/
s1.cpp
28 lines (28 loc) · 938 Bytes
/
s1.cpp
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
// OJ: https://leetcode.com/problems/sum-of-even-numbers-after-queries/
// Author: github.com/lzl124631x
// Time: O(A + Q)
// Space: O(1)
class Solution {
public:
vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) {
vector<int> ans(queries.size());
int sum = 0;
for (int n : A) {
if (n % 2 == 0) sum += n;
}
for (int i = 0; i < queries.size(); ++i) {
auto q = queries[i];
int val = q[0], index = q[1], prev = A[index];
A[index] += val;
bool wasEven = prev % 2 == 0, even = A[index] % 2 == 0;
if (wasEven && even) {
ans[i] = (sum += val);
} else if (wasEven && !even) {
ans[i] = (sum -= prev);
} else if (!wasEven && even) {
ans[i] = (sum += A[index]);
} else ans[i] = sum;
}
return ans;
}
};