C - 秘密特工

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 NN attendees in the party, and they are given attendee numbers from 11 through NN. The height of Attendee ii is AiAi​.

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 N(N−1)22N(N−1)​ ways to choose two from the NN 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≤N≤2×1052≤N≤2×105
  • 1≤Ai≤109 (1≤i≤N)1≤Ai​≤109 (1≤i≤N)

Input

Input is given from Standard Input in the following format:

NN
A1A1​ A2A2​ …… ANAN​

Output

Print the number of pairs satisfying the condition.

Sample 1

Inputcopy Outputcopy
6
2 3 3 1 3 1
3
  • A1+A4=3A1​+A4​=3, so the pair of Attendee 11 and 44 satisfy the condition.
  • A2+A6=4A2​+A6​=4, so the pair of Attendee 22 and 66 satisfy the condition.
  • A4+A6=2A4​+A6​=2, so the pair of Attendee 44 and 66 satisfy the condition.

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

Sample 2

Inputcopy Outputcopy
6
5 2 4 2 8 8
0

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

Sample 3

Inputcopy Outputcopy
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
22

分析,因为要满足对应位置数值之和等于下标之差,所以从前往后一边遍历一边标记就可以了

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include 
#include
#include
#include
#define dbug cout<<"hear!"<=b;i--)
#define pper(a,b) for(ll j=a;j>=b;j--)
#define no cout<<"NO"<,greater >q;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair PII;
const int N = 2e5 + 100;
const int  INF = 0x3f3f3f3f;
ll gcdd(ll a, ll b)
{
    if (b) while ((a %= b) && (b %= a));
    return a + b;
}
// ll h[N],ne[N],w[N],to[N],idx;
// void add(int a,int b,int c)
// {
// 	to[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
// }
const ll mod =998244353;
ll  t, n, m, a, b, c,d, x,y, k, cnt, ans, ant, sum,q, p,idx;
ll arr[N], brr[N], crr[N];

int main()
{
	cin>>n;
	rep(1,n)
	{
		cin>>arr[i];
	}
	mapmp;
	ans=0;
	rep(1,n)
	{
		ans+=mp[i-arr[i]];
		mp[i+arr[i]]++;
	}
	cout<

 

你可能感兴趣的:(c语言,c++,算法)