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



Sample input:
6
2 3 3 1 3 1


Sample output:
3

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

##### 割り算の切り上げ #####
# tmp = -(-4 // 3)

##### dict の for文 #####
# for k, v in d.items():
#     print(k, v)

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()
	A = inputList()

	mp = {}
	ans = 0
	for i in range(N):
		ii = i+1
		tmp = ii - A[i]
		if tmp in mp:
			ans += mp[tmp]
		tmp = ii + A[i]
		if tmp in mp:
			mp[tmp] += 1
		else:
			mp[tmp] = 1

	print(ans)


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# N 個のボールを K グループに分ける場合のパターン数
def sunuke(N, K, mod=10**9+7):
	if N < K or K-1 < 0:
		return 0
	else:
		return combination(N-1, K-1, mod)

# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)


class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# dfs のサンプル
def dfs(graph,parent,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input().strip())
L = list(map(int, input().strip().split()))
seen=Counter()
ans=0
for j in range(n):
    # if j-L[j] in seen:
    ans+=seen[j-L[j]]
    seen[L[j]+j]+=1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

N = int(input())
A = list(map(int, input().split()))
count=0
L = [0]*N
R = [0]*N
N2=2*N
hash_L=[0]*N2
hash_R=[0]*N2

sheet_L=[0]*N2
sheet_R=[0]*N2
for i in range(N):
    L[i]=A[i]+i+1
    if L[i]<N2:
        if sheet_L[L[i]]==0:
            hash_L[L[i]]=L[i]
            sheet_L[L[i]]=1
        else:
            sheet_L[L[i]]=sheet_L[L[i]]+1

    R[i]=-A[i]+i+1
    if R[i]>0:
        if sheet_R[R[i]]==0:
            hash_R[R[i]]=R[i]
            sheet_R[R[i]]=1
        else:
            sheet_R[R[i]]=sheet_R[R[i]]+1


for i in range(2*N):
    if not hash_L[i]==0:
        if hash_L[i]==hash_R[i]:
            count=count+sheet_L[i]*sheet_R[i]
#print("{}".format(hash_L))
#print("{}".format(hash_R))
print("{}".format(count))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))

result=0

D={}

for j in range(N):
    if j+A[j] in D:
        D[j+A[j]] += 1
    else:
        D[j+A[j]]=1

for j in range(N):
    if j-A[j] in D:
        result += D[j-A[j]]

print(result)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
A = list(map(int, input().split(" ")))

count = defaultdict(lambda: 0)
ans = 0
for i in range(N):
    ans += count[i - A[i]]
    count[i + A[i]] += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
A=[int(i) for i in input().split()]
import collections
D=collections.defaultdict(int)
ans=0
for i in range(n):
  D[i - A[i]]+=1
for i in range(n):
  ans+=D[A[i]+i]
print(ans)

Submission status: Accepted

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
A=[int(i) for i in input().split()]
import collections
D=collections.defaultdict(int)
ans=0
for i in range(n):
  D[A[i]+i]+=1
for i in range(n):
  ans+=D[i-A[i]]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split(" ")))

count = {}
ans = 0
for i in range(N):
    if (i - A[i]) in count:
        ans += count[i - A[i]]

    if (i + A[i]) in count:
        count[i + A[i]] += 1
    else:
        count[i + A[i]] = 1
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30
from collections import Counter, defaultdict

def main():
    n = int(input().strip())
    v = list(map(int, input().strip().split()))
    a = [0] * n
    b = [0] * n
    for i in range(n):
        a[i] = i + v[i]
        b[i] = i - v[i]

    b = Counter(b)
    ans = 0
    for c in a:
        ans += b.get(c, 0)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
alist = list(map(int, input().split()))

ans = 0
dic = {}
for i, a in enumerate(alist):
  if dic.get(str(i-a)) is not None:
    ans += dic[str(i-a)]
  if dic.get(str(i+a)) is not None:
    dic[str(i+a)] += 1
  else:
    dic[str(i+a)] = 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a= list(map(int,input().split()))

b=[]
c=[]
# enumerate()
for n,i in enumerate(a,1):
    b.append(n-i)
    c.append(n+i)

# print(b)
col = Counter(c)
ans=0

for bb in b:
    ans += col[bb]

print(ans)

Submission status: Accepted

Solution code:
def Main(N, A):
    d = {}
    d2 = {}
    ans = 0
    for i in range(N):
        p = i+A[i]
        if p not in d:
            d[p] = []
        d[p].append(i)
    for i in range(N):
        q = i-A[i]
        if q not in d2:
            d2[q] = []
        d2[q].append(i)
    for k, v in d.items():
        if len(v) > 0 and k in d2:
            ans += len(v) * len(d2[k])
    return ans
        
    
def main():
    N = int(input())
    A = list(map(int, input().split()))
    print(Main(N, A))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
L, R = {}, {}
for n in range(N):
    L[n + A[n]] = L.get(n + A[n], 0) + 1
    R[n - A[n]] = R.get(n - A[n], 0) + 1
print(sum([R.get(k, 0) * v for k, v in L.items()]))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
#i_list = [a + i + 1 for i, a in enumerate(A)]

ans = 0
mp = {}
for i in range(N):
    i_kou = i + 1 + A[i]
    if i_kou in mp:
        mp[i_kou] += 1
    else:
        mp[i_kou] = 1
    j_kou = i + 1 - A[i]
    if j_kou in mp:
        ans += mp[j_kou]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
la = [int(i) for i in input().split()]

ia = {}
pair = 0
for j, a in enumerate(la):
  ia[j + a] = ia.get(j + a, 0) + 1

for j, a in enumerate(la): 
  pair += ia.get(j - a, 0)

print(pair)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

up_lift = [0]*N
down_lift = [0]*N
for i in range(N):
    if i + A[i] < N:
        up_lift[i + A[i]] += 1
    if i - A[i] >= 0:
        down_lift[i - A[i]] += 1

print(sum([up_lift[i] * down_lift[i] for i in range(N)]))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
up_lift = [0]*N
down_lift = [0]*N
for i in range(N):
    if i + A[i] < N:
        up_lift[i + A[i]] += 1
    if i - A[i] >= 0:
        down_lift[i - A[i]] += 1


ans = 0
for i in range(N):
    ans += up_lift[i] * down_lift[i]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import numpy as np

N = int(input())
A = [int(_) for _ in input().split()]

# A_n = [a + b + 1 for a, b in enumerate(A)]

# ret = 0
# for n in range(len(A)):
#     #print(A_n[:n], n + 1 - A[n])
#     ret += A_n[:n].count(n + 1 - A[n])
# print(ret)

nmax = int(2e5 + 1)
X = [0] * nmax
Y = [0] * nmax
for n, a in enumerate(A):
    x = n - a
    y = n + a
    if x >= 0 and x < nmax:
        X[x] += 1
    if y >= 0 and y < nmax:
        Y[y] += 1
ret = 0
for x, y in zip(X, Y):
    ret += x * y
print(ret)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))

ans = 0

R = defaultdict(int) 
#R= defaultdict(lambda:0) 

for i ,Ai in enumerate(A,1):
  R[i+Ai]+=1

L = defaultdict(int) 
for j ,Aj in enumerate(A,1):
  L[j-Aj]+=1

for i in R:
  ans+=R.get(i,0)*L.get(i,0)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))

r = []
l = []

for i,j in enumerate(a):
    r.append(i+j)
    l.append(i-j)
    
ans = 0
#Counterで{'要素':'個数'}の連想配列(辞書型)をつくり、そこから取り出す
b = Counter(r)
for i in l:
    ans += b.get(i,0)
    
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [Scanner.string() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [Scanner.int() for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    N = Scanner.int()
    A = Scanner.map_int()
    L = defaultdict(int)
    for i in range(N):
        L[(i + 1 + A[i])] += 1
    R = defaultdict(int)
    for i in range(N):
        R[i + 1 - A[i]] += 1
    ans = 0
    for k, v in L.items():
        ans += v * R[k]
    print(ans)


def main():
    # sys.stdin = open("sample.txt")
    solve()


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
A = list(map(int,input().split()))

r = defaultdict(int)
l = defaultdict(int)

for i,a in enumerate(A):
    r[i-a] += 1
    l[i+a] += 1

ans = 0
for i in r.keys():
    ans += r[i]*l[i]

print(ans)

Submission status: Accepted

Solution code:
#166E
import bisect

N = int(input()) #2* 10**5
A_list = [0] + [int(e) for e in input().split()]

#まともに計算すると ≒ 2 * 10**10　でTLE

#参加者i, j (i < j) について，条件は以下のように書けます:
#j − i = Ai + Aj
#これを式変形すると，以下のようになります:
#i + Ai = j − Aj
#そこで，Li = i + Ai,Ri = i − Ai と定義すると，条件はLi = Rj と言い換えられます．
#したがって，とり得る値X を全て試し,
#Li = X
#Rj = X
#を満たす(i, j) の組を求めればいいですが，上と下の条件は独立なので，たとえば連想配列を使えば簡単に求まります．

Rj = [j - A_list[j] for j in range(1,N+1)]

X_Li_dict = dict()
for i in range(1,N+1):
    X = i + A_list[i]
    if X not in X_Li_dict:
        X_Li_dict[X] = set()

    X_Li_dict[X].add(i)

X_Rj_dict = dict()
for j in range(1,N+1):
    X = j - A_list[j]
    if X not in X_Rj_dict:
        X_Rj_dict[X] = set()
        
    X_Rj_dict[X].add(j)

ans = 0

for X_Li in X_Li_dict.items():
    X = X_Li[0]
    i_set = X_Li[1]
    #print("$$$$$$$$$$$$$$$$$$$$$")
    #print("X:",X,",i:",i_set)

    if X in X_Rj_dict:
        j_list = list(X_Rj_dict[X])
        j_list.sort(reverse=False)
        #print("j:",j_list)
        for i in i_set:
            ans_add = len(j_list) - bisect.bisect_right(j_list,i)
            #print(i,":",ans_add)
            ans += ans_add

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

n=int(input())
A=list(map(int,input().split()))
B=defaultdict(int)
res=0

for i in range(n):
	B[A[i]+i]+=1
	if i-A[i] in B:
		res+=B[i-A[i]]

print(res)

Submission status: Accepted

Solution code:
import numpy as np
import sys
import math
read = sys.stdin.readline

N = int(read().rstrip())
A = list(map(int,read().rstrip().split()))
da = []
for i , a in enumerate(A):
    da.append([i,a])

dR = {}
dL = {}

ans = 0
for d in reversed(da):
    r = d[0] + d[1] + 1
    l = d[0] - d[1] + 1 
    if r in dR:
        dR[r] += 1
    else:
        dR[r] = 1

    if r in dL:
        ans += dL[r]

    if l in dL :
        dL[l] += 1
    else:
        dL[l] = 1  

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys
import math
import collections
read = sys.stdin.readline

N = int(read().rstrip())
A = list(map(int,read().rstrip().split()))
da = []
for i , a in enumerate(A):
    da.append([i,a])

dR = collections.defaultdict(int)
dL = collections.defaultdict(int)

ans = 0
for d in reversed(da):
    r = d[0] + d[1] + 1
    l = d[0] - d[1] + 1 

    dR[r] += 1

    ans += dL[r]

    dL[l] += 1  

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))

left = defaultdict(int)
for i in range(n):
    left[i + a[i]] += 1

ans = 0
for i in range(n):
    sub = i - a[i]
    if sub in left:
        ans += left[sub]

print(ans)

Submission status: Accepted

Solution code:
## coding: UTF-8

N = int(input())
A = [0]
A = A + list(map(int,input().split()))
#print(A)

i_minus_Ai = [0] * (N+1)
j_plus_Aj = [0] * (N+1)

for i in range(1, N+1):
    minus = i - A[i]
    plus = i + A[i]
    #print(i,A[i],minus, plus)
    if(2 <= minus and minus <= N):
        i_minus_Ai[minus] += 1
    if(2 <= plus and plus <= N):
        j_plus_Aj[plus] += 1
#print(i_minus_Ai)
#print(j_plus_Aj)
ans = 0
for i in range(N+1):
    ans += i_minus_Ai[i] * j_plus_Aj[i]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools


def input_int_list_with_length(length=None):
    if length == None:
        length = int(input())
    return list(map(int, input().split()))[:length]


def solve(N, A):
    ans = 0
    L = {}
    for i in range(N):
        l = i+A[i]
        if l in L:
            L[l] += 1
        else:
            L[l] = 1
    for i in range(N):
        r = i-A[i]
        if r in L:
            ans += L[r]

    print(ans)


def main():
    A = input_int_list_with_length()
    solve(len(A), A)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
import collections
ans = 0
dic = collections.defaultdict(int)
for i in range(1, n + 1):
    ans += dic[i - a[i - 1]]
    dic[i + a[i - 1]] += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))

i_plus_Ai = [0]*n
j_minus_Aj = [0]*n
for i in range(n):
    if 0 <= i+a[i] < n:
        i_plus_Ai[i+a[i]] += 1
    if 0 <= i-a[i] < n:
        j_minus_Aj[i-a[i]] += 1

cnt = 0
for i, j in zip(i_plus_Ai, j_minus_Aj):
    cnt += i*j
print(cnt)

Submission status: Accepted

Solution code:
#input
import sys
imput = sys.stdin.readline
def main():
    
    from collections import Counter
    N = int(input())
    A = list(map(int, input().split()))

#output
    B = [i-a for i, a in enumerate(A) if i-a > 0]    
    C = [i+a for i, a in enumerate(A)]
    D = Counter(C)

    answer = 0
    for b in B:
        answer += D[b]

    print(answer)    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

mp = [0] * n

ans = 0

for i in range(n):
    if i-a[i] >= 0:
        ans += mp[i-a[i]]
    if n > a[i]+i:
        mp[a[i]+i] += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
heights = list(map(int, input().split()))

diffMax = 2 * max(heights)
diffMin = 2 * min(heights)

cnt = 0

arrSize = min([max([i + heights[i] for i in range(len(heights))]),max([i - heights[i] for i in range(len(heights))])]) + 1

seppenPlus = [0] * arrSize
seppenMinus = [0] * arrSize


for i in range(len(heights)):
    seppenPlusNum = i + heights[i]
    seppenMinusNum = i - heights[i]

    if seppenPlusNum < arrSize:
    	seppenPlus[seppenPlusNum] += 1

    if seppenMinusNum >= 0:
        seppenMinus[seppenMinusNum] += 1

for i in range(len(seppenPlus)):
    cnt += seppenPlus[i] * seppenMinus[i]

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
num = {}
cnt = 0
for i in range(n):
    i_minus_Ai = i-a[i]
    if i_minus_Ai in num:
        cnt += num[i_minus_Ai]

    i_plus_Ai = i+a[i]
    if i_plus_Ai in num:
        num[i_plus_Ai] += 1
    else:
        num[i_plus_Ai] = 1
print(cnt)

Submission status: Accepted

Solution code:
import numpy as np
import sys
import math
read = sys.stdin.readline

N = int(read().rstrip())
A = list(map(int,read().rstrip().split()))
da = []
for i , a in enumerate(A):
    da.append([i,a])

dR = {}
dL = {}

ans = 0
for d in reversed(da):
    r = d[0] + d[1] + 1
    l = d[0] - d[1] + 1 
    if r in dR:
        dR[r] += 1
    else:
        dR[r] = 1

    if r in dL:
        ans += dL[r]

    if l in dL :
        dL[l] += 1
    else:
        dL[l] = 1  

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(x) for x in input().split()]

B = {}
for i, a in enumerate(A):
    aa1 = i + 1 - a
    if aa1 > 0:
        if aa1 not in B:
            B[aa1] = []
        B[aa1].append((i + 1, a))

count = 0
for i, a in enumerate(A):
    aa1 = i + 1 + a
    if aa1 in B:
        count += len(B[aa1])
print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split(" ")))

b = [j - i for i, j in enumerate(a, start=1)]
# c = [-j - i for i, j in enumerate(a, start=1)]
count = {}
for i, j in enumerate(a, start=1):
    num = -i-j
    if num not in count:
        count[num] = 0
    count[num] += 1
result = sum([count.get(i, 0) for i in b])
print(result)

Submission status: Accepted

Solution code:
from math import sqrt

person = int(input())
longs = list(map(int, input().split(" ")))

longsi_dic = {}
for i in range(len(longs)):
    if str(i + longs[i]) not in longsi_dic:
        longsi_dic[str(i + longs[i])] = 0
    longsi_dic[str(i + longs[i])] += 1

longsj_dic = {}
for j in range(len(longs)):
    if str(j - longs[j]) not in longsj_dic:
        longsj_dic[str(j - longs[j])] = 0
    longsj_dic[str(j - longs[j])] += 1

sum = 0
for val, sumi in longsi_dic.items():
    if val not in longsj_dic:
        continue
    sum += int(sumi) * int(longsj_dic[val])

print(sum)

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    A=[int(_) for _ in input().split()]
    D={}
    ans=0
    for i,a in enumerate(A):
        if i-a in D:
            ans += D[i-a]
        if i+a in D:
            D[i+a] += 1
        else:
            D[i+a] = 1
    print(ans)

main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(v) for v in input().split()]
L, R = {}, {}
for i,a in enumerate(A):
  l,r = i+a,i-a
  if l not in L: L[l]=0
  if r not in R: R[r]=0
  L[l]+=1
  R[r]+=1

count = 0
for X in L:
  if X not in R: continue
  count += L[X]*R[X]
print(count)

Submission status: Accepted

Solution code:
from itertools import combinations

def detect_v1(N, A):
    count = 0
    for n, m in combinations(range(len(A)), 2):
        if abs(n - m) == A[n] + A[m]:
            count += 1

    # print(count)
    return count


# i < j より
# j - i == A[j] + A[i]
# j - A[j] == i + A[i]

from collections import defaultdict

def detect_v2(N, A):
    dict_i = defaultdict(set)

    count = 0
    for n in range(N):
        dict_i[n +  A[n]].add(n)
        # print(f'n + A[n]: {n - A[n]}')
        if n - A[n] in dict_i:
            count += len(dict_i[n - A[n]])

    # print('hash_table:', dict_i)
    # print('count:', count)
    return count

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))

    c = detect_v2(N, A)
    print(c)

Submission status: Accepted

Solution code:
n = int(input())
a = [num for num in map(int, input().split())]
a.insert(0, 0)

# print(n)
# print(a)

ans = 0
m = {}

for j in range(1, n+1):
  # j - A[j] = A[i] + i となるペアがあれば ans に加算
  j_val = j - a[j]
  if j_val in m:
    ans += m[j_val]
  
  # A[i] + i となる数をインクリメント
  i_val = a[j]+j
  if i_val in m:
    m[i_val] += 1
  else:
    m[i_val] = 1
print(ans)

Submission status: Accepted

Solution code:
#coding:utf-8

n = int(input())

a = input().split()

count = 0

r = [(n - int(m)) for n,m in enumerate(a)]
l = [(n + int(m)) for n,m in enumerate(a)]

r.remove(r[0])
l.remove(l[-1])

mar = max(r)
mir = min(r)
mal = max(l)
mil = min(l)

R = {}
L = {}

for n,m in zip(r,l):
    R[n] = 0
    L[m] = 0

for n,m in zip(r,l):
    R[n] += 1
    L[m] += 1

for i in R.keys():
    if i in L.keys():
        count += R[i] * L[i]

print("{}".format(count))

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
count=0
new=[]
s=set()
d={}
for i in range(0,len(l)):
    new.append(i+1-l[i])
    s.add(i+1+l[i])
    if i+1+l[i] in d.keys():
        d[i+1+l[i]]+=1
    else:
        d[i+1+l[i]]=1
for i in range(0,len(new)):
    if new[i] in s:
        count+=d[new[i]]
print(count)

Submission status: Accepted

Solution code:
import collections

N = int(input())
Heights = list(map(int, input().split()))

i_s = [i + 1 + Heights[i] for i in range(N)]
j_s = [j + 1 - Heights[j] for j in range(N)]

CTR_i = collections.Counter(i_s)
CTR_j = collections.Counter(j_s)

i_value = CTR_i.keys()
total = 0

for v in i_value:
    N_i = CTR_i[v]
    N_j = CTR_j[v]
    
    total += N_i * N_j

print(total)

Submission status: Accepted

Solution code:
import collections
n=int(input())
a=list(map(int,input().split()))

b=[]
for i in range(n):
    b.append(a[i]-i)

c = collections.Counter(b)

m=0
for i in range(n):
    m+=c[-(i+a[i])]
    c[a[i]-i]-=1
print(m)

Submission status: Accepted

Solution code:
from collections import defaultdict as dfd

n = int(input())
a = [int(i) for i in input().split()]

d = dfd(int)
ans = 0

for i, a_i in enumerate(a, 1):
    ans += d[i - a_i]
    d[i + a_i] += 1


print(ans)

Submission status: Accepted

Solution code:
# E - This Message Will Self-Distruct in 5s

from collections import Counter

n = int(input())
a = list(map(int, input().split()))
assert len(a) == n

# Aj - j の各値の個数
diff = Counter(a[j] - (j+1) for j in range(n))

# Aj - j == -Ai - i となる(i,j)の個数
count = sum(diff[-a[i] - (i+1)] for i in range(n))
print(count)

Submission status: Accepted

Solution code:
#j-i==a[j]+a[i]
#j-a[j]==a[i]+i

n=int(input())
a=list(map(int,input().split()))
import collections 
d=collections.defaultdict(int)
ans=0

for i in range(n):
    left=i-a[i]
    ans+=d[left]
    d[a[i]+i]+=1
    
print(ans)

Submission status: Accepted

Solution code:
def main():
    from collections import Counter as ct
    n = int(input())
    a = list(map(int, input().split()))

    npa = ct([i+1+j for i, j in enumerate(a)])
    nma = ct([i+1-j for i, j in enumerate(a)])

    cnt = 0

    for i, j in npa.items():
        cnt += j*nma[i]
    print(cnt)


main()

Submission status: Accepted

Solution code:
def main():
    int(input())

    d = {}
    ans = 0
    d_get = d.get
    for i, height in enumerate(input().split()):
        height = int(height)
        # 引き算のほうの i は足し算のよりも大きい。
        # 従って，このように順番に見ていく必要がある。
        got = d_get(i + height) or 0
        d[i + height] = got + 1
        ans += d_get(i - height) or 0

    return ans


if __name__ == '__main__':
    print(main())

# 大いに参考にした。
# https://kmjp.hatenablog.jp/entry/2020/05/03/1030

Submission status: Accepted

Solution code:
# class Map(dict):
#     def __missing__(self, key):
#         # この割当てなしのほうが速い。
#         # self[key] = 0
#         return 0


def main():
    int(input())

    d = {}
    ans = 0
    d_setdefault = d.setdefault
    d_get = d.get
    for i, height in enumerate(input().split()):
        height = int(height)
        # 引き算のほうの i は足し算のよりも大きい。
        # 従って，このように順番に見ていく必要がある。
        got = d_get(i + height) or 0
        d[i + height] = got + 1
        ans += d_setdefault(i - height, 0)

    return ans


if __name__ == '__main__':
    print(main())

# 大いに参考にした。
# https://kmjp.hatenablog.jp/entry/2020/05/03/1030

Submission status: Accepted

Solution code:
# class Map(dict):
#     def __missing__(self, key):
#         # この割当てなしのほうが速い。
#         # self[key] = 0
#         return 0


def main():
    int(input())

    d = {}
    ans = 0
    d_setdefault = d.setdefault
    for i, height in enumerate(input().split()):
        height = int(height)
        # 引き算のほうの i は足し算のよりも大きい。
        # 従って，このように順番に見ていく必要がある。
        d_setdefault(i + height, 0)
        d[i + height] += 1
        ans += d_setdefault(i - height, 0)

    return ans


if __name__ == '__main__':
    print(main())

# 大いに参考にした。
# https://kmjp.hatenablog.jp/entry/2020/05/03/1030

Submission status: Accepted

Solution code:
# class Map(dict):
#     def __missing__(self, key):
#         # この割当てなしのほうが速い。
#         # self[key] = 0
#         return 0


def main():
    int(input())

    d = {}
    ans = 0
    for i, height in enumerate(input().split()):
        height = int(height)
        # 引き算のほうの i は足し算のよりも大きい。
        # 従って，このように順番に見ていく必要がある。
        d.setdefault(i + height, 0)
        d[i + height] += 1
        ans += d.setdefault(i - height, 0)

    return ans


if __name__ == '__main__':
    print(main())

# 大いに参考にした。
# https://kmjp.hatenablog.jp/entry/2020/05/03/1030

Submission status: Accepted

Solution code:
# class Map(dict):
#     def __missing__(self, key):
#         # この割当てなしのほうが速い。
#         # self[key] = 0
#         return 0


def main():
    int(input())

    d = {}
    ans = 0
    for i, height in enumerate(input().split()):
        height = int(height)
        # 引き算のほうの i は足し算のよりも大きい。
        # 従って，このように順番に見ていく必要がある。
        got = d.get(i + height) or 0
        d[i + height] = got + 1
        ans += d.get(i - height) or 0

    return ans


if __name__ == '__main__':
    print(main())

# 大いに参考にした。
# https://kmjp.hatenablog.jp/entry/2020/05/03/1030

Submission status: Accepted

Solution code:
class Map(dict):
    def __missing__(self, key):
        self[key] = 0
        return 0


def main():
    int(input())

    d = Map()
    ans = 0
    for i, height in enumerate(input().split()):
        height = int(height)
        # 引き算のほうの i は足し算のよりも大きい。
        # 従って，このように順番に見ていく必要がある。
        d[i + height] += 1
        ans += d[i - height]

    return ans


if __name__ == '__main__':
    print(main())

# 大いに参考にした。
# https://kmjp.hatenablog.jp/entry/2020/05/03/1030