CF1209G2.Into Blocks (hard version)

NOI/NOI+/CTSC

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is a harder version of the problem. In this version q≤200 000q \le 200\,000.

A sequence of integers is called nice if its elements are arranged in blocks like in [3,3,3,4,1,1][3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal.

Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value xx to value yy, you must also change all other elements of value xx into yy as well. For example, for [3,3,1,3,2,1,2][3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 11 to 33 and second 11 to 22. You need to leave 11's untouched or change them to the same value.

You are given a sequence of integers a1,a2,…,ana_1, a_2, \ldots, a_n and qq updates.

Each update is of form "ii xx" — change aia_i to xx. Updates are not independent (the change stays for the future).

Print the difficulty of the initial sequence and of the sequence after every update.

这是一个更难的版本。在此版本中,q≤200 000q \le 200\,000。

若一个整数序列的元素以块状方式排列(例如 [3,3,3,4,1,1][3, 3, 3, 4, 1, 1]),则称其为“优美的”(nice)。形式化地,若序列中两个元素相等,则它们之间所有元素也必须等于该值。

我们定义一个序列的“难度”为:使其变为优美序列所需修改的最少元素个数。但有一个额外约束:若你将任意一个值为 xx 的元素修改为 yy,则所有其他值为 xx 的元素也必须全部修改为 yy。例如,对序列 [3,3,1,3,2,1,2][3, 3, 1, 3, 2, 1, 2],不允许仅将第一个 11 改为 33、而将第二个 11 改为 22;你必须要么保持所有 11 不变,要么将所有 11 都改为同一个值。

给定一个整数序列 a1,a2,…,ana_1, a_2, \ldots, a_n 和 qq 次更新。

每次更新形如 “ii xx” —— 将 aia_i 修改为 xx。这些更新不是相互独立的(每次修改会持续影响后续状态)。

请输出初始序列的难度,以及每次更新后序列的难度。

输入格式

The first line contains integers nn and qq (1≤n≤200 0001 \le n \le 200\,000, 0≤q≤200 0000 \le q \le 200\,000), the length of the sequence and the number of the updates.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤200 0001 \le a_i \le 200\,000), the initial sequence.

Each of the following qq lines contains integers iti_t and xtx_t (1≤it≤n1 \le i_t \le n, 1≤xt≤200 0001 \le x_t \le 200\,000), the position and the new value for this position.

第一行包含两个整数 nn 和 qq(1≤n≤200 0001 \le n \le 200\,000,0≤q≤200 0000 \le q \le 200\,000),分别表示序列的长度和更新操作的次数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤200 0001 \le a_i \le 200\,000),表示初始序列。

接下来的 qq 行中,每行包含两个整数 iti_t 和 xtx_t(1≤it≤n1 \le i_t \le n,1≤xt≤200 0001 \le x_t \le 200\,000),分别表示待更新的位置及其新值。

输出格式

Print q+1q+1 integers, the answer for the initial sequence and the answer after every update.

输出 q+1q+1 个整数,分别为初始序列的答案以及每次更新后的答案。

输入输出样例

  • 输入#1

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

    输出#1

    2
    1
    0
    0
    2
    3
    0

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

首页