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

#include <cmath>
#include <complex>
#include <iostream>
#include <tuple>

using namespace std;
using vec = complex<double>;
using line = pair<vec, vec>;

double inner_product(vec u, vec v) { return real(u * conj(v)); }
double cross_product(vec u, vec v) { return imag(conj(u) * v); }

vec projection(line l, vec p) { // p???l???????°???±
  vec s = l.first, t = l.second;
  double k = inner_product(t - s, p - s) / inner_product(t - s, t - s);
  return (1.0 - k) * s + k * t;
}

vec reflection(line l, vec p) { return 2.0 * projection(l, p) - p; }

vec interSection(line l0, line l1) {
  vec s0, t0, s1, t1;
  tie(s0, t0) = l0;
  tie(s1, t1) = l1;
  double k = cross_product(t1 - s1, s1 - s0) / cross_product(t1 - s1, t0 - s0);
  return s0 + (t0 - s0) * k;
}

double segLength(line l) { return abs(l.first - l.second); }

int number(string S, int &ite) {
  int ret = 0;
  int sign = 1;
  if (S[ite] == '-') {
    sign = -1;
    ite++;
  }
  while ('0' <= S[ite] && S[ite] <= '9') {
    ret *= 10;
    ret += S[ite] - '0';
    ite++;
  }
  ret *= sign;
  return ret;
}

line ope(line a, line b) {
  if (segLength(a) == 0 && segLength(b) == 0) {
    return line(a.first, b.first);
  } else if (segLength(a) == 0) {
    return line(reflection(b, a.first), reflection(b, a.first));
  } else if (segLength(b) == 0) {
    return line(reflection(a, b.first), reflection(a, b.first));
  } else {
    return line(interSection(a, b), interSection(a, b));
  }
}

line parse(string S, int &ite) {
  line ret;
  vec v1, v2;
  if ('0' <= S[ite] && S[ite] <= '9' || S[ite] == '-') {
    int x = number(S, ite);
    ite++; //','??????????£???°???
    int y = number(S, ite);
    return line(vec(x, y), vec(x, y));
  }
  while (ite < S.size()) {
    if (S[ite] == ')')
      break;
    if (S[ite] == '(') {
      ite++; //'('
      ret = parse(S, ite);
      ite++; //')'
    } else if (S[ite] == '@') {
      ite++; //'@'
      ite++; //'('
      ret = ope(ret, parse(S, ite));
      ite++; //')'
    }
  }
  return ret;
}

int main() {
  string S;
  while (cin >> S, S[0] != '#') {
    int ite = 0;
    vec ans = parse(S, ite).first;
    cout << fixed;
    cout.precision(8);
    cout << real(ans) << ' ' << imag(ans) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <complex>
#include <iostream>
#include <tuple>

using namespace std;
using vec = complex<double>;
using line = pair<vec, vec>;

double inner_product(vec u, vec v) { return real(u * conj(v)); }
double cross_product(vec u, vec v) { return imag(conj(u) * v); }

vec projection(line l, vec p) { // p???l???????°???±
  vec s = l.first, t = l.second;
  double k = inner_product(t - s, p - s) / inner_product(t - s, t - s);
  return (1.0 - k) * s + k * t;
}

vec reflection(line l, vec p) { return 2.0 * projection(l, p) - p; }

vec interSection(line l0, line l1) {
  vec s0, t0, s1, t1;
  tie(s0, t0) = l0;
  tie(s1, t1) = l1;
  double k = cross_product(t1 - s1, s1 - s0) / cross_product(t1 - s1, t0 - s0);
  return s0 + (t0 - s0) * k;
}

double segLength(line l) { return abs(l.first - l.second); }

int number(string S, int &ite) {
  int ret = 0;
  int sign = 1;
  if (S[ite] == '-') {
    sign = -1;
    ite++;
  }
  while ('0' <= S[ite] && S[ite] <= '9') {
    ret *= 10;
    ret += S[ite] - '0';
    ite++;
  }
  return ret * sign;
}

line ope(line a, line b) {
  if (segLength(a) == 0 && segLength(b) == 0) {
    return line(a.first, b.first);
  } else if (segLength(a) == 0) {
    return line(reflection(b, a.first), reflection(b, a.first));
  } else if (segLength(b) == 0) {
    return line(reflection(a, b.first), reflection(a, b.first));
  } else {
    return line(interSection(a, b), interSection(a, b));
  }
}

line parse(string S, int &ite) {
  line left, right;
  vec v1, v2;
  if ('0' <= S[ite] && S[ite] <= '9') {
    int x = number(S, ite);
    ite++; //','??????????£???°???
    int y = number(S, ite);
    return line(vec(x, y), vec(x, y));
  }
  while (ite < S.size()) {
    if (S[ite] == ')')
      break;
    if (S[ite] == '(') {
      ite++; //'('
      left = parse(S, ite);
      ite++; //')'
    } else if (S[ite] == '@') {
      ite++; //'@'
      ite++; //'('
      right = parse(S, ite);
      ite++; //')'
      left = ope(left, right);
    }
  }
  return left;
}

int main() {
  string S;
  while (cin >> S, S[0] != '#') {
    int ite = 0;
    vec ans = parse(S, ite).first;
    cout << fixed;
    cout.precision(8);
    cout << real(ans) << ' ' << imag(ans) << endl;
  }

  return 0;
}