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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

typedef long long ll;
typedef std::map<char, int> string_type;
typedef std::map<string_type, int> type_occurences;
typedef std::map<int, std::map<int, int>> type_lamp_map;

struct tokiomarine2020_c {
  // 1-origin
  std::string run(int N, int K, std::vector<int> A) {
    std::stringstream ss;
    long double km1ln2 = (K - 1) * std::log(2);
    long double lnN = std::log(N);

    if (N == 1) {
      // K > 0
      return "1 ";
    }

    if (km1ln2 > lnN * 3) {
      for (int i = 0; i < N; ++i) {
        ss << N << " ";
      }
      return ss.str();
    }

    std::vector<int> Bn1, Bn2;
    std::vector<int> *Bn_cur, *Bn_next, *Bn_temp;
    Bn1.resize(N);
    Bn2 = A;
    Bn_cur = &Bn1; // dummy
    Bn_next = &Bn2;

    for (int round = 0; round < K; ++round) {
      // update
      Bn_temp = Bn_cur;
      Bn_cur = Bn_next;
      Bn_next = Bn_temp;

      // renew
      Bn_next->clear();
      Bn_next->resize(N);

      // current
      // for(int i = 0; i < N; ++i) {
      // 	std::cout << (*Bn_cur)[i] << "\\t";
      // }
      // std::cout << std::endl;

      // loop

      for (int i = 0; i < N; ++i) {
        const int jmin = std::max(0, i - (*Bn_cur)[i]);
        const int jmax = std::min(N - 1, i + (*Bn_cur)[i]);
        // lamp_map[jmin][jmax] += 1;
        (*Bn_next)[jmin] += 1;
        if (jmax + 1 <= N) {
          (*Bn_next)[jmax + 1] -= 1;
        }
        // for(int j = jmin; j <= jmax; ++j) {
        // 	(*Bn_next)[j] += 1;
        // }
      }
      for (int i = 1; i < N; ++i) {
        (*Bn_next)[i] += (*Bn_next)[i - 1];
      }

      // fin if all N
      bool allN = true;
      for (int i = 0; i < N; ++i) {
        if ((*Bn_next)[i] != N) {
          allN = false;
          break;
        }
      }
      if (allN)
        break;
    }

    for (int i = 0; i < N; ++i) {
      ss << (*Bn_next)[i] << " ";
    }

    return ss.str();
  }
};

int main(void) {
  tokiomarine2020_c solver;
  int N, K, t;
  std::vector<int> A;

  std::cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    std::cin >> t;
    A.push_back(t);
  }

  std::cout << solver.run(N, K, A) << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

typedef long long ll;
typedef std::map<char, int> string_type;
typedef std::map<string_type, int> type_occurences;
typedef std::map<int, std::map<int, int>> type_lamp_map;

struct tokiomarine2020_c {
  // 1-origin
  std::string run(int N, int K, std::vector<int> A) {
    std::stringstream ss;
    long double km1ln2 = (K - 1) * std::log(2);
    long double lnN = std::log(N);

    if (N == 1) {
      // K > 0
      return "1 ";
    }

    if (km1ln2 > lnN * 3) {
      for (int i = 0; i < N; ++i) {
        ss << N << " ";
      }
      return ss.str();
    }

    std::vector<int> Bn1, Bn2;
    std::vector<int> *Bn_cur, *Bn_next, *Bn_temp;
    Bn1.resize(N);
    Bn2 = A;
    Bn_cur = &Bn1; // dummy
    Bn_next = &Bn2;

    for (int round = 0; round < K; ++round) {
      // update
      Bn_temp = Bn_cur;
      Bn_cur = Bn_next;
      Bn_next = Bn_temp;

      // renew
      Bn_next->clear();
      Bn_next->resize(N);

      // current
      // for(int i = 0; i < N; ++i) {
      // 	std::cout << (*Bn_cur)[i] << "\\t";
      // }
      // std::cout << std::endl;

      // loop
      // type_lamp_map lamp_map;
      int count_N = 0;
      for (int i = 0; i < N; ++i) {
        if ((*Bn_cur)[i] == N) {
          count_N++;
        }
      }

      for (int i = 0; i < N; ++i) {
        (*Bn_next)[i] += count_N;
        if ((*Bn_cur)[i] == N) {
          continue;
        }
        const int jmin = std::max(0, i - (*Bn_cur)[i]);
        const int jmax = std::min(N - 1, i + (*Bn_cur)[i]);
        // lamp_map[jmin][jmax] += 1;
        for (int j = jmin; j <= jmax; ++j) {
          (*Bn_next)[j] += 1;
        }
      }
      // N になったものは無視し、全てに加える

      // for(int i = 0; i < N; ++i) {
      // 	for(type_lamp_map::iterator left_it = lamp_map.begin(); left_it
      // != lamp_map.end(); ++left_it) {
      // 		// search left
      // 		if(left_it->first > i) {
      // 			break;
      // 		}
      // 		auto lb = (left_it->second).lower_bound(i);
      // 		// larger or same
      // 		for(auto it = lb; it != (left_it->second).end(); ++it) {
      // 			(*Bn_next)[i] += it->second;
      // 		}
      // 	}
      // }

      // fin if all N
      bool allN = true;
      for (int i = 0; i < N; ++i) {
        if ((*Bn_next)[i] != N) {
          allN = false;
          break;
        }
      }
      if (allN)
        break;
    }

    for (int i = 0; i < N; ++i) {
      ss << (*Bn_next)[i] << " ";
    }

    return ss.str();
  }
};

int main(void) {
  tokiomarine2020_c solver;
  int N, K, t;
  std::vector<int> A;

  std::cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    std::cin >> t;
    A.push_back(t);
  }

  std::cout << solver.run(N, K, A) << std::endl;

  return 0;
}