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

#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

struct DATA {
  int start;
  int end;
  int fileID;
  DATA(int start, int end, int fileID) {
    this->start = start, this->end = end, this->fileID = fileID;
  }
};

vector<DATA> data;

void Write(int fileID, int file_length) {
  int size = (int)data.size();
  for (int i = 0; i < size; i++) {
    if (data[i].fileID == -1) {
      if (file_length <= data[i].end - data[i].start + 1) {
        data.insert(
            data.begin() + i,
            DATA(data[i].start, data[i].start + file_length - 1, fileID));
        data[i + 1].start = data[i + 1].start + file_length;
        return;
      } else {
        data[i].fileID = fileID;
        file_length -= data[i].end - data[i].start + 1;
      }
    }
  }
}

void Arrange() {
  for (int i = 0; i < (int)data.size() - 1; i++) {
    if (data[i].fileID == -1) {
      if (data[i + 1].fileID == -1) {
        data[i].end = data[i + 1].end;
        data.erase(data.begin() + (i + 1));
        i--;
      }
    }
  }
}

void Delete(int fileID) {
  int size = (int)data.size();
  for (int i = 0; i < size; i++) {
    if (data[i].fileID == fileID)
      data[i].fileID = -1;
  }
  Arrange();
}

void Reference(int pos) {
  int size = (int)data.size();
  for (int i = 0; i < size; i++) {
    if (data[i].end >= pos) {
      printf("%d\\n", data[i].fileID);
      return;
    }
  }
}

int main(void) {
  int N;
  char com[5];
  int op1, op2;

  while (1) {
    scanf("%d", &N);
    if (N == 0)
      break;
    data.clear();
    data.push_back(DATA(0, 1000000001, -1));

    for (int i = 0; i < N; i++) {

      scanf("%s", com);
      if (strcmp(com, "W") == 0) {
        scanf("%d %d", &op1, &op2);
        Write(op1, op2);
      } else if (strcmp(com, "D") == 0) {
        scanf("%d", &op1);
        Delete(op1);
      } else if (strcmp(com, "R") == 0) {
        scanf("%d", &op1);
        Reference(op1);
      }
    }
    printf("\\n");
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

struct DATA {
  int start;
  int end;
  int fileID;
  DATA(int start, int end, int fileID) {
    this->start = start, this->end = end, this->fileID = fileID;
  }
};

vector<DATA> data;

void Write(int fileID, int file_length) {
  int size = (int)data.size();
  for (int i = 0; i < size; i++) {
    if (data[i].fileID == -1) {
      if (file_length <= data[i].end - data[i].start + 1) {
        data.insert(
            data.begin() + i,
            DATA(data[i].start, data[i].start + file_length - 1, fileID));
        data[i + 1].start = data[i + 1].start + file_length;
      } else {
        data[i].fileID = fileID;
        file_length -= data[i].end - data[i].start + 1;
      }
    }
  }
}

void Arrange() {
  for (int i = 0; i < (int)data.size() - 1; i++) {
    if (data[i].fileID == -1) {
      if (data[i + 1].fileID == -1) {
        data[i].end = data[i + 1].end;
        data.erase(data.begin() + (i + 1));
        i--;
      }
    }
  }
}

void Delete(int fileID) {
  int size = (int)data.size();
  for (int i = 0; i < size; i++) {
    if (data[i].fileID == fileID)
      data[i].fileID = -1;
  }
  Arrange();
}

void Reference(int pos) {
  int size = (int)data.size();
  for (int i = 0; i < size; i++) {
    if (data[i].end >= pos) {
      printf("%d\\n", data[i].fileID);
      return;
    }
  }
}

int main(void) {
  int N;
  char com[5];
  int op1, op2;

  while (1) {
    scanf("%d", &N);
    if (N == 0)
      break;
    data.push_back(DATA(0, 1000000001, -1));

    for (int i = 0; i < N; i++) {

      scanf("%s", com);
      if (strcmp(com, "W") == 0) {
        scanf("%d %d", &op1, &op2);
        Write(op1, op2);
      } else if (strcmp(com, "D") == 0) {
        scanf("%d", &op1);
        Delete(op1);
      } else if (strcmp(com, "R") == 0) {
        scanf("%d", &op1);
        Reference(op1);
      }
    }
  }
  return 0;
}