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

#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
struct Data {
  ll start, end, id;
  Data *next;
  Data *prev;
  Data(ll s, ll e, int i) : start(s), end(e), id(i) { ; }
};

void insert(int id, ll size, Data *d) {
  while (true) {
    // cout<<(d->start)<<","<<(d->end)<<":"<<(d->id)<<endl;
    if (size == 0)
      return;
    if (d->id != -1) {
      d = d->next;
      continue;
    }
    ll use = min(size, (d->end) - (d->start) + 1);
    if (use == size) {
      ll remain = (d->end) - (d->start) + 1 - size;
      if (remain == 0)
        d->id = id;
      else {
        Data *nd = new Data(d->start + size, d->end, -1);
        nd->prev = d;
        nd->next = d->next;
        d->next = nd;
        d->end = d->start + size - 1;
        d->id = id;
      }
      break;
    } else {
      ll remain = size - ((d->end) - (d->start) + 1);
      d->id = id;
      size = remain;
      d = d->next;
    }
  }
}

int lookup(Data *n, ll p) {
  while (true) {
    if (n == NULL)
      return -1;
    if (n->start <= p && n->end >= p)
      return n->id;
    n = n->next;
  }
}

void remove(int id, Data *n) {
  while (n != NULL) {
    if (n == NULL)
      return;
    if ((n->id) != id) {
      n = n->next;
      continue;
    }

    n->id = -1;
    n = n->next;
  }
}

void dump(Data *n) {
  while (true) {
    if (n == NULL)
      return;
    // cout<<(n->start)<<" "<<(n->end)<<" "<<(n->id)<<endl;
    n = n->next;
  }
}

int main() {
  int n;
  string op;
  while (cin >> n, n) {
    Data root(0, 1ll << 60, -1);
    Data *rp = &root;
    root.prev = &root;
    root.next = NULL;

    ll id, size;
    for (int i = 0; i < n; ++i) {
      cin >> op;
      if (op == "W") {
        cin >> id >> size;
        // cout<<"W "<<id<<" "<<size<<endl;
        insert(id, size, rp);
        // dump(rp);
        // cout<<endl;
      } else if (op == "D") {
        cin >> id;
        // cout<<"D "<<id<<endl;
        remove(id, rp);
        // dump(rp);
        // cout<<endl;
      } else if (op == "R") {
        cin >> size;
        cout << lookup(rp, size) << endl;
      }
    }
    cout << endl;
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
struct Data {
  int start, end, id;
  Data *next;
  Data *prev;
  Data(int s, int e, int i) : start(s), end(e), id(i) { ; }
};

void insert(int id, int size, Data *d) {
  if (size == 0)
    return;
  if (d->id != -1) {
    insert(id, size, d->next);
    return;
  }
  int use = min(size, (d->end) - (d->start) + 1);
  if (use == size) {
    int remain = (d->end) - (d->start) + 1 - size;
    if (remain == 0)
      d->id = id;
    else {
      Data *nd = new Data(d->start + size, d->end, -1);
      nd->prev = d;
      nd->next = d->next;
      d->next = nd;
      d->end = d->start + size - 1;
      d->id = id;
    }
  } else {
    int remain = size - ((d->end) - (d->start) + 1);
    d->id = id;
    insert(id, remain, d->next);
  }
}

int lookup(Data *n, ll p) {
  if (n == NULL)
    return -1;
  if (n->start <= p && n->end >= p)
    return n->id;
  lookup(n->next, p);
}

void remove(int id, Data *n) {
  if (n == NULL)
    return;
  if ((n->id) != id) {
    remove(id, n->next);
    return;
  }
  n->id = -1;
  if (n->prev != n && n->prev->id == -1) {
    n->prev->next = n->next;
    n->prev->end = n->end;
    Data *ne = n->next;
    delete n;
    remove(id, ne);
  }
}

int main() {
  int n;
  string op;
  while (cin >> n, n) {
    Data root(0, 1 << 30, -1);
    Data *rp = &root;
    root.prev = &root;
    root.next = NULL;

    int id, size;
    for (int i = 0; i < n; ++i) {
      cin >> op;
      if (op == "W") {
        cin >> id >> size;
        insert(id, size, rp);
      } else if (op == "D") {
        cin >> id;
        remove(id, rp);
      } else if (op == "R") {
        cin >> size;
        cout << lookup(rp, size) << endl;
      }
    }
    cout << endl;
  }
}