To play the game, Bessie chooses some interval (say, the iith interval) and her cousin Elsie chooses some interval (say, the jjth interval, possibly the same as Bessie's interval). Given some value kk, they win if ai+aj≤k≤bi+bjai+aj≤k≤bi+bj.
For every value of kk in the range 0…2M0…2M, please count the number of ordered pairs (i,j)(i,j) for which Bessie and Elsie can win the game.
2 5 1 3 2 5
0 0 1 3 4 4 4 3 3 1 1
In this example, for just k=3k=3, there are three ordered pairs that will allow Bessie and Elie to win: (1,1)(1,1), (1,2),(1,2), and (2,1)(2,1).
Note that output values might be too large to fit into a 32-bit integer, so you may want to use 64-bit integers (e.g., "long long" ints in C or C++).
Problem credits: Benjamin Qi
2 5
1 3
2 5
0
0
1
3
4
4
4
3
3
1
1