CF1736C2.Good Subarrays (Hard Version)

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is the hard version of this problem. In this version, we have queries. Note that we do not have multiple test cases in this version. You can make hacks only if both versions of the problem are solved.

An array bb of length mm is good if for all ii the ii-th element is greater than or equal to ii. In other words, bb is good if and only if bi≥ib_i \geq i for all ii (1≤i≤m1 \leq i \leq m).

You are given an array aa consisting of nn positive integers, and you are asked qq queries.

In each query, you are given two integers pp and xx (1≤p,x≤n1 \leq p,x \leq n). You have to do ap:=xa_p := x (assign xx to apa_p). In the updated array, find the number of pairs of indices (l,r)(l, r), where 1≤l≤r≤n1 \le l \le r \le n, such that the array [al,al+1,…,ar][a_l, a_{l+1}, \ldots, a_r] is good.

Note that all queries are independent, which means after each query, the initial array aa is restored.

这是本题的困难版本。在该版本中,我们需要处理多个查询。注意,本版本中不包含多组测试数据。只有当本题的两个版本(简单版与困难版)均被解决时,才允许进行 Hack。

长度为 mm 的数组 bb 被称为好数组,当且仅当对所有 ii,第 ii 个元素不小于 ii。换言之,bb 是好数组当且仅当对所有 ii(1≤i≤m1 \leq i \leq m)满足 bi≥ib_i \geq i。

给定一个由 nn 个正整数组成的数组 aa,你需要回答 qq 个查询。

每个查询给出两个整数 pp 和 xx(1≤p,x≤n1 \leq p, x \leq n)。你需要执行赋值操作 ap:=xa_p := x(即将 xx 赋给 apa_p)。在更新后的数组中,求满足条件的下标对 (l,r)(l, r) 的个数,其中 1≤l≤r≤n1 \le l \le r \le n,且子数组 [al,al+1,…,ar][a_l, a_{l+1}, \ldots, a_r] 是好数组。

注意:所有查询相互独立,即每次查询结束后,数组 aa 都会恢复为初始状态。

输入格式

The first line contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n).

The third line contains an integer qq (1≤q≤2⋅1051 \leq q \leq 2 \cdot 10^5) — the number of queries.

Each of the next qq lines contains two integers pjp_j and xjx_j (1≤pj,xj≤n1 \leq p_j, x_j \leq n) – the description of the jj-th query.

第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n)。

第三行包含一个整数 qq(1≤q≤2⋅1051 \leq q \leq 2 \cdot 10^5)—— 查询的数量。

接下来的 qq 行中,每行包含两个整数 pjp_j 和 xjx_j(1≤pj,xj≤n1 \leq p_j, x_j \leq n),表示第 jj 个查询的描述。

输出格式

For each query, print the number of suitable pairs of indices after making the change.

对于每个查询,请输出执行更改后满足条件的索引对的数量。

输入输出样例

  • 输入#1

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

    输出#1

    6
    10
    5
  • 输入#2

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

    输出#2

    7
    9
    8

说明/提示

Here are notes for first example.

In first query, after update a=[2,4,1,4]a=[2,4,1,4]. Now (1,1)(1,1), (2,2)(2,2), (3,3)(3,3), (4,4)(4,4), (1,2)(1,2), and (3,4)(3,4) are suitable pairs.

In second query, after update a=[2,4,3,4]a=[2,4,3,4]. Now all subarrays of aa are good.

In third query, after update a=[2,1,1,4]a=[2,1,1,4]. Now (1,1)(1,1), (2,2)(2,2), (3,3)(3,3), (4,4)(4,4), and (3,4)(3,4) are suitable.

以下是第一个示例的说明。

在第一次查询中,更新后 a=[2,4,1,4]a=[2,4,1,4]。此时,满足条件的数对有 (1,1)(1,1)、(2,2)(2,2)、(3,3)(3,3)、(4,4)(4,4)、(1,2)(1,2) 和 (3,4)(3,4)。

在第二次查询中,更新后 a=[2,4,3,4]a=[2,4,3,4]。此时,aa 的所有子数组均为“好”子数组。

在第三次查询中,更新后 a=[2,1,1,4]a=[2,1,1,4]。此时,满足条件的数对有 (1,1)(1,1)、(2,2)(2,2)、(3,3)(3,3)、(4,4)(4,4) 和 (3,4)(3,4)。

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

首页