Problem p01057 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

struct node {
  char c;
  node *next, *down;
  int siz;
};

const double p = 0.25;
const int max_height = 8;
node null;

struct slis {
  int n;
  node head;
  void show() {
    node *h[max_height];
    h[max_height - 1] = &head;
    rrep(i, max_height - 1) h[i] = (*h[i + 1]).down;
    rrep(i, max_height) {
      printf("%d:", i);
      while (h[i] != &null) {
        printf("%c,%d ", h[i]->c, h[i]->siz);
        h[i] = h[i]->next;
      }
      cout << endl;
    }
  }
  void show_() {
    node *id = &head;
    while (id->down != &null)
      id = id->down;
    while (id->next != &null) {
      id = id->next;
      printf("%c", id->c);
    }
    puts("");
  }
};

node *init() { // 0???????????????
  node *h = new node;
  node *head[max_height];
  head[max_height - 1] = h;
  rrep1(i, max_height - 1) {
    head[i]->c = '$';
    head[i]->down = new node;
    head[i - 1] = head[i]->down;
  }
  head[0]->c = '$';
  head[0]->down = &null;
  return h;
}

slis *mkslis(int n, char *c, slis *ret) {

  //???????´??????????????±??????????
  vector<int> height;
  rep(i, n) {
    int h = 1;
    while (h < max_height && rand() % 100 < p * 100.0)
      h++;
    height.pb(h);
  }

  // 0???????????????
  node *head[max_height];
  head[max_height - 1] = init();
  rrep1(i, max_height - 1) head[i - 1] = head[i]->down;
  /*node* h = new node;
  node* head[max_height];
  head[max_height-1] = h;
  rrep1(i,max_height-1){
          head[i]->c = '$';
          head[i]->down = new node;
          head[i-1] = head[i]->down;
  }
  head[0]->c = '$';
  head[0]->down = &null;*/

  //??¬????????????
  vector<node *> down;
  rep(i, n) down.pb(&null);
  rep(i, max_height) {
    node *id = head[i];
    int loc = 0;
    rep(j, n) {
      if (i < height[j]) {
        (*id).next = new node;
        (*id).siz = j + 1 - loc;
        loc = j + 1;
        id = (*id).next;
        (*id).c = *(c + j);
        (*id).down = down[j];
        down[j] = id;
      }
    }
    (*id).next = &null;
    (*id).siz = n - loc;
  }

  (*ret).n = n;
  (*ret).head = *head[max_height - 1];
  return ret;
}

pair<slis *, slis *> split(slis *a, slis *b, int k) {
  // b???0???????????????
  node *head[max_height];
  head[max_height - 1] = init();
  rrep1(i, max_height - 1) head[i - 1] = head[i]->down;

  // split
  node *id = &a->head;
  int loc = 0;
  int h = max_height - 1;
  while (1) {
    while (loc + id->siz <= k) {
      loc += id->siz;
      id = id->next;
    }
    head[h]->next = id->next;
    head[h]->siz = loc + id->siz - k;
    id->next = &null;
    id->siz = k - loc;
    if (id->down == &null)
      break;
    id = id->down;
    h--;
  }

  //??????
  b->n = a->n - k;
  a->n = k;
  b->head = *head[max_height - 1];
  return pair<slis *, slis *>(a, b);
}

slis *merge(slis *a, slis *b) {
  node *id0 = &a->head;
  node *id1 = &b->head;
  while (id0 != &null) {
    while (id0->next != &null)
      id0 = id0->next;
    id0->next = id1->next;
    id0->siz += id1->siz;
    id0 = id0->down;
    id1 = id1->down;
  }
  a->n += b->n;
  return a;
}

void updata(slis *lis, int k, char c) {
  node *id = &lis->head;
  int loc = 0;
  while (id != &null) {
    while (id->next != &null && loc + id->siz <= k) {
      loc += id->siz;
      id = id->next;
    }
    if (loc == k)
      id->c = c;
    id = id->down;
  }
}

int main() {
  static int n, q;
  static char c[2000010];
  static slis *s[100010];
  scanf("%d%d", &n, &q);
  rep(i, n) {
    scanf("%s", &c);
    int siz = 0;
    while (c[siz] != '\\0')
      siz++;
    s[i] = mkslis(siz, c, new slis);
    rep(j, siz) c[j] = '\\0';
  }
  rep(i, q) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      int a, b, c, d;
      scanf("%d%d%d%d", &a, &b, &c, &d);
      pair<slis *, slis *> p = split(s[a - 1], new slis, b - 1);
      pair<slis *, slis *> q = split(s[c - 1], new slis, d - 1);
      s[a - 1] = merge(p.fr, q.sc);
      s[c - 1] = merge(q.fr, p.sc);
    } else {
      int a, b;
      char c_;
      scanf("%d%d %c", &a, &b, &c_);
      updata(s[a - 1], b, c_);
    }
  }

  rep(i, n) { s[i]->show_(); }

  /*int n;
  char c[30];
  scanf("%d %s",&n,&c);
  slis *test = new slis;
  test = mkslis(n,c,test);
  test->show();
  int k;
  scanf("%d",&k);
  pair<slis*,slis*> p = split(test,new slis,k);
  p.fr->show();
  p.sc->show();
  test = merge(p.fr,p.sc);
  test->show();*/
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

struct node {
  char c;
  node *next, *down;
  int siz;
};

const double p = 0.5;
const int max_height = 10;
node null;

struct slis {
  int n;
  node head;
  void show() {
    node *h[max_height];
    h[max_height - 1] = &head;
    rrep(i, max_height - 1) h[i] = (*h[i + 1]).down;
    rrep(i, max_height) {
      printf("%d:", i);
      while (h[i] != &null) {
        printf("%c,%d ", h[i]->c, h[i]->siz);
        h[i] = h[i]->next;
      }
      cout << endl;
    }
  }
  void show_() {
    node *id = &head;
    while (id->down != &null)
      id = id->down;
    while (id->next != &null) {
      id = id->next;
      printf("%c", id->c);
    }
    puts("");
  }
};

node *init() { // 0???????????????
  node *h = new node;
  node *head[max_height];
  head[max_height - 1] = h;
  rrep1(i, max_height - 1) {
    head[i]->c = '$';
    head[i]->down = new node;
    head[i - 1] = head[i]->down;
  }
  head[0]->c = '$';
  head[0]->down = &null;
  return h;
}

slis *mkslis(int n, char *c, slis *ret) {

  //???????´??????????????±??????????
  vector<int> height;
  rep(i, n) {
    int h = 1;
    while (h < max_height && rand() % 100 < p * 100.0)
      h++;
    height.pb(h);
  }

  // 0???????????????
  node *head[max_height];
  head[max_height - 1] = init();
  rrep1(i, max_height - 1) head[i - 1] = head[i]->down;
  /*node* h = new node;
  node* head[max_height];
  head[max_height-1] = h;
  rrep1(i,max_height-1){
          head[i]->c = '$';
          head[i]->down = new node;
          head[i-1] = head[i]->down;
  }
  head[0]->c = '$';
  head[0]->down = &null;*/

  //??¬????????????
  vector<node *> down;
  rep(i, n) down.pb(&null);
  rep(i, max_height) {
    node *id = head[i];
    int loc = 0;
    rep(j, n) {
      if (i < height[j]) {
        (*id).next = new node;
        (*id).siz = j + 1 - loc;
        loc = j + 1;
        id = (*id).next;
        (*id).c = *(c + j);
        (*id).down = down[j];
        down[j] = id;
      }
    }
    (*id).next = &null;
    (*id).siz = n - loc;
  }

  (*ret).n = n;
  (*ret).head = *head[max_height - 1];
  return ret;
}

pair<slis *, slis *> split(slis *a, slis *b, int k) {
  // b???0???????????????
  node *head[max_height];
  head[max_height - 1] = init();
  rrep1(i, max_height - 1) head[i - 1] = head[i]->down;

  // split
  node *id = &a->head;
  int loc = 0;
  int h = max_height - 1;
  while (1) {
    while (loc + id->siz <= k) {
      loc += id->siz;
      id = id->next;
    }
    head[h]->next = id->next;
    head[h]->siz = loc + id->siz - k;
    id->next = &null;
    id->siz = k - loc;
    if (id->down == &null)
      break;
    id = id->down;
    h--;
  }

  //??????
  b->n = a->n - k;
  a->n = k;
  b->head = *head[max_height - 1];
  return pair<slis *, slis *>(a, b);
}

slis *merge(slis *a, slis *b) {
  node *id0 = &a->head;
  node *id1 = &b->head;
  while (id0 != &null) {
    while (id0->next != &null)
      id0 = id0->next;
    id0->next = id1->next;
    id0->siz += id1->siz;
    id0 = id0->down;
    id1 = id1->down;
  }
  a->n += b->n;
  return a;
}

void updata(slis *lis, int k, char c) {
  node *id = &lis->head;
  int loc = 0;
  while (id != &null) {
    while (id->next != &null && loc + id->siz <= k) {
      loc += id->siz;
      id = id->next;
    }
    if (loc == k)
      id->c = c;
    id = id->down;
  }
}

int main() {
  static int n, q;
  static char c[2000010];
  static slis *s[100010];
  scanf("%d%d", &n, &q);
  rep(i, n) {
    scanf("%s", &c);
    int siz = 0;
    while (c[siz] != '\\0')
      siz++;
    s[i] = mkslis(siz, c, new slis);
    rep(j, siz) c[j] = '\\0';
  }
  rep(i, q) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      int a, b, c, d;
      scanf("%d%d%d%d", &a, &b, &c, &d);
      pair<slis *, slis *> p = split(s[a - 1], new slis, b - 1);
      pair<slis *, slis *> q = split(s[c - 1], new slis, d - 1);
      s[a - 1] = merge(p.fr, q.sc);
      s[c - 1] = merge(q.fr, p.sc);
    } else {
      int a, b;
      char c_;
      scanf("%d%d %c", &a, &b, &c_);
      updata(s[a - 1], b, c_);
    }
  }

  rep(i, n) { s[i]->show_(); }

  /*int n;
  char c[30];
  scanf("%d %s",&n,&c);
  slis *test = new slis;
  test = mkslis(n,c,test);
  test->show();
  int k;
  scanf("%d",&k);
  pair<slis*,slis*> p = split(test,new slis,k);
  p.fr->show();
  p.sc->show();
  test = merge(p.fr,p.sc);
  test->show();*/
}