CF1736C2.Good Subarrays (Hard Version)
普及/提高-
通过率:0%
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 b of length m is good if for all i the i -th element is greater than or equal to i . In other words, b is good if and only if bi≥i for all i ( 1≤i≤m ).
You are given an array a consisting of n positive integers, and you are asked q queries.
In each query, you are given two integers p and x ( 1≤p,x≤n ). You have to do ap:=x (assign x to ap ). In the updated array, find the number of pairs of indices (l,r) , where 1≤l≤r≤n , such that the array [al,al+1,…,ar] is good.
Note that all queries are independent, which means after each query, the initial array a is restored.
输入格式
The first line contains a single integer n ( 1≤n≤2⋅105 ).
The second line contains n integers a1,a2,…,an ( 1≤ai≤n ).
The third line contains an integer q ( 1≤q≤2⋅105 ) — the number of queries.
Each of the next q lines contains two integers pj and xj ( 1≤pj,xj≤n ) – the description of the j -th query.
输出格式
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] . Now (1,1) , (2,2) , (3,3) , (4,4) , (1,2) , and (3,4) are suitable pairs.
In second query, after update a=[2,4,3,4] . Now all subarrays of a are good.
In third query, after update a=[2,1,1,4] . Now (1,1) , (2,2) , (3,3) , (4,4) , and (3,4) are suitable.