Problem p02266 (C++) — buggy submission, verdict: Runtime Error

/*
?°´??????????????¢???
?°´?????????????±±??¨?±±?????????????????§??????
?±±(?\\???§???)?????¢???

?±±??????????????§????????£????????????????????¨????????§??????

?°´???????????????????????§???????°´??????????????????????????????????????¨?????????
?????§????±±???????¬????????±±??§?????????????°´????????????????????????????????¨????????????

?°´??????????????¢???????????????????±±???????????????
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
*/
#include <algorithm>
#include <cassert>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

//--- --- --- debug function --- --- ---
// #define DEBUG
#ifdef DEBUG
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
int dprintf(const char *format, ...) {
  char buffer[1024];
  va_list arg;
  va_start(arg, format);
  int len = vsprintf(buffer, format, arg);
  va_end(arg);
  std::cout << buffer;
  return len;
}
#else
int dprintf(const char *format, ...) { return 0; }
#endif
//--- --- --- debug function --- --- ---

typedef std::vector<int> TList;
typedef TList::iterator TIt;
struct TPeak {
  int Pos;
  int Alt;
  TPeak(int APos, int AAlt) : Pos(APos), Alt(AAlt) {}
};
typedef std::vector<TPeak> TPeakList;
bool CompareAlt(const TPeak &Left, const TPeak &Right) {
  return Left.Alt < Right.Alt;
}

void Output(const TList &List) {
  TList::const_iterator It = List.begin();
  std::cout << *(It++);
  for (; It != List.end(); ++It) {
    std::cout << " " << *It;
  }
  std::cout << std::endl;
}
TList GetAltitude(const std::string Diagram) {
  TList AltList;
  AltList.reserve(Diagram.size() + 1);
  int Alt = 0;
  AltList.push_back(Alt);
  for (int i = 0; i < Diagram.size(); ++i) {
    if (Diagram[i] == '\\\\') {
      --Alt;
    } else if (Diagram[i] == '/') {
      ++Alt;
    }
    AltList.push_back(Alt);
  }
  return AltList;
}
TPeakList GetPeakList(const TList &AltList) {
  TPeakList PeakList;
  bool IsUpped = true;
  for (int Pos = 1; Pos < AltList.size(); ++Pos) {
    if (IsUpped && AltList[Pos] < AltList[Pos - 1]) {
      PeakList.push_back(TPeak(Pos - 1, AltList[Pos - 1]));
      IsUpped = false;
    }
    if (AltList[Pos] > AltList[Pos - 1]) {
      IsUpped = true;
    }
  }
  if (IsUpped) {
    PeakList.push_back(TPeak(AltList.size() - 1, AltList[AltList.size() - 1]));
  }
  return PeakList;
}
int GetArea(const TList &AltList, int TopPos, int SecondPos) {
  dprintf("TopPos:%d, SecondPos:%d\\n", TopPos, SecondPos);
  int way = TopPos < SecondPos ? -1 : 1;
  int Depth = 0;
  int Area = 0;
  for (int i = SecondPos + way; i != TopPos + way; i += way) {
    dprintf("%2d:%d\\n", i, AltList[i]);
    switch (AltList.at(i) - AltList.at(i - way)) {
    case 1:
      --Depth;
      Area += Depth * 2 + 1;
      if (Depth == 0) {
        dprintf("Area:%d\\n", Area / 2);
        return Area / 2;
      }
      break;
    case 0:
      Area += Depth * 2;
      break;
    case -1:
      Area += Depth * 2 + 1;
      ++Depth;
      break;
    default:
      assert(false);
    }
  }
  assert(false);
}
TList SimulateFlood(const TList &AltList, TPeakList::const_iterator begin,
                    TPeakList::const_iterator end) {
  TList List;
  TPeakList::const_iterator Top = begin;
  while (Top + 1 != end) {
    TPeakList::const_iterator Second =
        std::max_element(Top + 1, end, CompareAlt);
    int Area = GetArea(AltList, Top->Pos, Second->Pos);
    List.push_back(Area);
    Top = Second;
  }
  return List;
}
// for Debug
void ShowPeakList(const TPeakList &PeakList) {
  for (int i = 0; i < PeakList.size(); ++i) {
    std::cout << PeakList[i].Pos << " ";
  }
  std::cout << std::endl;
  for (int i = 0; i < PeakList.size(); ++i) {
    std::cout << PeakList[i].Alt << " ";
  }
  std::cout << std::endl;
}
TList SimulateFlood(const std::string Diagram) {
  TList AltList = GetAltitude(Diagram);
  TPeakList PeakList = GetPeakList(AltList);
  if (PeakList.size() < 2) {
    return TList();
  }

  // ShowPeakList(PeakList); // debug
  TPeakList::iterator TopPeak =
      std::max_element(PeakList.begin(), PeakList.end(), CompareAlt);
  dprintf("TopPeakPos:%d\\n", TopPeak->Pos);
  dprintf("TopPeakAlt:%d\\n", TopPeak->Pos);
  TList RightAreaList = SimulateFlood(AltList, TopPeak, PeakList.end());

  int D = std::distance(PeakList.begin(), TopPeak);
  dprintf("D:%d\\n", D);
  if (D == 0)
    return RightAreaList;
  TPeakList LeftPeakList(PeakList.begin(), TopPeak + 1);
  std::reverse(LeftPeakList.begin(), LeftPeakList.end());
  TList LeftAreaList =
      SimulateFlood(AltList, LeftPeakList.begin(), LeftPeakList.end());
  std::reverse(LeftAreaList.begin(), LeftAreaList.end());

  LeftAreaList.insert(LeftAreaList.end(), RightAreaList.begin(),
                      RightAreaList.end());
  return LeftAreaList;
}

int main() {
  std::string Input;
  std::cin >> Input;

  TList AreaList = SimulateFlood(Input);
  if (AreaList.empty()) {
    std::cout << 0 << std::endl << 0 << std::endl;
    return 0;
  }
  int Sum = std::accumulate(AreaList.begin(), AreaList.end(), 0);
  std::cout << Sum << std::endl;
  std::cout << AreaList.size() << " ";
  Output(AreaList);
  return 0;
}

Error: 0

Fixed submission:

/*
?°´??????????????¢???
?°´?????????????±±??¨?±±?????????????????§??????
?±±(?\\???§???)?????¢???

?±±??????????????§????????£????????????????????¨????????§??????

?°´???????????????????????§???????°´??????????????????????????????????????¨?????????
?????§????±±???????¬????????±±??§?????????????°´????????????????????????????????¨????????????

?°´??????????????¢???????????????????±±???????????????
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
*/
#include <algorithm>
#include <cassert>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

//--- --- --- debug function --- --- ---
// #define DEBUG
#ifdef DEBUG
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
int dprintf(const char *format, ...) {
  char buffer[1024];
  va_list arg;
  va_start(arg, format);
  int len = vsprintf(buffer, format, arg);
  va_end(arg);
  std::cout << buffer;
  return len;
}
#else
int dprintf(const char *format, ...) { return 0; }
#endif
//--- --- --- debug function --- --- ---

typedef std::vector<int> TList;
typedef TList::iterator TIt;
struct TPeak {
  int Pos;
  int Alt;
  TPeak(int APos, int AAlt) : Pos(APos), Alt(AAlt) {}
};
typedef std::vector<TPeak> TPeakList;
bool CompareAlt(const TPeak &Left, const TPeak &Right) {
  return Left.Alt < Right.Alt;
}

void Output(const TList &List) {
  TList::const_iterator It = List.begin();
  std::cout << *(It++);
  for (; It != List.end(); ++It) {
    std::cout << " " << *It;
  }
  std::cout << std::endl;
}
TList GetAltitude(const std::string Diagram) {
  TList AltList;
  AltList.reserve(Diagram.size() + 1);
  int Alt = 0;
  AltList.push_back(Alt);
  for (int i = 0; i < Diagram.size(); ++i) {
    if (Diagram[i] == '\\\\') {
      --Alt;
    } else if (Diagram[i] == '/') {
      ++Alt;
    }
    AltList.push_back(Alt);
  }
  return AltList;
}
TPeakList GetPeakList(const TList &AltList) {
  TPeakList PeakList;
  bool IsUpped = true;
  for (int Pos = 1; Pos < AltList.size(); ++Pos) {
    if (IsUpped && AltList[Pos] < AltList[Pos - 1]) {
      PeakList.push_back(TPeak(Pos - 1, AltList[Pos - 1]));
      IsUpped = false;
    }
    if (AltList[Pos] > AltList[Pos - 1]) {
      IsUpped = true;
    }
  }
  if (IsUpped) {
    PeakList.push_back(TPeak(AltList.size() - 1, AltList[AltList.size() - 1]));
  }
  return PeakList;
}
int GetArea(const TList &AltList, int TopPos, int SecondPos) {
  dprintf("TopPos:%d, SecondPos:%d\\n", TopPos, SecondPos);
  int way = TopPos < SecondPos ? -1 : 1;
  int Depth = 0;
  int Area = 0;
  for (int i = SecondPos + way; i != TopPos + way; i += way) {
    dprintf("%2d:%d\\n", i, AltList[i]);
    switch (AltList.at(i) - AltList.at(i - way)) {
    case 1:
      --Depth;
      Area += Depth * 2 + 1;
      if (Depth == 0) {
        dprintf("Area:%d\\n", Area / 2);
        return Area / 2;
      }
      break;
    case 0:
      Area += Depth * 2;
      break;
    case -1:
      Area += Depth * 2 + 1;
      ++Depth;
      break;
    default:
      assert(false);
    }
  }
  assert(false);
}
TList SimulateFlood(const TList &AltList, TPeakList::const_iterator begin,
                    TPeakList::const_iterator end) {
  TList List;
  TPeakList::const_iterator Top = begin;
  while (Top + 1 != end) {
    TPeakList::const_iterator Second =
        std::max_element(Top + 1, end, CompareAlt);
    int Area = GetArea(AltList, Top->Pos, Second->Pos);
    List.push_back(Area);
    Top = Second;
  }
  return List;
}
// for Debug
void ShowPeakList(const TPeakList &PeakList) {
  for (int i = 0; i < PeakList.size(); ++i) {
    std::cout << PeakList[i].Pos << " ";
  }
  std::cout << std::endl;
  for (int i = 0; i < PeakList.size(); ++i) {
    std::cout << PeakList[i].Alt << " ";
  }
  std::cout << std::endl;
}
TList SimulateFlood(const std::string Diagram) {
  TList AltList = GetAltitude(Diagram);
  TPeakList PeakList = GetPeakList(AltList);

  // ShowPeakList(PeakList); // debug
  TPeakList::iterator TopPeak =
      std::max_element(PeakList.begin(), PeakList.end(), CompareAlt);
  dprintf("TopPeakPos:%d\\n", TopPeak->Pos);
  dprintf("TopPeakAlt:%d\\n", TopPeak->Pos);
  TList RightAreaList = SimulateFlood(AltList, TopPeak, PeakList.end());

  int D = std::distance(PeakList.begin(), TopPeak);
  dprintf("D:%d\\n", D);
  if (D == 0)
    return RightAreaList;
  TPeakList LeftPeakList(PeakList.begin(), TopPeak + 1);
  std::reverse(LeftPeakList.begin(), LeftPeakList.end());
  TList LeftAreaList =
      SimulateFlood(AltList, LeftPeakList.begin(), LeftPeakList.end());
  std::reverse(LeftAreaList.begin(), LeftAreaList.end());

  LeftAreaList.insert(LeftAreaList.end(), RightAreaList.begin(),
                      RightAreaList.end());
  return LeftAreaList;
}

int main() {
  std::string Input;
  std::cin >> Input;

  TList AreaList = SimulateFlood(Input);
  int Sum = std::accumulate(AreaList.begin(), AreaList.end(), 0);
  std::cout << Sum << std::endl;
  std::cout << AreaList.size() << " ";
  Output(AreaList);
  return 0;
}