JPMorgan ChaseOA #1
Negate Range Updates on Array
Estimated MediumJudge-ready
Topics (AI-labelled)Prefix & Ranges
A data analyst has an array data of length n representing data for n days. He performs k updates in order; each update is [l, r], meaning every element from index l to r (inclusive, 1-indexed) is negated (x becomes −x).
Find the array after all updates are done.
Input
The first line contains an integer n, and the second line contains n integers data[1..n]. The third line contains an integer k, followed by k lines with two integers l r each.
1≤n≤10^6, 1≤k≤10^6, |data[i]|≤10^9, 1≤l≤r≤n.
Output
Output one line with the n integers of the final array, separated by spaces.
Example 1
Input:
4
1 -4 -5 2
2
2 4
1 2
Output:
-1 -4 5 -2
Example 2
Input:
5
1 2 3 4 5
2
1 3
2 4
Output:
-1 2 3 -4 5
Example 3
Input:
3
0 -7 1000000000
3
1 3
1 3
2 2
Output:
0 7 1000000000
Example notes
Example 1: after the first update [2,4] the array is [1,4,5,−2], and after the second update [1,2] it is [−1,−4,5,−2]. Example 2: after [1,3] it is [−1,−2,−3,4,5], and after [2,4] it is [−1,2,3,−4,5]. Example 3: applying [1,3] twice changes nothing, and then the 2nd element is negated; negating 0 still gives 0.
Editorial
Loading solution…