Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <sstream>
#define ll long long int
#include <stdlib.h>
#include <vector>
#define pi acos(-1)
#define maxm 100005
#define pb push_back
#define MOD 1000000007
#define MOD2 998244353
#define highest(x) numeric_limits<x>::max()
#define lowest(x) numeric_limits<x>::min()
/// s1.substr(1,3);///STARTING FROM INDEX 1, 3 LNEGTH SUBSTRING.
/// s1.find("adn")///RETURNS STARTING INDEX OF THE SUBSTRING
///"adn" IF IT EXISTS IN THE STRING S1.///
/// s1.find("adn",int pos);///RETURNS STARTING INDEX OF THE
/// SUBSTRING "adn" SEARCHING FROM INDEX POS OF STRING s1.///
/// string::npos returns a garbage value if not found...
/// reverse(s1.begin(),s1.end())-> to reverse a string.
/// next_permutation(f,f+n)->f array er next permutation return korbe..
/// str.erase(1,2)->deletes 2 char from index 1..
/// for(auto i:p2)->i=p2.begin() theke p2.end() er ag porjonto..
/// max_element(v,v+4);->return a pointer to the max element
/// in a array/vector.. v from 1st element to 4th element.
/// same for min_element(v,v+4);
///__builtin_popcount(a);->counts how many number of one's
///  in a in binary representation.
/// accumulate(l1+1,l1+10+1,0LL)=>it perform the sum from l1[1] to
/// l1[10] and 0 is the initial sum.
/// cout<<fixed<<setprecision(12)<<x14<<endl;//=>x14 er
/// doshomik er por 12 ta 0 thakbe.
/// sort()=>[startaddress,endaddress).
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define gcd(a, b) __gcd(a, b)
using namespace std;

ll l1[200005], vis[200005], l3[1000005], s = 0, m = 0, mn = 0;
ll dx[] = {1, 0, -1, 0};
ll dy[] = {0, -1, 0, 1};

char s2[maxm];
// int sum[max];
int tree[maxm * 4];
// int prop[max*3];
void STB(int at, int L, int R) {
  // int sum[at]=0;
  if (L == R) {
    tree[at] = (s2[L - 1] - '0');
    return;
  }
  int mid = (L + R) / 2;
  STB(2 * at, L, mid);
  STB(2 * at + 1, mid + 1, R);
  // tree[at]=tree[2*at]+tree[2*at+1];
  // return sum[at];
}

void update(int at, int L, int R, int pos1, int pos2) {
  if (L > pos2 || R < pos1)
    return;

  if (L >= pos1 && R <= pos2) {
    tree[at] ^= 1;
    return;
  }
  int mid = (L + R) >> 1;
  //    if(pos2<=mid)update((at<<1)+1,L,mid,pos1,pos2);
  //    else if(pos1>mid)update((at<<1)+2,mid+1,R,pos1,pos2);
  //    else{
  //        update((at<<1)+1,L,mid,pos1,mid);
  //        update((at<<1)+2,mid+1,R,mid+1,pos2);
  //    }
  update((at << 1), L, mid, pos1, pos2);
  update((at << 1) + 1, mid + 1, R, pos1, pos2);
  // tree[at]=tree[(at<<1)+1]+tree[(at<<1)+2]+prop[at]*(R-L+1);
}

int query(int at, int L, int R, int l) {
  // if(L<=l && l<=R)return tree[at];
  if (L == R)
    return tree[at];
  if (L > l && l > R)
    return 0;

  int mid = (L + R) >> 1;
  //    if(l<=mid)query((at<<1)+1,L,mid,l,carry+prop[at]);
  //    else query((at<<1)+2,mid+1,R,l,carry+prop[at]);
  if (l <= mid)
    return tree[at] ^ query((at << 1), L, mid, l);
  else
    return tree[at] ^ query((at << 1) + 1, mid + 1, R, l);
  // return ;
}

/// reverse sorting function.
bool revp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  if (a.first != b.first)
    return a.first > b.first;
  else
    return a.second < b.second;
}
bool revp2(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return a.second < b.second;
}
bool revp3(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return a.first > b.first;
}

ll bigmod(ll b, ll p) {
  ll an = 1;
  b %= MOD;
  while (p > 0) {
    if (p & 1)
      an = an * b % MOD;
    b = b * b % MOD;
    p >>= 1;
  }
  return an;
  /// BINARY EXPONENTATION///
  /// TAKEN FROM "e-maxx"///
}

map<ll, ll> m4;

int main() /// Hafiz///ICE 6th///
{
  // IOS;
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ll t, i = 1, j, k, y, n, c, m, z, z2, l, r, cases = 0, d, p, nl, np, a, b,
        x2 = -1, x1 = 1000000005, x3 = 0, x4 = 0, x5 = 0, x6 = 0, x7 = 0,
        x8 = 0;
  double x10 = 100000, x14, x15, x16, x17, x18;
  char x11, y1 = 'a', y2, y3;
  int x12 = 0;
  string s3, s4, s5, s6, s7, s8;
  queue<ll> q2;
  stack<ll> st;
  set<ll> y4;
  vector<ll> vp;
  // vector<ll>vp2[200005];
  // map<ll,ll>mp;
  map<ll, ll> m2;
  map<ll, ll>::iterator it;
  map<ll, ll> m3;

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> l1[i];
    m4[i + l1[i]]++;
    x3 += m4[i - l1[i]];
  }
  cout << x3 << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <sstream>
#define ll long long int
#include <stdlib.h>
#include <vector>
#define pi acos(-1)
#define maxm 100005
#define pb push_back
#define MOD 1000000007
#define MOD2 998244353
#define highest(x) numeric_limits<x>::max()
#define lowest(x) numeric_limits<x>::min()
/// s1.substr(1,3);///STARTING FROM INDEX 1, 3 LNEGTH SUBSTRING.
/// s1.find("adn")///RETURNS STARTING INDEX OF THE SUBSTRING
///"adn" IF IT EXISTS IN THE STRING S1.///
/// s1.find("adn",int pos);///RETURNS STARTING INDEX OF THE
/// SUBSTRING "adn" SEARCHING FROM INDEX POS OF STRING s1.///
/// string::npos returns a garbage value if not found...
/// reverse(s1.begin(),s1.end())-> to reverse a string.
/// next_permutation(f,f+n)->f array er next permutation return korbe..
/// str.erase(1,2)->deletes 2 char from index 1..
/// for(auto i:p2)->i=p2.begin() theke p2.end() er ag porjonto..
/// max_element(v,v+4);->return a pointer to the max element
/// in a array/vector.. v from 1st element to 4th element.
/// same for min_element(v,v+4);
///__builtin_popcount(a);->counts how many number of one's
///  in a in binary representation.
/// accumulate(l1+1,l1+10+1,0LL)=>it perform the sum from l1[1] to
/// l1[10] and 0 is the initial sum.
/// cout<<fixed<<setprecision(12)<<x14<<endl;//=>x14 er
/// doshomik er por 12 ta 0 thakbe.
/// sort()=>[startaddress,endaddress).
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define gcd(a, b) __gcd(a, b)
using namespace std;

ll l1[200005], vis[200005], l3[1000005], s = 0, m = 0, mn = 0;
ll dx[] = {1, 0, -1, 0};
ll dy[] = {0, -1, 0, 1};

// vector<ll>v[5];
// set<ll>v2;
// map<ll,ll>m2;
// vector<pair<ll,ll>>p2;
// vector<pair<ll,ll>>p3;

char s2[maxm];
// int sum[max];
int tree[maxm * 4];
// int prop[max*3];
void STB(int at, int L, int R) {
  // int sum[at]=0;
  if (L == R) {
    tree[at] = (s2[L - 1] - '0');
    return;
  }
  int mid = (L + R) / 2;
  STB(2 * at, L, mid);
  STB(2 * at + 1, mid + 1, R);
  // tree[at]=tree[2*at]+tree[2*at+1];
  // return sum[at];
}

void update(int at, int L, int R, int pos1, int pos2) {
  if (L > pos2 || R < pos1)
    return;

  if (L >= pos1 && R <= pos2) {
    tree[at] ^= 1;
    return;
  }
  int mid = (L + R) >> 1;
  //    if(pos2<=mid)update((at<<1)+1,L,mid,pos1,pos2);
  //    else if(pos1>mid)update((at<<1)+2,mid+1,R,pos1,pos2);
  //    else{
  //        update((at<<1)+1,L,mid,pos1,mid);
  //        update((at<<1)+2,mid+1,R,mid+1,pos2);
  //    }
  update((at << 1), L, mid, pos1, pos2);
  update((at << 1) + 1, mid + 1, R, pos1, pos2);
  // tree[at]=tree[(at<<1)+1]+tree[(at<<1)+2]+prop[at]*(R-L+1);
}

int query(int at, int L, int R, int l) {
  // if(L<=l && l<=R)return tree[at];
  if (L == R)
    return tree[at];
  if (L > l && l > R)
    return 0;

  int mid = (L + R) >> 1;
  //    if(l<=mid)query((at<<1)+1,L,mid,l,carry+prop[at]);
  //    else query((at<<1)+2,mid+1,R,l,carry+prop[at]);
  if (l <= mid)
    return tree[at] ^ query((at << 1), L, mid, l);
  else
    return tree[at] ^ query((at << 1) + 1, mid + 1, R, l);
  // return ;
}

/// reverse sorting function.
bool revp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  if (a.first != b.first)
    return a.first > b.first;
  else
    return a.second < b.second;
}
bool revp2(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return a.second < b.second;
}
bool revp3(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return a.first > b.first;
}

ll bigmod(ll b, ll p) {
  ll an = 1;
  b %= MOD;
  while (p > 0) {
    if (p & 1)
      an = an * b % MOD;
    b = b * b % MOD;
    p >>= 1;
  }
  return an;
  /// BINARY EXPONENTATION///
}

map<ll, ll> m4;

long long int maxDiff(int num) {
  string s3, s4, s5;
  long long int x3 = 0, x4 = 0, x, y, z;
  char a, b, c;
  s3 = to_string(num);
  for (int i = 0; i < s3.size(); i++) {
    if (x3 == 0 && s3[i] != '9') {
      s4 += '9';
      x3++;
      a = s3[i];
    } else if (x3 > 0 && s3[i] == a)
      s4 += '9';
    else
      s4 += s3[i];
  }
  for (int i = 0; i < s3.size(); i++) {
    if (x4 == 0 && s3[i] != '1' && i == 0) {
      s5 += '1';
      x4++;
      b = s3[i];
      c = '1';
    } else if (x4 == 0 && s3[i] != '0' && s3[i] != '1' && i != 0) {
      s5 += '0';
      x4++;
      b = s3[i];
      c = '0';
    } else if (x4 > 0 && s3[i] == b)
      s5 += c;
    else
      s5 += s3[i];
  }
  x = stoi(s4);
  y = stoi(s5);
  cout << x << " " << y << endl;
  z = x - y;
  return z;
}
vector<ll> vp2[200005];
int main() {
  // IOS;
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ll t, i = 1, j, k, x, y, q = 0, n, c, m, z, z2, l, r, cases = 0, d, p, nl, np,
        a, e = 1, b, x2 = -1, x1 = 1000000005, x3 = 0, x4 = 0, x5 = 0, x6 = 0,
        x7 = 0, x8 = 0;
  double x10 = 100000, x14, x15, x16, x17, x18;
  char x11, y1 = 'a', y2, y3;
  int x12 = 0;
  string s3, s4, s5, s6, s7, s8;
  queue<ll> q2;
  stack<ll> st;
  set<ll> y4;
  vector<ll> vp;
  // vector<ll>vp2[200005];
  // map<ll,ll>mp;
  map<ll, ll> m2;
  map<ll, ll>::iterator it;
  map<ll, ll> m3;

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> l1[i];
    if (i >= 2) {
      x = l1[1] + l1[i];
      if (x == i - 1)
        x3++;
    }
  }
  for (int i = 2; i <= n - 1; i++) {
    for (int j = i + 1; j <= n; j++) {
      x = l1[i] + l1[j];
      y = abs(i - j);
      if (x == y)
        x3++;
    }
  }
  cout << x3 << endl;

  return 0;
}