GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Monday 10 October 2022

[Solution] Good Subarrays (Hard Version) Codeforces Solution



C2. Good Subarrays (Hard Version)
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 bii for all i (1im).

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 (1p,xn). You have to do ap:=x (assign x to ap). In the updated array, find the number of pairs of indices (l,r), where 1lrn, 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.

Input

The first line contains a single integer n (1n2105).

The second line contains n integers a1,a2,,an (1ain).

The third line contains an integer q (1q2105) — the number of queries.

Each of the next q lines contains two integers pj and xj (1pj,xjn) – the description of the j-th query.

Output

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


Note

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.

No comments:

Post a Comment