Problem:
Score: 500 points

Problem Statement

You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.

There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is A_i.

According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.

The absolute difference of their attendee numbers is equal to the sum of their heights.

There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?

P.S.: We cannot let you know the secret.

Constraints

All values in input are integers.

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the number of pairs satisfying the condition.

Sample Input 1

6
2 3 3 1 3 1

Sample Output 1

3

A_1 + A_4 = 3, so the pair of Attendee 1 and 4 satisfy the condition.

A_2 + A_6 = 4, so the pair of Attendee 2 and 6 satisfy the condition.

A_4 + A_6 = 2, so the pair of Attendee 4 and 6 satisfy the condition.

No other pair satisfies the condition, so you should print 3.

Sample Input 2

6
5 2 4 2 8 8

Sample Output 2

0

No pair satisfies the condition, so you should print 0.

Sample Input 3

32
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5

Sample Output 3

22

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

using static Atcoder_Template_NetCore3.MyBitUtil;
using static Atcoder_Template_NetCore3.MyInputUtil;
using static System.Console;

namespace Atcoder_Template_NetCore3
{
    class Program
    {
        static void Main()
        {
            var personCount = ReadInput<long>();
            var height = ReadArray<long>();

            var kvp = new Dictionary<long, int>();
            for (int i = 0; i < height.Length; i++)
            {
                var diff = i + 1 - height[i];
                if (kvp.ContainsKey(diff))
                {
                    kvp[diff]++;
                }
                else
                {
                    kvp.Add(diff, 1);
                }
            }

            var ans = 0l;
            for (int i = 0; i < personCount; i++)
            {
                var sum = i + 1 + height[i];
                if (kvp.TryGetValue(sum,out var value))
                {
                    ans += value;
                }

                var diff = i + 1 - height[i];
                kvp[diff]--;
            }

            WriteLine(ans);
        }
    }

    /// <summary>
    /// Bit取得、およびbit全探索を提供します。
    /// </summary>
    internal static class MyBitUtil
    {
        /// <summary>
        /// 指定の桁のビットを取得します。
        /// </summary>
        /// <param name="count">取得する桁。</param>
        /// <returns></returns>
        public static int GetBit(this int num, int count)
        {
            var x = num >> (count - 1);
            const int mask = 1;
            return x & mask;
        }

        /// <summary>
        /// 各ビットの値を <see cref="int"/> 配列として取得します。
        /// 下位の桁が配列の前に来ます。
        /// </summary>
        public static int[] ToBitArray(this int num)
        {
            const int size = sizeof(int) * 8;
            var array = new int[size];
            for (var i = 0; i < size; i++)
            {
                array[i] = num.GetBit(i + 1);
            }
            return array;
        }

        /// <summary>
        /// 各ビットを真偽値として解釈した結果を <see cref="bool"/> 配列として取得します。
        /// 下位の桁が配列の前に来ます。
        /// </summary>
        public static bool[] ToBoolArray(this int num)
        {
            const int size = sizeof(int) * 8;
            var array = new bool[size];
            for (var i = 0; i < size; i++)
            {
                array[i] = num.GetBit(i + 1) == 1;
            }
            return array;
        }

        /// <summary>
        /// Bit全探索を行います。
        /// </summary>
        /// <param name="count">対象とする要素の個数。30以下である必要があります。</param>
        /// <param name="action">各パターンに対して行う処理</param>
        public static void BitAllSearch(int count, Action<bool[]> action)
        {
            var max = 1 << count;
            for (var i = 0; i < max; i++)
            {
                action.Invoke(i.ToBoolArray());
            }
        }

        /// <summary>
        /// 指定したコレクションに対してBit全探索を行います。
        /// </summary>
        /// <param name="list">対象とする集合。要素数30以下である必要があります。</param>
        /// <param name="action">各パターンに対して行う処理</param>
        public static IEnumerable<IEnumerable<T>> GetAllPatterns<T>(IReadOnlyList<T> list)
        {
            var max = 1 << list.Count;
            for (var i = 0; i < max; i++)
            {
                var usePattern = i.ToBoolArray();
                IEnumerable<T> query = list.Where((item, j) => usePattern[j]);
                yield return query;
            }
        }
    }

    /// <summary>
    /// 標準入力の高機能な読み取りを提供します。
    /// </summary>
    internal static class MyInputUtil
    {
        private const char separator = ' ';

        /// <summary>
        /// 標準入力から入力を1つ指定の型に変換して読み取ります。
        /// </summary>
        /// <typeparam name="T">変換先として指定する型。</typeparam>
        /// <param name="converter">変換に使用する関数。<c>null</c>の場合は自動決定を試みます。</param>
        /// <returns></returns>
        public static T ReadInput<T>(Func<string, T> converter = null)
        {
            if (converter is null)
            {
                converter = GetConverter<T>();
            }
            return converter.Invoke(Console.ReadLine());
        }

        #region 一定個数の入力の読み取り
        // 用法は ReadInput<T> と共通です。
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T> ReadInput2<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T> ReadInput3<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T, T> ReadInput4<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]), converter.Invoke(input[3]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T, T, T> ReadInput5<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]), converter.Invoke(input[3]), converter.Invoke(input[4]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T, T, T, T> ReadInput6<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]), converter.Invoke(input[3]), converter.Invoke(input[4]), converter.Invoke(input[5]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T, T, T, T, T> ReadInput7<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]), converter.Invoke(input[3]), converter.Invoke(input[4]), converter.Invoke(input[5]), converter.Invoke(input[6]));
        }
        #endregion

        /// <summary>
        /// 標準入力から配列を読み取ります。
        /// </summary>
        /// <returns></returns>
        public static T[] ReadArray<T>(Func<string, T> converter = null)
        {
            var str = ReadLine().Split(separator);
            if (converter is null)
            {
                converter = GetConverter<T>();
            }
            return str.Select(s => converter.Invoke(s)).ToArray();
        }

        #region 変換用メソッド
        /// <summary>
        /// 文字列を指定の型に変換します。
        /// </summary>
        /// <typeparam name="T">変換先の型。すべての組み込み型がサポートされます。</typeparam>
        /// <param name="str">変換元の文字列</param>
        /// <exception cref="NotSupportedException">指定の型引数がサポートされない場合にスローされる例外。</exception>
        /// <remarks>大量の値を処理する場合、このメソッドは低速です。<see cref="ConvertTo{T}(string[])"/> の使用を検討してください。</remarks>
        public static T ConvertTo<T>(string str)
        {
            return GetConverter<T>().Invoke(str);
        }

        /// <summary>
        /// 文字列配列を指定の型の配列に変換します。
        /// </summary>
        /// <typeparam name="T">変換先の型。すべての組み込み型がサポートされます。</typeparam>
        /// <param name="str">変換元の文字列配列</param>
        /// <exception cref="NotSupportedException">指定の型引数がサポートされない場合にスローされる例外。</exception>
        public static T[] ConvertTo<T>(string[] str)
        {
            var conveter = GetConverter<T>();
            var array = new T[str.Length];
            for (var i = 0; i < str.Length; i++)
            {
                array[i] = conveter.Invoke(str[i]);
            }
            return array;
        }

        private static Func<string, T> GetConverter<T>() => GetConverterOrDefault<T>() ?? throw new NotSupportedException("この型に対する変換関数の取得はサポートされていません");
        private static bool TryGetConverter<T>(out Func<string, T> func)
        {
            func = GetConverterOrDefault<T>();
            return !(func is null);
        }

        private static Func<string, T> GetConverterOrDefault<T>()
        {
            Type type = typeof(T);
            Func<string, T> conveter;

            // 出現頻度が高そうなものを上
            if (type == typeof(int))
            {
                conveter = str => (T)(object)int.Parse(str);
            }
            else if (type == typeof(long))
            {
                conveter = str => (T)(object)long.Parse(str);
            }
            else if (type == typeof(double))
            {
                conveter = str => (T)(object)double.Parse(str);
            }
            else if (type == typeof(decimal))
            {
                conveter = str => (T)(object)decimal.Parse(str);
            }
            else if (type == typeof(float))
            {
                conveter = str => (T)(object)float.Parse(str);
            }
            else if (type == typeof(char))
            {
                conveter = str => (T)(object)str[0];
            }
            else if (type == typeof(short))
            {
                conveter = str => (T)(object)short.Parse(str);
            }
            else if (type == typeof(byte))
            {
                conveter = str => (T)(object)byte.Parse(str);
            }
            else if (type == typeof(bool))
            {
                conveter = str => (T)(object)bool.Parse(str);
            }
            else if (type == typeof(ulong))
            {
                conveter = str => (T)(object)ulong.Parse(str);
            }
            else if (type == typeof(uint))
            {
                conveter = str => (T)(object)uint.Parse(str);
            }
            else if (type == typeof(ushort))
            {
                conveter = str => (T)(object)ushort.Parse(str);
            }
            else if (type == typeof(sbyte))
            {
                conveter = str => (T)(object)sbyte.Parse(str);
            }
            else if (type == typeof(string))
            {
                conveter = str => (T)(object)str;
            }
            else if (type == typeof(object))
            {
                conveter = str => (T)(object)str;
            }
            else
            {
                conveter = null;
            }

            return conveter;
        }
        #endregion
    }
}