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

#include <iostream>
using namespace std;
typedef long long ll;

struct newrm {
  ll start, end, dot;
  newrm *next;
};
void write(newrm *begin, ll i, ll s) {
  ll tmp;
  newrm *prev, *pres, *sinki;

  prev = begin;
  pres = prev->next;

  while (pres != NULL) {
    tmp = pres->start - prev->end;
    if (tmp == 1) {
    } else {
      sinki = new newrm();
      sinki->start = prev->end + 1;
      sinki->dot = i;
      if (tmp - 1 < s) {
        sinki->end = pres->start - 1;
        s -= tmp - 1;
      } else {
        sinki->end = sinki->start + s - 1;
        s = 0;
      }
      prev->next = sinki;
      sinki->next = pres;
      if (s == 0)
        return;
    }
    prev = prev->next;
    pres = prev->next;
  }
  //  cout<<"test"<<endl;
  sinki = new newrm();
  prev->next = sinki;
  sinki->start = prev->end + 1;
  sinki->end = sinki->start + s - 1;
  sinki->dot = i;
  sinki->next = NULL;
}

void read(newrm *begin, ll p) {
  // cout<<"read!\\n";

  newrm *prev, *pres;
  prev = begin;
  pres = prev->next;
  while (pres != NULL) {
    if (pres->start <= p && pres->end >= p) {
      cout << pres->dot << endl;
      return;
    } else if (pres->end < p) {
      //  cout<<prev->end<<" | "<<pres->start<<endl;
      prev = pres;
      pres = pres->next;
      // cout<<prev->end<<" || "<<pres->start<<endl;
      continue;
    } else {
      cout << -1 << endl;
      return;
    }
  }
  cout << -1 << endl;
}

void del(newrm *begin, ll i) {
  newrm *prev, *pres, *tmp;
  prev = begin;
  pres = prev->next;
  while (pres != NULL) {
    if (pres->dot == i) {
      tmp = pres;
      prev->next = pres->next;
      pres = prev->next;
      //      cout<<"debug"<<tmp->start<<endl;
      delete tmp;
      continue;
    }
    prev = prev->next;
    pres = prev->next;
  }
}
void reset(newrm *begin) {
  newrm *prev, *pres;
  prev = begin;
  pres = prev->next;
  while (pres != NULL) {
    delete prev;
    prev = pres;
    pres = pres->next;
  }
  delete prev;
  return;
}
void debug(newrm *begin) {
  ll i;
  while (begin != NULL) {
    for (i = begin->start; i <= begin->end; i++)
      cout << " " << begin->dot << " ";
    if (begin->next == NULL)
      break;
    for (; i < begin->next->start; i++)
      cout << " " << 'x' << " ";
    begin = begin->next;
  }
  cout << endl;
}
int main() {
  ll N, i, p, s;
  char com;
  while (1) {
    newrm *begin = new newrm();
    begin->start = -2;
    begin->end = -1;
    begin->dot = -1;
    begin->next = NULL;
    cin >> N;

    if (N == 0)
      return 0;
    for (ll i = 0; i < N; i++) {
      cin >> com;

      if (com == 'W') {
        cin >> p >> s;
        write(begin, p, s);
      } else if (com == 'D') {
        cin >> p;
        del(begin, p);
      } else if (com == 'R') {
        cin >> p;
        read(begin, p);
      }
      //      else if(com=='B')
      // debug(begin);
    }
    //     reset(begin);
    cout << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
typedef long long ll;

struct newrm {
  int start, end, dot;
  newrm *next;
};
void write(newrm *begin, int i, int s) {
  int tmp;
  newrm *prev, *pres, *sinki;

  prev = begin;
  pres = prev->next;

  while (pres != NULL) {
    tmp = pres->start - prev->end;
    if (tmp == 1) {
      prev = pres;
      pres = pres->next;
    } else {
      sinki = new newrm();
      sinki->start = prev->end + 1;
      sinki->dot = i;
      if (tmp - 1 < s) {
        sinki->end = pres->start - 1;
        s -= tmp - 1;
      } else {
        sinki->end = sinki->start + s - 1;
        s = 0;
      }
      prev->next = sinki;
      sinki->next = pres;
      if (s == 0)
        return;
    }
  }
  sinki = new newrm();
  prev->next = sinki;
  sinki->start = prev->end + 1;
  sinki->end = sinki->start + s - 1;
  sinki->dot = i;
  sinki->next = NULL;
}

void read(newrm *begin, int p) {
  // cout<<"read!\\n";
  newrm *prev, *pres;
  prev = begin;
  pres = prev->next;
  while (pres != NULL) {
    if (pres->start <= p && pres->end >= p) {
      cout << pres->dot << endl;
      return;
    } else if (pres->end < p) {
      //  cout<<prev->end<<" | "<<pres->start<<endl;
      prev = pres;
      pres = pres->next;
      // cout<<prev->end<<" || "<<pres->start<<endl;
      continue;
    } else {
      cout << -1 << endl;
      return;
    }
  }
  cout << -1 << endl;
}
void del(newrm *begin, int i) {
  newrm *prev, *pres, *tmp;
  prev = begin;
  pres = prev->next;
  while (pres != NULL) {
    if (pres->dot == i) {
      tmp = pres;
      prev->next = pres->next;
      pres = prev->next;
      //      cout<<"debug"<<tmp->start<<endl;
      delete tmp;
      continue;
    }
    prev = prev->next;
    pres = prev->next;
  }
}
void reset(newrm *begin) {
  newrm *prev, *pres;
  prev = begin;
  pres = prev->next;
  while (pres != NULL) {
    delete prev;
    prev = pres;
    pres = pres->next;
  }
  delete prev;
  return;
}
void debug(newrm *begin) {
  while (begin != NULL) {
    cout << begin->start << " " << begin->end << endl;
    begin = begin->next;
  }
}
int main() {
  int N, i, p, s;
  char com;

  while (1) {
    newrm *begin = new newrm();
    begin->start = -2;
    begin->end = -1;
    begin->dot = -1;
    begin->next = NULL;
    cin >> N;

    if (N == 0)
      return 0;
    for (int i = 0; i < N; i++) {
      cin >> com;

      if (com == 'W') {
        cin >> p >> s;
        write(begin, p, s);
      } else if (com == 'D') {
        cin >> p;
        del(begin, p);
      } else if (com == 'R') {
        cin >> p;
        read(begin, p);
      } else if (com == 'B')
        debug(begin);
    }
    reset(begin);
    cout << endl;
  }
  return 0;
}