CF1715C.Monoblock

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 $$\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].

斯坦利决定购买一台由“Monoblock”公司生产的新台式电脑,为了在该公司网站上通过验证码,他需要解决以下问题。

一个数组的“酷炫值”(awesomeness)是指将该数组划分为若干个连续相同数字组成的块时,所需块数的最小值。例如:

  • 数组 [1,1,1][1, 1, 1] 的酷炫值为 11;
  • 数组 [5,7][5, 7] 的酷炫值为 22,因为它可被划分为块 [5][5] 和 [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] 的酷炫值为 33,因为它可被划分为块 [1][1]、[7,7,7,7,7,7,7][7, 7, 7, 7, 7, 7, 7] 和 [9,9,9,9,9,9,9,9,9][9, 9, 9, 9, 9, 9, 9, 9, 9]。

给定一个长度为 nn 的数组 aa。接下来有 mm 个查询,每个查询包含两个整数 ii 和 xx。查询 ii, xx 表示从此时起,将数组 aa 的第 ii 个元素修改为 xx。

每次查询后,请输出数组 aa 的所有子段(subsegment)的酷炫值之和。换言之,每次查询后你需要计算

∑l=1n∑r=lng(l,r),\sum\limits_{l = 1}^n \sum\limits_{r = l}^n g(l, r),

其中 g(l,r)g(l, r) 表示子数组 b=[al,al+1,…,ar]b = [a_l, a_{l + 1}, \ldots, a_r] 的酷炫值。

输入格式

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

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \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 (1≤i≤n1 \leq i \leq n, 1≤x≤1091 \leq x \leq 10^9).

第一行给出两个整数 nn 和 mm(1≤n,m≤1051 \leq n, m \leq 10^5)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \le a_i \le 10^9)—— 数组 aa。

接下来 mm 行,每行给出一个查询的描述。每行包含两个整数 ii 和 xx(1≤i≤n1 \leq i \leq n, 1≤x≤1091 \leq x \leq 10^9)。

输出格式

Print the answer to each query on a new line.

将每个查询的答案打印在新的一行上。

输入输出样例

  • 输入#1

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

    输出#1

    29
    23
    35
    25
    35

说明/提示

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;

which is 1+2+2+3+4+1+1+2+3+1+2+3+1+2+1=291 + 2 + 2 + 3 + 4 + 1 + 1 + 2 + 3 + 1 + 2 + 3 + 1 + 2 + 1 = 29 in total.

第一次查询后,aa 变为 [1,2,2,4,5][1, 2, 2, 4, 5],答案为 2929,因为我们可以将每个子段按如下方式划分:

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

总和为 1+2+2+3+4+1+1+2+3+1+2+3+1+2+1=291 + 2 + 2 + 3 + 4 + 1 + 1 + 2 + 3 + 1 + 2 + 3 + 1 + 2 + 1 = 29。

输入解题思路,AI测评打分。不知道怎么写?

首页