AmazonOA #347
Sum After Query Update
Estimated MediumJudge-ready
Topics (AI-labelled)Hashing
Each query old new replaces every element of the current array that equals old with new, then outputs the sum after the change. Queries take effect cumulatively; when old=new or old is not currently present, the array does not change.
Input
The first line contains n q, the second line contains n integers, followed by q lines of old new. On this site 0≤n,q≤200000, and all array and query values are in −10^9..10^9.
Output
Output the sum after each change, one per line in query order, for q lines in total; when q=0, output an empty line.
Example 1
Input:
3 3
1 1 2
1 2
2 2
2 -1
Output:
6
6
-3
Example 2
Input:
1 3
0
3 4
0 0
0 2
Output:
0
0
2
Example 3
Input:
0 0
Output:
Example notes
The outputs of the three public examples are 6 / 6 / -3; 0 / 0 / 2; and an empty line, checked by independent enumeration or direct simulation.
Editorial
Loading solution…