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

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define sz 200009
#define mx 10000000000008
#define mod 1000000007
int n, q, arr[sz], brr[sz], crr[sz], t[4 * sz], t1[4 * sz];
bool marked[4 * sz], marked1[4 * sz];
void done(int node) {
  if (marked[node]) {
    marked[2 * node] = marked[2 * node + 1] = true;
    t[2 * node] = t[2 * node + 1] = t[node];
    marked[node] = false;
  }
  return;
}
void koro(int node, int l, int r, int val) {
  if (t[node] == 0) {
    t[node] = val;
    marked[node] = true;
    return;
  }
  if (l == r || marked[node] == true) {
    t[node] = min(t[node], val);
    marked[node] = true;
    return;
  }
  if (t[node] <= val)
    return;
  int mid = (l + r) / 2;
  koro(2 * node, l, mid, val);
  koro(2 * node + 1, mid + 1, r, val);
  t[node] = max(t[2 * node], t[2 * node + 1]);
  return;
}
void update(int node, int l, int r, int indl, int indr, int val) {
  if (indl > indr || indl > r || indr < l)
    return;
  if (l >= indl && r <= indr) {
    koro(node, l, r, val);
    return;
  }
  done(node);
  int mid = (l + r) / 2;
  update(node * 2, l, mid, indl, indr, val);
  update(node * 2 + 1, mid + 1, r, indl, indr, val);
  t[node] = max(t[2 * node], t[2 * node + 1]);
  return;
}
int query(int node, int l, int r, int le, int ri) {
  if (le > ri || l > ri || r < le)
    return 0;
  if (le <= l && r <= ri)
    return t[node];
  done(node);
  int mid = (l + r) / 2;
  return ((query(2 * node, l, mid, le, ri) +
           query(2 * node + 1, mid + 1, r, le, ri)));
}
void done1(int node) {
  if (marked1[node]) {
    marked1[2 * node] = marked1[2 * node + 1] = true;
    t1[2 * node] = t1[2 * node + 1] = t1[node];
    marked1[node] = false;
  }
  return;
}
void koro1(int node, int l, int r, int val) {
  if (t1[node] == 0) {
    t1[node] = val;
    marked1[node] = true;
    return;
  }
  if (l == r || marked1[node] == true) {
    t1[node] = min(t1[node], val);
    marked1[node] = true;
    return;
  }
  if (t1[node] <= val)
    return;
  int mid = (l + r) / 2;
  koro1(2 * node, l, mid, val);
  koro1(2 * node + 1, mid + 1, r, val);
  t1[node] = max(t1[2 * node], t1[2 * node + 1]);
  return;
}
void update1(int node, int l, int r, int indl, int indr, int val) {
  if (indl > indr || indl > r || indr < l)
    return;
  if (l >= indl && r <= indr) {
    koro1(node, l, r, val);
    return;
  }
  done1(node);
  int mid = (l + r) / 2;
  update1(node * 2, l, mid, indl, indr, val);
  update1(node * 2 + 1, mid + 1, r, indl, indr, val);
  t1[node] = max(t1[2 * node], t1[2 * node + 1]);
  return;
}
int query1(int node, int l, int r, int le, int ri) {
  if (le > ri || l > ri || r < le)
    return 0;
  if (le <= l && r <= ri)
    return t1[node];
  done1(node);
  int mid = (l + r) / 2;
  return ((query1(2 * node, l, mid, le, ri) +
           query1(2 * node + 1, mid + 1, r, le, ri)));
}
int main() {
  scanf("%d %d", &n, &q);
  ll aa = n;
  ll ans = (aa - 2) * (aa - 2);
  //  printf("%lld %d %lld\\n",ans,n,(n-2)*(n-2));
  update(1, 1, n, 1, n, n);
  update1(1, 1, n, 1, n, n);
  // printf("    %intd\\n",ans);
  for (int i = 1; i <= q; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    if (a == 1) {
      int val = query1(1, 1, n, b, b);
      //  printf("    %d %lld\\n",val,ans);
      ans -= val - 2;
      //  printf("        %lld\\n",ans);
      update(1, 1, n, 1, val, b);
    } else {
      int val = query(1, 1, n, b, b);
      ans -= val - 2;
      update1(1, 1, n, 1, val, b);
    }
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define sz 200009
#define mx 10000000000008
#define mod 1000000007
int n, q, arr[sz], brr[sz], crr[sz], t[4 * sz], t1[4 * sz];
bool marked[4 * sz], marked1[4 * sz];
void done(int node) {
  if (marked[node]) {
    marked[2 * node] = marked[2 * node + 1] = true;
    t[2 * node] = t[2 * node + 1] = t[node];
    marked[node] = false;
  }
  return;
}
void koro(int node, int l, int r, int val) {
  if (t[node] == 0) {
    t[node] = val;
    marked[node] = true;
    return;
  }
  if (l == r || marked[node] == true) {
    t[node] = min(t[node], val);
    marked[node] = true;
    return;
  }
  int mid = (l + r) / 2;
  koro(2 * node, l, mid, val);
  koro(2 * node + 1, mid + 1, r, val);
  return;
}
void update(int node, int l, int r, int indl, int indr, int val) {
  if (indl > indr || indl > r || indr < l)
    return;
  if (l >= indl && r <= indr) {
    koro(node, l, r, val);
    return;
  }
  done(node);
  int mid = (l + r) / 2;
  update(node * 2, l, mid, indl, indr, val);
  update(node * 2 + 1, mid + 1, r, indl, indr, val);
  return;
}
int query(int node, int l, int r, int le, int ri) {
  if (le > ri || l > ri || r < le)
    return 0;
  if (le <= l && r <= ri)
    return t[node];
  done(node);
  int mid = (l + r) / 2;
  return ((query(2 * node, l, mid, le, ri) +
           query(2 * node + 1, mid + 1, r, le, ri)));
}
void done1(int node) {
  if (marked1[node]) {
    marked1[2 * node] = marked1[2 * node + 1] = true;
    t1[2 * node] = t1[2 * node + 1] = t1[node];
    marked1[node] = false;
  }
  return;
}
void koro1(int node, int l, int r, int val) {
  if (t1[node] == 0) {
    t1[node] = val;
    marked1[node] = true;
    return;
  }
  if (l == r || marked1[node] == true) {
    t1[node] = min(t1[node], val);
    marked1[node] = true;
    return;
  }
  int mid = (l + r) / 2;
  koro1(2 * node, l, mid, val);
  koro1(2 * node + 1, mid + 1, r, val);
  return;
}
void update1(int node, int l, int r, int indl, int indr, int val) {
  if (indl > indr || indl > r || indr < l)
    return;
  if (l >= indl && r <= indr) {
    koro1(node, l, r, val);
    return;
  }
  done1(node);
  int mid = (l + r) / 2;
  update1(node * 2, l, mid, indl, indr, val);
  update1(node * 2 + 1, mid + 1, r, indl, indr, val);
  return;
}
int query1(int node, int l, int r, int le, int ri) {
  if (le > ri || l > ri || r < le)
    return 0;
  if (le <= l && r <= ri)
    return t1[node];
  done1(node);
  int mid = (l + r) / 2;
  return ((query1(2 * node, l, mid, le, ri) +
           query1(2 * node + 1, mid + 1, r, le, ri)));
}
int main() {
  scanf("%d %d", &n, &q);
  ll aa = n;
  ll ans = (aa - 2) * (aa - 2);
  //  printf("%lld %d %lld\\n",ans,n,(n-2)*(n-2));
  update(1, 1, n, 1, n, n);
  update1(1, 1, n, 1, n, n);
  // printf("    %intd\\n",ans);
  for (int i = 1; i <= q; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    if (a == 1) {
      int val = query1(1, 1, n, b, b);
      //  printf("    %d %lld\\n",val,ans);
      ans -= val - 2;
      //  printf("        %lld\\n",ans);
      update(1, 1, n, 1, val, b);
    } else {
      int val = query(1, 1, n, b, b);
      ans -= val - 2;
      update1(1, 1, n, 1, val, b);
    }
  }
  printf("%lld\\n", ans);
  return 0;
}