CF BUDDY
← Problems·

1715C · Monoblock

1700 · combinatorics, data structures, implementation

Problem: Stanley has decided to buy a new desktop PC made by the company "Monoblock", and to solve captcha on their website, he needs to solve the following task.

The awesomeness of an array is the minimum number of blocks of consecutive identical numbers in which the array could be split. For example, the awesomeness of an array

  • [1,1,1][1, 1, 1] is 11;
  • [5,7][5, 7] is 22, as it could be split into blocks [5][5] and [7][7];
  • [1,7,7,7,7,7,7,7,9,9,9,9,9,9,9,9,9][1, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9] is 3, as it could be split into blocks [1][1], [7,7,7,7,7,7,7][7, 7, 7, 7, 7, 7, 7], and [9,9,9,9,9,9,9,9,9][9, 9, 9, 9, 9, 9, 9, 9, 9].

You are given an array aa of length nn. There are mm queries of two integers ii, xx. A query ii, xx means that from now on the ii-th element of the array aa is equal to xx.

After each query print the sum of awesomeness values among all subsegments of array aa. In other words, after each query you need to calculate l=1nr=lng(l,r),\sum\limits_{l = 1}^n \sum\limits_{r = l}^n g(l, r), where g(l,r)g(l, r) is the awesomeness of the array b=[al,al+1,,ar]b = [a_l, a_{l + 1}, \ldots, a_r].

Input Format: In the first line you are given with two integers nn and mm (1n,m1051 \leq n, m \leq 10^5).

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1091 \le a_i \le 10^9) — the array aa.

In the next mm lines you are given the descriptions of queries. Each line contains two integers ii and xx (1in1 \leq i \leq n, 1x1091 \leq x \leq 10^9).

Output Format: Print the answer to each query on a new line.

Note: After the first query aa is equal to [1,2,2,4,5][1, 2, 2, 4, 5], and the answer is 2929 because we can split each of the subsegments the following way:

  1. [1;1][1; 1]: [1][1], 1 block;
  2. [1;2][1; 2]: [1]+[2][1] + [2], 2 blocks;
  3. [1;3][1; 3]: [1]+[2,2][1] + [2, 2], 2 blocks;
  4. [1;4][1; 4]: [1]+[2,2]+[4][1] + [2, 2] + [4], 3 blocks;
  5. [1;5][1; 5]: [1]+[2,2]+[4]+[5][1] + [2, 2] + [4] + [5], 4 blocks;
  6. [2;2][2; 2]: [2][2], 1 block;
  7. [2;3][2; 3]: [2,2][2, 2], 1 block;
  8. [2;4][2; 4]: [2,2]+[4][2, 2] + [4], 2 blocks;
  9. [2;5][2; 5]: [2,2]+[4]+[5][2, 2] + [4] + [5], 3 blocks;
  10. [3;3][3; 3]: [2][2], 1 block;
  11. [3;4][3; 4]: [2]+[4][2] + [4], 2 blocks;
  12. [3;5][3; 5]: [2]+[4]+[5][2] + [4] + [5], 3 blocks;
  13. [4;4][4; 4]: [4][4], 1 block;
  14. [4;5][4; 5]: [4]+[5][4] + [5], 2 blocks;
  15. [5;5][5; 5]: [5][5], 1 block;

Sample Cases

Case 1

Input

5 5
1 2 3 4 5
3 2
4 2
3 1
2 1
2 2

Output

29
23
35
25
35

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback