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:
from collections import Counter

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

L = [A[i]+i for i in range(N)]
R = [j-A[j] for j in range(N)]

l = Counter(L)
r = Counter(R)

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=list(map(int, input().split()))
l=[i+a for i,a in enumerate(a)]
r=[i-a for i,a in enumerate(a)]
cl=Counter(l)
cr=Counter(r)
ans=0
for j in cl.keys():
  ans+=cl[j]*cr[j]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
l1 = []
l2 = []
for i in range(n):
    l1.append(a[i]+i)
    l2.append(-a[i]+i)
j = {}
ans = 0
for i in l1:
    if i in j:
        j[i]+= 1
    else:
        j[i] = 1
for i in l2:
    if i in j:
        ans+=j[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

def main():
    n = int(input())
    a = tuple([int(t)for t in input().split()])

    d_1 = {}
    d_2 = {}

    for i,a_i in enumerate(a):
        d_1.setdefault(i+a_i,0)
        d_1[i+a_i]+=1

    for j,a_j in enumerate(a):
        d_2.setdefault(j-a_j,0)
        d_2[j-a_j]+=1

    ans = 0
    for key in d_1.keys():
        if key in d_2.keys():
            ans += d_1[key]*d_2[key]

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

Submission status: Accepted

Solution code:
from collections import Counter
cnt = 0
N = int(input())
A = list(map(int, input().split()))
minus_list = [j - A[j] for j in range(N)]
counter = Counter(minus_list)
for i in range(N):
    add = A[i] + i
    cnt += counter[add]
print(cnt)

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter
n = int(input())
i = np.arange(n)
a = np.array(list(map(int, input().split())))
p = i + a
m = i - a
pc = Counter(p)
mc = Counter(m)
ans = sum([cnt * mc[x] for x, cnt in pc.items()])
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int,input().split())))
r = np.array(range(N))
X = r+A
Y = r-A
ans = 0
x_dic = {}
y_dic = {}
for x in X:
    if x in x_dic:
        x_dic[x] += 1
    else:
        x_dic[x] = 1
  
for y in Y:
    if y in y_dic:
        y_dic[y] += 1
    else:
        y_dic[y] = 1

for x in x_dic:
    if x in y_dic:
        ans += x_dic[x]*y_dic[x]        
print(ans)
#print(X,x_dic,Y,y_dic)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
x2l = {} # i+A_i
ans = 0
for i,a in enumerate(A,1):
    if i - a in x2l:
        ans += x2l[i-a]
    if i + a in x2l:
        x2l[i+a] += 1
    else:
        x2l[i+a] = 1
print(ans)

Submission status: Accepted

Solution code:
import collections

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

L = [A[x]+x+1 for x in range(N)]
R = [x+1-A[x] for x in range(N)]

cL = collections.Counter(L)
cR = collections.Counter(R)

cRlist = list(cR.keys())

for i in range(len(cRlist)):
    tmp = cRlist[i]
    ans += cR.get(tmp,0) * cL.get(tmp,0)
    
print(ans)

Submission status: Accepted

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

ans = 0
L = {}
R = {}
for i in range(n):
    if i+a[i] in L:
        L[i+a[i]].append(i)
    else:
        L[i+a[i]] = [i]
    if i-a[i] in R:
        R[i-a[i]].append(i)
    else:
        R[i-a[i]] = [i]
for i in range(n):
    if i in L and i in R:
        ans += len(L[i])*len(R[i])
print(ans)

Submission status: Accepted

Solution code:
def main(n,a_):
  from collections import defaultdict
  lr=[[i+ai,i-ai] for i,ai in enumerate(a_)]
  rs=defaultdict(lambda:0)
  ls=defaultdict(lambda:0)
  ans=0
  for l,r in lr:
    rs[r]+=1
    ls[l]+=1
  for k in rs.keys():
    ans+=rs[k]*ls[k]
  print(ans)
if __name__=='__main__':
  n=int(input())
  a_=list(map(int,input().split()))
  #n=200000
  #a_=[3 for _ in range(n)]
  main(n,a_)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
r = {}
for i in range(n):
    try:
        r[i-a[i]] += 1
    except:
        r[i-a[i]] = 1
cnt = 0
for i in range(n):
    r[i-a[i]] -= 1
    try:
        cnt += r[i+a[i]]
    except:
        pass
print(cnt)

Submission status: Accepted

Solution code:
import collections as col

n = int(input())
a = [0] + list(map(int,input().split()))

def binp(arr, val):
    left,right = 0,len(arr)-1
    while left <= right:
        mid = (left+right)//2
        if val==arr[mid]: return 1
        elif val>=arr[mid]: left = mid+1
        else: right = mid-1
    return 0

ap = [a[i] + i for i in range(1,n+1)]
an = [i - a[i] for i in range(1,n+1)]
counter = col.Counter(an)

ans = 0
for ele in ap:
    ans += counter[ele]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

L = [i+j for i, j in enumerate(A)]
R = [i-j for i, j in enumerate(A)]

l = collections.Counter(L)
r = collections.Counter(R)

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

print(ans)

Submission status: Accepted

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

ans = 0
sum = {} # sum[x] == (l + a[l] = x なる l の個数)
# 0 <= l < r < n なる (l,r) の全探索
for r in range(n):
  ans += sum.get(r - a[r], 0)
  sum[r + a[r]] = sum.get(r + a[r], 0) + 1
  
  # for l in range(r):
    # if r - l == a[l] + a[r]:
    # if r - a[r] == l + a[l]:
      # ans += 1
print(ans)

Submission status: Accepted

Solution code:
def main():
  import bisect
  n=int(input())
  a=list(map(int,input().split()))
  b=[a[i]+i+1 for i in range(n)]
  c=[i+1-a[i] for i in range(n)]
  b.sort()
  c.sort()
  ans=0
  for i in b:
    x=bisect.bisect_left(c,i)
    y=bisect.bisect_right(c,i)
    z=y-x
    ans+=z
  print(ans)
if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
import numpy as np
from collections import defaultdict

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

idxs = np.arange(n)
L, R = idxs + a, idxs - a

count = 0
d = defaultdict(int)
for i in range(n-1):
    d[L[i]] += 1
    count += d[R[i+1]]

print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

r = defaultdict(int)
l = defaultdict(int)
for i in range(n):
    r[al[i]+i] += 1
    l[i-al[i]] += 1

res = 0
for k in r:
    res += r[k]*l[k]

print(res)

Submission status: Accepted

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

cnt = defaultdict(int)
ans = 0
for i, a in enumerate(A, start=1):
    ans += cnt[-a + i]
    cnt[a + i] += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

N = int(sys.stdin.readline().rstrip())
As = list(map(int, sys.stdin.readline().rstrip().split()))

ll = [0] * N
lr = [0] * N
for i, a in enumerate(As):
    ll[i] = i + a
    lr[i] = i - a

cl = Counter(ll)
cr = Counter(lr)

ans = 0
for l in cl.keys():
    ans += cl[l] * cr[l]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

c = [0]*n
d = [0]*n

for i, j in enumerate(a):
    c[i] = i + j

for i, j in enumerate(a):
    d[i] = i - j

x = {}
for i, j in enumerate(c):
    if j not in x:
        x[j]=[i]
    else:
        x[j].append(i)
ans = 0
for i in d:
    if i not in x:
        continue
    else:
        ans += len(x[i])
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = [int(n) for n in input().split()]
record = defaultdict(int)
count = 0
for i in range(len(A)):
    cur = i-A[i]
    count += record[cur]
    record[i+A[i]] += 1
print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
k=0
b={}
for i in range(n):
    if a[i]+i in b:
        b[a[i]+i]+=1
    else:
        b[a[i]+i]=1
    if i-a[i] in b:
        k+=b[i-a[i]]
print(k)

Submission status: Accepted

Solution code:
n,*l=map(int,open(0).read().split());d,a={},0
for i in range(n):d[i+l[i]]=d.get(i+l[i],0)+1;a+=d.get(i-l[i],0)
print(a)

Submission status: Accepted

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

p = collections.Counter() # +
n = collections.Counter() # -
for i in range(N):
  p[i+A[i]] += 1
  n[i-A[i]] += 1
  
ans = 0
for i in p.keys():
  ans += p[i]*n[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

mp = defaultdict(lambda: [0, 0])
for i in range(1, N+1):
    L = i + A[i-1]
    R = i - A[i-1]
    mp[L][0] += 1
    mp[R][1] += 1

ans = 0
for v in mp.values():
    ans += v[0] * v[1]
print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
a=[int(_) for _ in input().split()]
l=[a[i]+i for i in range(n)]
r=[i-a[i] for i in range(n)]
c=collections.Counter(r)
ans=0
for i in range(n):ans+=c[l[i]]
print(ans)

Submission status: Accepted

Solution code:
def solve(n, xs):

    ans = 0
    tmp = {}
    for i in range(n-1, -1, -1):
        l = i+1 + xs[i]
        r = i+1 - xs[i]

        if l in tmp:
            ans += tmp[l]

        if r in tmp:
            tmp[r] += 1
        else:
            tmp[r] = 1

    return ans
if __name__ == "__main__":
    n = int(input())
    xs = [int(i) for i in input().split()]
    print(solve(n, xs))

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
height = list(map(int, input().split()))
ans = 0

d = defaultdict(int)

for i in range(n):
    x = i+height[i]
    d[x] += 1

for i in range(n):
    x = i-height[i]
    ans += d[x]

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
from collections import Counter
L = [i+a for i, a in enumerate(A)]
R = [i-a for i, a in enumerate(A)]
l = Counter(L)
r = Counter(R)
ans = 0
for i in l.keys():
    ans += l[i]*r[i]
print(ans)

Submission status: Accepted

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

L_list = {}
R_list = {}
cnt = 0
for i in range(N):
    L = i+1 + A[i]
    R = i+1 - A[i]

    if L in R_list.keys():
        cnt += R_list[L]
    if R in L_list.keys():
        cnt += L_list[R]

    if L in L_list.keys():
        L_list[L] += 1
    else:
        L_list[L] = 1
    if R in R_list.keys():
        R_list[R] += 1
    else:
        R_list[R] = 1

print(cnt)

Submission status: Accepted

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

L = [A[i] + (i + 1) for i in range(N)]
R = [(i + 1) - A[i] for i in range(N)]
import collections
dL = dict(collections.Counter(L))
dR = dict(collections.Counter(R))

ans = 0
for k in dL.keys():
    ans += dL[k] * dR.get(k, 0)
print(ans)
# print(L)
# print(R)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
L = [0] * 2 * N
R = [0] * 2 * N
for i in range(N):
    l = i + A[i]
    if l < N: L[l] += 1
    r = i - A[i]
    if r > 0: R[r] += 1
ans = 0 
for i in range(N):
    ans += L[i] * R[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N = I()
A = LI()

ans = 0
d = defaultdict(int)

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

Submission status: Accepted

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

ans = 0
r = {}
l = {}

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

    if i + a in r:
        r[i + a] += 1
    else:
        r[i + a] = 1

for k in r:
    if k in l:
        ans += r[k] * l[k]

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import collections
n = int(input())
A = np.array(list(map(int,input().split())))
B = np.array([i+1 for i in range(n)])
C,D = A+B,B-A
ans = 0

c = collections.Counter(C)
d = collections.Counter(D)
ans = 0
for i in c.keys():
    ans += c[i]*d[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
from collections import defaultdict

def main():

    N = int(input())
    a = list(map(int,input().split()))
    pl = defaultdict(int)
    ans = 0
    for i,num in enumerate(a):
        s = num+i
        t = num-i
        ans += pl[-t]
        pl[s] += 1

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

Submission status: Accepted

Solution code:
n=int(input())

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

I=[]#1~n-1
for i in range(n-1):
    I.append(0)
    
for i in range(n):
    if A[i]+i>=1 and A[i]+i<=n-1:
        I[A[i]+i-1]+=1


J=[]#1~n-1
for i in range(n-1):
    J.append(0)

for j in range(n):
    if j-A[j]>=1 and j-A[j]<=n-1:
        J[j-A[j]-1]+=1

s=0
for i in range(n-1):
    s+=I[i]*J[i]

print(s)

Submission status: Accepted

Solution code:
n = int(input())
a_array = list(map(int, input().split()))
a_array.insert(0, 0)
x_sum = {}
x_dis = {}

for i in range(1, n+1):
    a_sum = i + a_array[i]
    if a_sum in x_sum:
        x_sum[a_sum].append(i)
    else:
        x_sum[a_sum] = [i]
    if not(a_sum in x_dis):
        x_dis[a_sum] = []

    a_dis = i - a_array[i]
    if a_dis > 0:
        if a_dis in x_dis:
            x_dis[a_dis].append(i)
        else:
            x_dis[a_dis] = [i]
        if not(a_dis in x_sum):
            x_sum[a_dis] = []

# print(x_sum)
# print(x_dis)
count = 0
for k in x_sum.keys():
    count += len(x_sum[k]) * len(x_dis[k])

print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict
def E():
    N = int(input())
    A = [int(n) for n in input().split()]
    record = defaultdict(int)
    count = 0
    for i in range(len(A)):
        cur = i-A[i]
        count += record[cur]
        record[i+A[i]] += 1
    
    print(count)

E()

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N = int(rl())
    A = list(map(int, rl().split()))
    
    d = defaultdict(int)
    ans = 0
    for i in range(N)[::-1]:
        ans += d[-A[i] - i]
        d[A[i] - i] += 1
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import collections

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

L = [item+i for i, item in enumerate(A)]
R = [i - item for i, item in enumerate(A)]


C1 = collections.Counter(L)
C2 = collections.Counter(R)

ans = 0
for k, v in C1.items():
    if k in C2:
        ans += v * C2[k]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
l=[0]*n
r=[0]*n
for i in range(n):
    if a[i]+i+1<n:
        l[a[i]+i+1]+=1
    if i+1-a[i]>0:
        r[i+1-a[i]]+=1
ans=0
for i in range(n):
    ans+=l[i]*r[i]

print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
s=list(map(int, input().split()))
L=[i+a for i,a in enumerate(s)]
R=[i-a for i,a in enumerate(s)] 
t=collections.Counter(L)
u=collections.Counter(R)
ans = 0
for i in t.keys():
  ans+=t[i]*u[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

x = [0] * n
y = [0] * n
for i in range(n):
    x[i] = i + a[i]
    y[i] = i - a[i]

cx = Counter(x)
cy = Counter(y)

ans = 0
for i in cx.keys():
    ans += cx[i] * cy[i]

print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
s=list(map(int, input().split()))
L=[i+a for i,a in enumerate(s)]
R=[i-a for i,a in enumerate(s)] 
t=collections.Counter(L)
u=collections.Counter(R)
ans = 0
for i in t.keys():
  ans+=t[i]*u[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
R = [A[i] + i for i in range(n)]
L = [-A[i] + i for i in range(n)]
R_c = dict(Counter(R))
L_c = dict(Counter(L))
vals = set(R) & set(L)
ans = 0
for v in vals:
    r = 0 if R_c.get(v) is None else R_c[v]
    l = 0 if L_c.get(v) is None else L_c[v]
    ans += r*l
print(ans)

Submission status: Accepted

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

lx = {}
rx = {}

for i, ai in enumerate(a):
    li = (i + 1) + ai
    ri = (i + 1) - ai
    if li not in lx:
        lx[li] = set()
    lx[li].add(i + 1)
    if ri not in rx:
        rx[ri] = set()
    rx[ri].add(i + 1)
# print(lx)
# print(rx)

answer = 0
for x in lx.keys():
    if x in rx:
        s = lx[x]
        t = rx[x] - lx[x]
        answer += len(s) * len(t)

print(answer)

Submission status: Accepted

Solution code:
from collections import Counter

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

ai=[]
aj=[]

for i,a in enumerate(A):
    ai.append(i+1-a)
    aj.append(i+1+a)

ajc=Counter(aj)
ret=0
for x in ai:
    ret+=ajc.get(x,0)

print(ret)

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A_plus_index = list(A[i]+i+1 for i in range(N))
    A_plus_index = Counter(A_plus_index)
    index_minus_A = list(i+1-A[i] for i in range(N))
    ans = 0
    for a in index_minus_A:
        ans += A_plus_index.get(a, 0)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))[::-1]

import collections

L = [i + A[i] for i in range(n)]
R = [j - A[j] for j in range(n)]

cL = collections.Counter(L)
cR = collections.Counter(R)

cnt = 0

MAX = 200000
for x in range(MAX):
	cnt += cL[x] * cR[x]

print(cnt)

Submission status: Accepted

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

# 入力を整数に変換して受け取る
def input_int():
    return int(input())


# マイナス1した値を返却
def int1(x):
    return int(x) - 1


# 半角スペース区切り入力をIntに変換してMapで受け取る
def input_to_int_map():
    return map(int, input().split())


# 半角スペース区切り入力をIntに変換して受け取る
def input_to_int_tuple():
    return tuple(map(int, input().split()))


# 半角スペース区切り入力をIntに変換してマイナス1した値を受け取る
def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))


def main():
    n = int(input())
    a = input_to_int_tuple()

    from collections import defaultdict
    l = defaultdict(int)
    r = defaultdict(int)
    for i, h in enumerate(a):
        no = i + 1
        l[no + h] += 1
        r[no - h] += 1

    ret = 0
    for i in range(1, n + 1):
        ret += (l.get(i, 0) * r.get(i, 0))

    return ret


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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Wa = []
Sa = []
for i, j in enumerate(A):
    Wa.append(j + i + 1)
    Sa.append(i + 1 - j)
CWa = Counter(Wa)
CSa = Counter(Sa)
ans = 0

for i in CWa.keys():
    ans += CWa[i] * CSa[i]
print(ans)

Submission status: Accepted

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

X = {}
ans = 0
for i in range(n):
  L = i+A[i]
  R = i-A[i]
  
  if L not in X:
    X[L] = 0
    
  X[L]+=1
  
  if R in X:
    ans += X[R]
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int, input().split()))
iai = dict()
for i in range(n): 
	if(li[i] + i not in iai):
		iai[li[i] + i] = 1
	else:
		iai[li[i] + i] += 1
ans = 0
for j in range(n):
  if(j - li[j] in iai):
  	ans += iai[j - li[j]]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
mae = {}
ans = 0
for i in range(n):
    mae[i+a[i]]=mae.get(i+a[i],0)+1
    ans += mae.get(i-a[i],0)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
from collections import defaultdict
I = defaultdict(int)
J = defaultdict(int)
ans = 0

for i in range(N):
  I[A[i]+i] += 1
  J[i-A[i]] += 1
for i,j in I.items():
  ans += J[i]*j
    
print(ans)

Submission status: Accepted

Solution code:
import math
import numpy as np
x = int(input())
height = list(map(lambda x: int(x), input().split()))  
cands = [0 for i in range(len(height))]


count = 0
  
for i in range(len(height)):
  if i-height[i] >= 0:
    count += cands[i-height[i]]
      
  if i+height[i] < len(height):
    cands[i+height[i]] += 1

print(count)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

n, *a = map(int, read().split())
dict = defaultdict(int)
ans = 0
for i in range(n - 1, -1, -1):
    ans += dict[-a[i] - i]
    dict[a[i] - i] += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

cnt = 0
L = defaultdict(lambda: 0)
R = defaultdict(lambda: 0)

for i in range(N):
    L[i+A[i]] += 1
    R[i-A[i]] += 1

for key in L:
    cnt += L[key] * R[key]


print(cnt)

Submission status: Accepted

Solution code:
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd # 最大公約数
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) # 最小公倍数
# list(map(int, input().split()))
n = int(input())
a = list(map(int, input().split()))

x = {}
y = {}
for i in range(n):
    t = i - a[i]
    s = i + a[i]
    if t in x:
        x[t] += 1
    else:
        x[t] = 1
    if s in y:
        y[s] += 1
    else:
        y[s] = 1
cnt = 0
for i in x.keys():
    if i in y:
        cnt += x[i]*y[i]
print(cnt)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


N = readint()
A = list(readnums())

L = [i + x for i, x in enumerate(A)]
R = [i - x for i, x in enumerate(A)]

l_c = Counter(L)
r_c = Counter(R)

x1 = min(max(L), max(R))
x2 = max(min(L), min(R))

ans = 0
for i in range(x2, x1 + 1):
    ans += l_c[i] * r_c[i]

print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
ans = 0
R = {}
L = {}
for i in range(n):
    r = i + a[i]
    l = max(i - a[i], 0)
    if r in R:
        R[r] += 1
    else:
        R[r] = 1
    if l in L:
        L[l] += 1
    else:
        L[l] = 1

for k, v in R.items():
    if k in L:
        ans += v * L[k]

print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
#from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

#にぶたんサーチ
def search(arr, num):
    l = 0
    r = len(arr) - 1
    while r - l > 1:
        c = (r + l) // 2
        if arr[c] > num:
            r = c
        elif arr[c] < num:
            l = c
        else:
            r = c
            l = c
    if arr[l] == num:
        return l
    elif arr[r] == num:
        return r
    else:
        return -1

n = INT()
a = LIST()

l = [i + 1 + a[i] for i in range(n)]
r = [i + 1 - a[i] for i in range(n)]
b = Counter(r)
c = list(set(r))
c.sort()
ans = 0
for i in l:
    if search(c, i) < 0:
        continue
    ans += b[i]

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())

ij = defaultdict(int)
ji = defaultdict(int)

As = list(map(int, input().split()))
for j in range(n):
  ij[j+1+As[j]] += 1
  ji[j+1-As[j]] += 1
keys = set(ji.keys()) | set(ij.keys())
print(sum(ij[k] * ji[k] for k in keys if k > 1))

Submission status: Accepted

Solution code:
import collections
n,*a=map(int,open(0).read().split())
l=[i+1+a[i] for i in range(n)]
r=[i+1-a[i] for i in range(n)]
L=collections.Counter(l)
R=collections.Counter(r)
print(sum(L[i]*R[i] for i in L.keys()))

Submission status: Accepted

Solution code:
from collections import Counter

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

L = []
R = []
for i, j in enumerate(lis):
    L.append(i+j)
for i, j in enumerate(lis):
    R.append(i-j)
    
ref = dict(Counter(R))
ans = 0
for i in L:
    ans += ref.get(i, 0)
print(ans)

Submission status: Accepted

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

L = {}
R_val = []
for i in range(N):
    if i+A[i] not in L:
        L[i+A[i]] = 0
    L[i+A[i]]+=1
    R_val.append(i-A[i])
    
ans = 0
for one in R_val:
    if one in L:
        ans+=L[one]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N = int(readline())
    A = [int(i) for i in readline().split()]

    B = []
    C = []
    for i, a in enumerate(A, start=1):
        B.append(i + a)
        C.append(i - a)

    b = Counter(B)
    c = Counter(C)

    ans = 0
    for k, v in b.items():
        if k in c:
            ans += v * c[k]
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
from collections import defaultdict
def resolve():
    n = int(input())
    A = list(map(int,input().split()))
    count = defaultdict(int)

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

    print(res)
resolve()

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())

a_list = [int(x) for x in input().split()]

l = defaultdict(int)
r = defaultdict(int)
for i in range(n):
    l[i + a_list[i]] += 1
    r[i - a_list[i]] += 1

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

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left
input = sys.stdin.readline
def main():
    N = int(input())
    ans = 0
    L = {}
    R = {}
    A = tuple(map(int,input().split()))
    for i,a in enumerate(A):
        if a+i in L:
            L[a+i] += 1
        else:
            L[a+i] = 1
        if i-a in R:
            R[i-a] += 1
        else:
            R[i-a] = 1
    for k in L:
        if k in R:
            ans += L[k]*R[k]
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a = [0] + [int(x) for x in input().split()]

d1 = defaultdict(int)
d2 = defaultdict(int)

for i in range(1,n+1):
    d1[i-a[i]] += 1
    d2[i+a[i]] += 1

ans = 0

for k in d1.keys():
    ans += d1[k]*d2[k]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a2 = [i - a[i] for i in range(n)]
a3 = [a[i] + i for i in range(n)]
b2 = {}
for i in a2:
    b2.setdefault(i, 0)
    b2[i] += 1
b3 = {}
for i in a3:
    b3.setdefault(i, 0)
    b3[i] += 1
ans = 0
for i, j in b2.items():
    b3.setdefault(i, 0)
    ans += j * b3[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


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

a_plus_idx = Counter([a[i] + i for i in range(n)])
a_minus_idx = Counter([i - a[i] for i in range(n)])

total = 0
for k, v in a_plus_idx.items():
    total += v * a_minus_idx[k]

print(total)

Submission status: Accepted

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