Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int a[300001];
int B[600003];
int n, N = 1;
int right(int k) { return 2 * k + 2; }
int left(int k) { return 2 * k + 1; }
int parent(int k) { return (k - 1) / 2; }
void query(int a, int b, int k, int l, int r) {
  // if (n==1)res = aray[0];
  if (r <= a || b <= l)
    return;
  else if (a <= l && r <= b)
    B[k]++;
  else {
    query(a, b, left(k), l, (l + r) / 2);
    query(a, b, right(k), (l + r) / 2, r);
  }
}
void update(int root) {
  int r = right(root);
  int l = left(root);
  if (r < N - 1) {
    B[r] += B[root];
    update(r);
  } else
    a[r - N + 1] = (B[r] + B[root] > n) ? n : B[r] + B[root];
  if (l < N - 1) {
    B[l] += B[root];
    update(l);
  } else
    a[l - N + 1] = (B[l] + B[root] > n) ? n : B[l] + B[root];
}
void init() {
  for (int i = 0; i < 2 * N; i++)
    B[i] = 0;
}
int main() {

  int K, l, r;
  cin >> n >> K;
  int limit = log2(2 * pow(10, 7));
  while (N < n)
    N *= 2;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < K; i++) {
    // if(i>limit+10) break;
    for (int j = 0; j < n; j++) {
      l = (j - a[j] >= 0) ? j - a[j] : 0;
      r = (j + a[j] + 1 <= n) ? j + a[j] + 1 : n;
      query(l, r, 0, 0, N);
    }
    update(0);
    init();
  }
  for (int i = 0; i < n; i++)
    cout << a[i] << " ";
  cout << endl;
  /*
  int t,p;
  t=p=1;
  for(int i=0;i<2*N-1;i++){
    cout<<B[i]<<" ";
    if(t==p){
      cout<<endl;
      t=1;
      p*=2;
    }
    else t++;
  }*/
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int a[300001];
int B[600003];
int n, N = 1;
int right(int k) { return 2 * k + 2; }
int left(int k) { return 2 * k + 1; }
int parent(int k) { return (k - 1) / 2; }
void query(int a, int b, int k, int l, int r) {
  // if (n==1)res = aray[0];
  if (r <= a || b <= l)
    return;
  else if (a <= l && r <= b)
    B[k]++;
  else {
    query(a, b, left(k), l, (l + r) / 2);
    query(a, b, right(k), (l + r) / 2, r);
  }
}
void update(int root) {
  int r = right(root);
  int l = left(root);
  if (r < N - 1) {
    B[r] += B[root];
    update(r);
  } else
    a[r - N + 1] = (B[r] + B[root] > n) ? n : B[r] + B[root];
  if (l < N - 1) {
    B[l] += B[root];
    update(l);
  } else
    a[l - N + 1] = (B[l] + B[root] > n) ? n : B[l] + B[root];
}
void init() {
  for (int i = 0; i < 2 * N; i++)
    B[i] = 0;
}
int main() {

  int K, l, r;
  cin >> n >> K;
  int limit = log2(2 * pow(10, 7));
  while (N < n)
    N *= 2;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < K; i++) {
    if (i > 43)
      break;
    for (int j = 0; j < n; j++) {
      l = (j - a[j] >= 0) ? j - a[j] : 0;
      r = (j + a[j] + 1 <= n) ? j + a[j] + 1 : n;
      query(l, r, 0, 0, N);
    }
    update(0);
    init();
  }
  for (int i = 0; i < n; i++)
    cout << a[i] << " ";
  cout << endl;
  /*
  int t,p;
  t=p=1;
  for(int i=0;i<2*N-1;i++){
    cout<<B[i]<<" ";
    if(t==p){
      cout<<endl;
      t=1;
      p*=2;
    }
    else t++;
  }*/
  return 0;
}