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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define l long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define testcase()                                                             \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

ll M = 1e9 + 7;
ll powerm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return res % M;
}
ll power(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
ll max3(ll a, ll b, ll c) { return max(max(a, b), c); }
ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
const int N = 1e5 + 5;
vector<int> adj[N];
bool vis[N];
int h[N];
int ans;

void dfs(int u) {
  vis[u] = 1;
  bool ok = 1;
  for (auto it : adj[u]) {
    if (h[u] <= h[it]) {
      ok = 0;
      break;
    }
  }
  if (ok)
    ans++;
  for (auto it : adj[u]) {
    if (vis[it])
      continue;
    dfs(it);
  }
}

int main() {

  // ifstream cin("input.txt");
  // ofstream cout("output.txt");
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  int a[n + 1];
  // vector <ll> v1,v2;
  map<int, int> m1, m2;
  for (int i = 1; i <= n; i++) {
    ll h;
    cin >> h;
    a[i] = h;
    // v1.pb(i-h); v2.pb(i+h);
    m1[i - h]++;
    m2[i + h]++;
  }
  // 	sort(v1.begin(),v1.end());
  // 	sort(v2.begin(),v2.end());
  ll ans = 0;
  // int id = 0;
  // 	for(auto it:v1) cout<<it<<' '; cout<<endl;
  // 	for(auto it:v2) cout<<it<<' '; cout<<endl;
  // 	for(int i=0;i<n;i++)
  // 	{
  // 	    int g = v1[i];
  // 	    if(v2[id]>g) continue;
  // 	    ll tmp = 0;
  // 	    while(id<n && v2[id]<g) id++;
  // 	    if(id==n) break;
  // 	    int t1 = id;
  // 	    while(id<n && v2[id]==g){ tmp++; id++; }
  // 	    id=t1;
  // 	    ans += tmp;
  // 	    if(id==n) break;
  // 	}

  for (int i = 1; i <= n; i++) {
    ans += m2[i - a[i]];
    ans += m1[i + a[i]];
  }
  cout << ans / 2;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define l long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define testcase()                                                             \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

ll M = 1e9 + 7;
ll powerm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return res % M;
}
ll power(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
ll max3(ll a, ll b, ll c) { return max(max(a, b), c); }
ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
const int N = 1e5 + 5;
vector<int> adj[N];
bool vis[N];
int h[N];
int ans;

void dfs(int u) {
  vis[u] = 1;
  bool ok = 1;
  for (auto it : adj[u]) {
    if (h[u] <= h[it]) {
      ok = 0;
      break;
    }
  }
  if (ok)
    ans++;
  for (auto it : adj[u]) {
    if (vis[it])
      continue;
    dfs(it);
  }
}

int main() {

  // ifstream cin("input.txt");
  // ofstream cout("output.txt");
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;

  vector<ll> v1, v2;
  for (int i = 1; i <= n; i++) {
    ll h;
    cin >> h;
    v1.pb(i - h);
    v2.pb(i + h);
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  ll ans = 0;
  int id = 0;
  // 	for(auto it:v1) cout<<it<<' '; cout<<endl;
  // 	for(auto it:v2) cout<<it<<' '; cout<<endl;
  for (int i = 0; i < n; i++) {
    int g = v1[i];
    if (v2[id] > g)
      continue;
    ll tmp = 0;
    while (id < n && v2[id] < g)
      id++;
    if (id == n)
      break;
    int t1 = id;
    while (id < n && v2[id] == g) {
      tmp++;
      id++;
    }
    id = t1;
    ans += tmp;
    if (id == n)
      break;
  }
  cout << ans;
  return 0;
}