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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using ull = unsigned long long;
using namespace std;
// #define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v, a) (reverse((v), (v) + (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
// max_element

//---------- セグ木 ----------
// 1<<17 = 2^17 = 131072
#define SEG_MAX (1 << 18)
int seg[SEG_MAX * 2] = {0};

//[a,b]にxを加算 (節点kをベースに[l,r]に足す)
void add(int a, int b, int x, int k = 0, int l = 0, int r = SEG_MAX - 1) {
  if (a <= l && r <= b) {
    seg[k] += x;
  } else {
    int l0 = l;
    int r0 = l + (r - l) / 2;
    int l1 = r0 + 1;
    int r1 = r;
    if (a <= r0) {
      int kk = 2 * k + 1;
      add(a, b, x, kk, l0, r0);
    }
    if (l1 <= b) {
      int kk = 2 * k + 2;
      add(a, b, x, kk, l1, r1);
    }
  }
}

int get(int index) {
  int sum = 0;
  int k = SEG_MAX - 1 + index; // 根
  while (true) {
    sum += seg[k];
    if (k == 0)
      break;
    k = (k - 1) / 2;
  }
  return sum;
}

int N, K;
int A[200010];

signed main() {
  cin >> N >> K;
  REP(i, N) cin >> A[i + 1];

  for (int i = 1; i <= N; i++) {
    add(i, i, A[i]);
  }

  for (int k = 0; k < K; k++) {
    memset(seg, 0, sizeof(seg));
    for (int i = 1; i <= N; i++) {
      int l = max(1, i - A[i]);
      int r = min(N, i + A[i]);
      add(l, r, 1);
    }

    bool end = true;
    for (int i = 1; i <= N; i++) {
      A[i] = min(N, get(i));
      if (A[i] < N) {
        end = false;
      }
    }
    if (end) {
      break;
    }
  }

  for (int i = 1; i <= N; i++) {
    cout << (i != 1 ? " " : "") << A[i];
  }
  cout << endl;
}

//
// signed main()
//{
//	cin >> N >> K;
//	REP(i, N) cin >> A[i];
//
//	for (int k = 0; k < K; k++)
//	{
//		memset(B, 0, sizeof(B));
//		for (int i = 0; i < N; i++)
//		{
//			for (int j = i - A[i]; j <= i + A[i]; j++)
//			{
//				if (0 <= j && j < N)
//					B[j]++;
//			}
//		}
//		memcpy(A, B, sizeof(A));
//		REP(i, N) cout << A[i] << " ";
//		cout << endl;
//	}
//
//}
//


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v, a) (reverse((v), (v) + (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
// max_element

//---------- セグ木 ----------
// 1<<17 = 2^17 = 131072
#define SEG_MAX (1 << 18)
int seg[SEG_MAX * 2] = {0};

//[a,b]にxを加算 (節点kをベースに[l,r]に足す)
void add(int a, int b, int x, int k = 0, int l = 0, int r = SEG_MAX - 1) {
  if (a <= l && r <= b) {
    seg[k] += x;
  } else {
    int l0 = l;
    int r0 = l + (r - l) / 2;
    int l1 = r0 + 1;
    int r1 = r;
    if (a <= r0) {
      int kk = 2 * k + 1;
      add(a, b, x, kk, l0, r0);
    }
    if (l1 <= b) {
      int kk = 2 * k + 2;
      add(a, b, x, kk, l1, r1);
    }
  }
}

int get(int index) {
  int sum = 0;
  int k = SEG_MAX - 1 + index; // 根
  while (true) {
    sum += seg[k];
    if (k == 0)
      break;
    k = (k - 1) / 2;
  }
  return sum;
}

int N, K;
int A[200010];

signed main() {
  cin >> N >> K;
  REP(i, N) cin >> A[i + 1];

  for (int i = 1; i <= N; i++) {
    add(i, i, A[i]);
  }

  for (int k = 0; k < K; k++) {
    memset(seg, 0, sizeof(seg));
    for (int i = 1; i <= N; i++) {
      int l = max(1LL, i - A[i]);
      int r = min(N, i + A[i]);
      add(l, r, 1);
    }

    bool end = true;
    for (int i = 1; i <= N; i++) {
      A[i] = min(N, get(i));
      if (A[i] < N) {
        end = false;
      }
    }
  }

  for (int i = 1; i <= N; i++) {
    cout << (i != 1 ? " " : "") << A[i];
  }
  cout << endl;
}

//
// signed main()
//{
//	cin >> N >> K;
//	REP(i, N) cin >> A[i];
//
//	for (int k = 0; k < K; k++)
//	{
//		memset(B, 0, sizeof(B));
//		for (int i = 0; i < N; i++)
//		{
//			for (int j = i - A[i]; j <= i + A[i]; j++)
//			{
//				if (0 <= j && j < N)
//					B[j]++;
//			}
//		}
//		memcpy(A, B, sizeof(A));
//		REP(i, N) cout << A[i] << " ";
//		cout << endl;
//	}
//
//}
//