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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <valarray>
#include <vector>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RFOR(i, c)                                                             \\
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a, b) make_pair(a, b)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &t) {
  os << "[";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "]";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &t) {
  os << "{";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
  return os << "(" << t.first << "," << t.second << ")";
}
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first - t.first, s.second - t.second);
}

namespace geom {
#define X real()
#define Y imag()
#define at(i) ((*this)[i])
#define SELF (*this)
enum { TRUE = 1, FALSE = 0, BORDER = -1 };
typedef int BOOL;
typedef double R;
const R INF = 1e8;
const R EPS = 1e-8;
const R PI = 3.1415926535897932384626;
inline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
inline BOOL less(const R &x, const R &y) { return sig(x - y) ? x < y : BORDER; }
typedef complex<R> P;
inline R norm(const P &p) { return p.X * p.X + p.Y * p.Y; }
inline R inp(const P &a, const P &b) { return (conj(a) * b).X; }
inline R outp(const P &a, const P &b) { return (conj(a) * b).Y; }
inline P unit(const P &p) { return p / abs(p); }
inline P proj(const P &s, const P &t) { return t * inp(s, t) / norm(t); }
inline int ccw(const P &s, const P &t, const P &p, int adv = 0) {
  int res = sig(outp(t - s, p - s));
  if (res || !adv)
    return res;
  if (sig(inp(t - s, p - s)) < 0)
    return -2; // p-s-t
  if (sig(inp(s - t, p - t)) < 0)
    return 2; // s-t-p
  return 0;   // s-p-t
}

struct L : public vector<P> { // line
  L(const P &p1, const P &p2) {
    this->push_back(p1);
    this->push_back(p2);
  }
  L() {}
  P dir() const { return at(1) - at(0); }
  BOOL online(const P &p) const { return !sig(outp(p - at(0), dir())); }
};
struct S : public L { // segment
  S(const P &p1, const P &p2) : L(p1, p2) {}
  S() {}
  BOOL online(const P &p) const {
    if (!sig(norm(p - at(0))) || !sig(norm(p - at(1))))
      return BORDER;
    return !sig(abs(at(0) - p) + abs(at(1) - p) - abs(at(0) - at(1)));
  }
};
struct C : public P {
  C() {}
  C(const P &p, const R r) : P(p), r(r) {}
  R r;
  BOOL inside(const P &p) const { return less(norm(p - SELF), r * r); }
};
struct F : public C {
  R s, t;
  F(const C &c, R ss, R tt) : C(c), s(ss), t(tt) {
    if (PI < s)
      s -= 2 * PI;
    if (PI < t)
      t -= 2 * PI;
  }
  BOOL inside(const P &p) const {
    P v = p - SELF;
    if (!sig(norm(v)))
      return BORDER;
    R a = arg(v);
    if (t < s) {
      if ((!less(s, a) && !less(a, t)) || !less(norm(v), r * r))
        return FALSE;
      return less(s, a) | less(a, t) | less(norm(v), r * r);
    } else {
      if (!less(s, a) || !less(a, t) || !less(norm(v), r * r))
        return FALSE;
      return less(s, a) | less(a, t) | less(norm(v), r * r);
    }
  }
};
P crosspoint(const L &l, const L &m);
struct G : public vector<P> {
  G(size_type size = 0) : vector(size) {}
  S edge(int i) const { return S(at(i), at(i + 1 == size() ? 0 : i + 1)); }
  BOOL contains(const P &p) const {
    R sum = .0;
    REP(i, size()) {
      if (S(at(i), at((i + 1) % size())).online(p))
        return BORDER; // online
      sum += arg((at(i) - p) / (at((i + 1) % size()) - p));
    }
    return !!sig(sum);
  }
  R area() const {
    R sum = 0;
    REP(i, size()) sum += outp(at(i), at((i + 1) % size()));
    return abs(sum / 2.);
  }

  G convex_hull(bool online = false) {
    if (size() < 2)
      return *this;
    sort(ALL(*this));
    G r;
    r.resize((int)size() * 2);
    int k = 0;
    for (int i = 0; i < size(); r[k++] = at(i++))
      while (k > 1 && ccw(r[k - 2], r[k - 1], at(i)) < 1 - online)
        k--;
    int t = k;
    for (int i = (int)size() - 1; i >= 0; r[k++] = at(i--))
      while (k > t && ccw(r[k - 2], r[k - 1], at(i)) < 1 - online)
        k--;
    r.resize(k - 1);
    return r;
  }
  G cut(const L &l) const {
    G g;
    REP(i, size()) {
      const S &s = edge(i);
      if (ccw(l[0], l[1], s[0], 0) >= 0)
        g.push_back(s[0]);
      if (ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0)
        g.push_back(crosspoint(s, l));
    }
    return g;
  }
  G Voronoi(const vector<P> &p, const int t) const {
    G g = *this;
    REP(i, p.size()) if (i != t) {
      const P m = (p[t] + p[i]) * 0.5;
      g = g.cut(L(m, m + (p[i] - p[t]) * P(0, 1)));
    }
    return g;
  }
};

inline P proj(const P &s, const L &t) {
  return t[0] + proj(s - t[0], t[1] - t[0]);
}
inline P reflect(const P &s, const L &t) { return 2. * proj(s, t) - s; }
inline S reflect(const S &s, const L &t) {
  return S(reflect(s[0], t), reflect(s[1], t));
}
inline P crosspoint(const L &l, const L &m) {
  R A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);
  if (!sig(abs(A)) && !sig(abs(B)))
    return m[0]; // same line
  if (abs(A) < EPS)
    assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}
#undef SELF
#undef at
} // namespace geom

using namespace geom;

namespace std {
bool operator<(const P &a, const P &b) {
  return sig(a.X - b.X) ? a.X < b.X : a.Y + EPS < b.Y;
}
bool operator==(const P &a, const P &b) { return !sig(norm(a - b)); }
/*	istream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return
   is;} istream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >>
   l[1];} istream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}
        const double B = 500;
        const double Z = 1;
        ostream& operator<<(ostream &os, const C &c){return os <<
   "circle("<<B+Z*(c.X)<<", "<<1000-B-Z*(c.Y)<<", "<<Z*(c.r)<<")";} ostream&
   operator<<(ostream &os, const P &p){return os << C(p, 2.);} ostream&
   operator<<(ostream &os, const S &s){return os << "line("<<B+Z*(s[0].X)<<",
   "<<1000-B-Z*(s[0].Y)<<", "<<B+Z*(s[1].X)<<", "<<1000-B-Z*(s[1].Y)<<")";}
        ostream& operator<<(ostream &os, const G &g){REP(i, g.size()) cout <<
   g.edge(i) << endl;return os;}
*/
} // namespace std

int n;
string s;

struct PL {
  int t;
  P p;
  L l;
  PL() {}
  PL(P p) : p(p), t(0) {}
  PL(L l) : l(l), t(1) {}
  PL operator*(const PL &opp) {
    if (!t && !opp.t)
      return PL(L(p, opp.p));
    if (t && opp.t)
      return PL(crosspoint(l, opp.l));
    if (!t && opp.t)
      return PL(reflect(p, opp.l));
    if (t && !opp.t)
      return PL(reflect(opp.p, l));
  }
};

R number(int &p) {
  int sig = 1;
  if (s[p] == '-') {
    sig = -1;
    p++;
  }
  int res = 0;
  while (isdigit(s[p]))
    res = res * 10 + s[p++] - '0';
  return sig * res;
}

PL point(int &p);
PL fact(int &p) {
  assert(s[p] == '(');
  p++;
  PL res;
  if (s[p] == '(') {
    res = point(p);
    p++;
    return res;
  } else if (isdigit(s[p]) || s[p] == '-') {
    R x = number(p);
    assert(s[p] == ',');
    p++;
    R y = number(p);
    res = PL(P(x, y));
  } else
    assert(false);
  assert(s[p] == ')');
  p++;
  return res;
}

PL point(int &p) {
  PL res = fact(p);
  while (p < s.size() && s[p] == '@') {
    res = res * fact(++p);
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);

  while (cin >> s, s != "#") {
    int p = 0;
    P res = point(p).p;
    printf("%.10f %.10f\\n", res.X, res.Y);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <valarray>
#include <vector>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RFOR(i, c)                                                             \\
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a, b) make_pair(a, b)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &t) {
  os << "[";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "]";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &t) {
  os << "{";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
  return os << "(" << t.first << "," << t.second << ")";
}
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first - t.first, s.second - t.second);
}

namespace geom {
#define X real()
#define Y imag()
#define at(i) ((*this)[i])
#define SELF (*this)
enum { TRUE = 1, FALSE = 0, BORDER = -1 };
typedef int BOOL;
typedef double R;
const R INF = 1e8;
const R EPS = 1e-8;
const R PI = 3.1415926535897932384626;
inline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
inline BOOL less(const R &x, const R &y) { return sig(x - y) ? x < y : BORDER; }
typedef complex<R> P;
inline R norm(const P &p) { return p.X * p.X + p.Y * p.Y; }
inline R inp(const P &a, const P &b) { return (conj(a) * b).X; }
inline R outp(const P &a, const P &b) { return (conj(a) * b).Y; }
inline P unit(const P &p) { return p / abs(p); }
inline P proj(const P &s, const P &t) { return t * inp(s, t) / norm(t); }
inline int ccw(const P &s, const P &t, const P &p, int adv = 0) {
  int res = sig(outp(t - s, p - s));
  if (res || !adv)
    return res;
  if (sig(inp(t - s, p - s)) < 0)
    return -2; // p-s-t
  if (sig(inp(s - t, p - t)) < 0)
    return 2; // s-t-p
  return 0;   // s-p-t
}

struct L : public vector<P> { // line
  L(const P &p1, const P &p2) {
    this->push_back(p1);
    this->push_back(p2);
  }
  L() {}
  P dir() const { return at(1) - at(0); }
  BOOL online(const P &p) const { return !sig(outp(p - at(0), dir())); }
};
struct S : public L { // segment
  S(const P &p1, const P &p2) : L(p1, p2) {}
  S() {}
  BOOL online(const P &p) const {
    if (!sig(norm(p - at(0))) || !sig(norm(p - at(1))))
      return BORDER;
    return !sig(abs(at(0) - p) + abs(at(1) - p) - abs(at(0) - at(1)));
  }
};
struct C : public P {
  C() {}
  C(const P &p, const R r) : P(p), r(r) {}
  R r;
  BOOL inside(const P &p) const { return less(norm(p - SELF), r * r); }
};
struct F : public C {
  R s, t;
  F(const C &c, R ss, R tt) : C(c), s(ss), t(tt) {
    if (PI < s)
      s -= 2 * PI;
    if (PI < t)
      t -= 2 * PI;
  }
  BOOL inside(const P &p) const {
    P v = p - SELF;
    if (!sig(norm(v)))
      return BORDER;
    R a = arg(v);
    if (t < s) {
      if ((!less(s, a) && !less(a, t)) || !less(norm(v), r * r))
        return FALSE;
      return less(s, a) | less(a, t) | less(norm(v), r * r);
    } else {
      if (!less(s, a) || !less(a, t) || !less(norm(v), r * r))
        return FALSE;
      return less(s, a) | less(a, t) | less(norm(v), r * r);
    }
  }
};
P crosspoint(const L &l, const L &m);
struct G : public vector<P> {
  G(size_type size = 0) : vector(size) {}
  S edge(int i) const { return S(at(i), at(i + 1 == size() ? 0 : i + 1)); }
  BOOL contains(const P &p) const {
    R sum = .0;
    REP(i, size()) {
      if (S(at(i), at((i + 1) % size())).online(p))
        return BORDER; // online
      sum += arg((at(i) - p) / (at((i + 1) % size()) - p));
    }
    return !!sig(sum);
  }
  R area() const {
    R sum = 0;
    REP(i, size()) sum += outp(at(i), at((i + 1) % size()));
    return abs(sum / 2.);
  }

  G convex_hull(bool online = false) {
    if (size() < 2)
      return *this;
    sort(ALL(*this));
    G r;
    r.resize((int)size() * 2);
    int k = 0;
    for (int i = 0; i < size(); r[k++] = at(i++))
      while (k > 1 && ccw(r[k - 2], r[k - 1], at(i)) < 1 - online)
        k--;
    int t = k;
    for (int i = (int)size() - 1; i >= 0; r[k++] = at(i--))
      while (k > t && ccw(r[k - 2], r[k - 1], at(i)) < 1 - online)
        k--;
    r.resize(k - 1);
    return r;
  }
  G cut(const L &l) const {
    G g;
    REP(i, size()) {
      const S &s = edge(i);
      if (ccw(l[0], l[1], s[0], 0) >= 0)
        g.push_back(s[0]);
      if (ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0)
        g.push_back(crosspoint(s, l));
    }
    return g;
  }
  G Voronoi(const vector<P> &p, const int t) const {
    G g = *this;
    REP(i, p.size()) if (i != t) {
      const P m = (p[t] + p[i]) * 0.5;
      g = g.cut(L(m, m + (p[i] - p[t]) * P(0, 1)));
    }
    return g;
  }
};

inline P proj(const P &s, const L &t) {
  return t[0] + proj(s - t[0], t[1] - t[0]);
}
inline P reflect(const P &s, const L &t) { return 2. * proj(s, t) - s; }
inline S reflect(const S &s, const L &t) {
  return S(reflect(s[0], t), reflect(s[1], t));
}
inline P crosspoint(const L &l, const L &m) {
  R A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);
  if (!sig(abs(A)) && !sig(abs(B)))
    return m[0]; // same line
  if (abs(A) < EPS)
    assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}
#undef SELF
#undef at
} // namespace geom

using namespace geom;

namespace std {
bool operator<(const P &a, const P &b) {
  return sig(a.X - b.X) ? a.X < b.X : a.Y + EPS < b.Y;
}
bool operator==(const P &a, const P &b) { return !sig(norm(a - b)); }
/*	istream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return
   is;} istream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >>
   l[1];} istream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}
        const double B = 500;
        const double Z = 1;
        ostream& operator<<(ostream &os, const C &c){return os <<
   "circle("<<B+Z*(c.X)<<", "<<1000-B-Z*(c.Y)<<", "<<Z*(c.r)<<")";} ostream&
   operator<<(ostream &os, const P &p){return os << C(p, 2.);} ostream&
   operator<<(ostream &os, const S &s){return os << "line("<<B+Z*(s[0].X)<<",
   "<<1000-B-Z*(s[0].Y)<<", "<<B+Z*(s[1].X)<<", "<<1000-B-Z*(s[1].Y)<<")";}
        ostream& operator<<(ostream &os, const G &g){REP(i, g.size()) cout <<
   g.edge(i) << endl;return os;}
*/
} // namespace std

int n;
string s;

struct PL {
  int t;
  P p;
  L l;
  PL() {}
  PL(P p) : p(p), t(0) {}
  PL(L l) : l(l), t(1) {}
  PL operator*(const PL &opp) {
    if (!t && !opp.t)
      return PL(L(p, opp.p));
    if (t && opp.t)
      return PL(crosspoint(l, opp.l));
    if (!t && opp.t)
      return PL(reflect(p, opp.l));
    if (t && !opp.t)
      return PL(reflect(opp.p, l));
  }
};

R number(int &p) {
  int sig = 1;
  if (s.at(p) == '-') {
    sig = -1;
    p++;
  }
  int res = 0;
  while (isdigit(s.at(p)))
    res = res * 10 + s[p++] - '0';
  return sig * res;
}

PL point(int &p);
PL fact(int &p) {
  p++;
  PL res;
  if (s.at(p) == '(') {
    res = point(p);
    p++;
    return res;
  } else if (isdigit(s.at(p))) {
    R x = number(p);
    p++;
    R y = number(p);
    res = PL(P(x, y));
  }
  p++;
  return res;
}

PL point(int &p) {
  PL res = fact(p);
  while (p < s.size() && s.at(p) == '@') {
    res = res * fact(++p);
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);

  while (cin >> s, s != "#") {
    int p = 0;
    P res = point(p).p;
    printf("%.10f %.10f\\n", res.X, res.Y);
  }
  return 0;
}