Spoj question help

question link: https://www.spoj.com/problems/XXXXXXXX/
solution link : https://ideone.com/13oYrG
my solution is getting WA at 17 or 18th test case at spoj.
can somebody plz tell the error?

I think you should avoid such a huge template and macros when you ask someone to check your code, it’s not easy to follow. I’ve not tested your solution but…you unordered_map “mp” stores just one update for each x, what if there are more updates for the same x? I think the error can come from that.

I am just using mp to store the previous value of array at that index which gets updated. All the updates are stored in upd . And yes i will remove the template from next time

ok. I see you first read in this order n, q, a[]; but the input is n,a[],q. Also, your comp function uses <=, you should avoid that; sorting without strict weak ordering could lead to unexpected results

now runtime on approx 20th
updated solution:

the numbers can be negative or larger than 1000001. The lines for example in the add function:

ll ele=a[pos];

will produce runtime error when ele < 0 or ele > 1000000

ohh so what should i do because i need to check the frequency of each element

you can use a map to do that

ohok give me some time i will do the req changes and tell you

WA still on 20

what I would do is write a simple brute force solution to compare results on small test cases. That can help to find the error in the main solution. For example, try this test case
-3 7 -8 -1 -4 5
R 6 7
Q 1 4
R 2 -1
Q 4 6

The solution should be -5 2 but your code is giving -5 3

updated solution :https://ideone.com/gh52Zm
now time limit exceeded after 20
it should be solved with mo with updates right?