UPDATEIT - Update the array !

You have an array containing n elements initially all 0. You need to do a number of update operations on it. In each update you specify l, r and val which are the starting index, ending index and value to be added. After each update, you add the 'val' to all elements from index l to r. After 'u' updates are over, there will be q queries each containing an index for which you have to print the element at that index.


First line consists of t, the number of test cases. (1 <= t <= 10)

Each test case consists of "n u",number of elements in the array and the number of update operations, in the first line (1 <= n <= 10000 and 1 <= u <= 100000)

Then follow u lines each of the format "l r val" (0 <= l,r < n, 0 <= val <=10000)

Next line contains q, the number of queries. (1 <= q <= 10000)

Next q lines contain an index (0 <= index < n)


For each test case, output the answers to the corresponding queries in separate lines.


5 3
0 1 7
2 4 6
1 3 2


hide comments
jmr99: 2018-06-19 17:09:46

bit does the job simple implementation .

rajcoolaryan: 2018-06-19 12:37:03

5 lines of code and 5 TLE due to cin cout..

ankit1cool: 2018-06-18 21:33:42

No BIT or segment tree needed

shithole: 2018-05-09 08:14:53

how to read and write to standard input output (console) in Javascript (Spidermonkey , Rhino). I tried using readline() but it does not work.

dsri_99: 2017-12-24 07:48:56

Last edit: 2017-12-31 12:32:31
storyteller: 2017-12-22 21:44:37

Difference array technique gives overall O(n) .
O(1) ->For each update
O(n) -> Processing
O(1) ->Query

shubham_04_04: 2017-12-19 17:32:46

I had written a code of complexity O(u+n+q) but still getting tle in java. Can anyone tell me why?

kaneki_04: 2017-11-18 13:32:59

Use the fact that updates occur before queries and you can do it without BIT ;)

Ravi Upadhyay: 2017-11-08 08:35:18

can be solved without using segment tree or BIT.
O(q+n) simple solution is possible.

jha_gaurav98: 2017-11-08 04:27:01

Don't forget to use scanf and printf

Added by:Pandian
Time limit:0.118s-0.324s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64