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

/*
 *   Copyright (C) 2015 All rights reserved.
 *
 *   filename: 1341.cpp
 *   author: doublehh
 *   e-mail: sserdoublehh@foxmail.com
 *   create time: 2015-03-11
 *   last modified: 2015-03-11 23:11:23
 */
#include <bits/stdc++.h>
using namespace std;

const int maxn = 4e5;
struct Point {
  int x, y, z, f;
};
int n, m;
Point pt[maxn];
int c[maxn];
int z[maxn];

bool cmpxY(const Point &p1, const Point &p2) {
  return p1.x < p2.x || (p1.x == p2.x && p1.y > p2.y);
}
bool cmpyID(int a, int b) {
  return pt[a].y < pt[b].y || (pt[a].y == pt[b].y && a > b);
}

#define lowbit(p) (p & -p)
void update(int p, int x) {
  for (; p <= m; p += lowbit(p))
    c[p] = max(c[p], x);
}
int query(int p) {
  int ret = 0;
  for (; p; p -= lowbit(p))
    ret = max(ret, c[p]);
  return ret;
}
void cls(int p) {
  for (; p <= m; p += lowbit(p))
    c[p] = 0;
}

int id[maxn];
void rec(int l, int r) {
  if (r - l == 1)
    return;

  int m = (l + r) >> 1;
  rec(l, m);

  for (int i = l; i < r; i++)
    id[i] = i;
  sort(id + l, id + r, cmpyID);

  for (int i = l; i < r; i++) {
    if (id[i] < m)
      update(pt[id[i]].z, pt[id[i]].f);
    else
      pt[id[i]].f = max(pt[id[i]].f, query(pt[id[i]].z - 1) + 1);
  }
  for (int i = l; i < m; i++)
    cls(pt[i].z);

  rec(m, r);
}
int a, b, C = ~(1 << 31);
int M = (1 << 16) - 1;
int r() {
  a = 36969 * (a & M) + (a >> 16);
  b = 18000 * (b & M) + (b >> 16);
  return (C & ((a << 16) + b)) % 1000000;
}

void init() {
  n += m;
  for (int i = 0; i < m; i++)
    scanf("%d %d %d", &pt[i].x, &pt[i].y, &pt[i].z);
  for (int i = m; i < n; i++)
    pt[i].x = r(), pt[i].y = r(), pt[i].z = r();
}

void solve() {
  for (int i = 0; i < n; i++) {
    pt[i].f = 1;
    z[i] = pt[i].z;
  }

  sort(pt, pt + n, cmpxY);
  sort(z, z + n);
  m = unique(z, z + n) - z;
  for (int i = 0; i < n; i++)
    pt[i].z = lower_bound(z, z + m, pt[i].z) - z + 1;
  for (int i = 1; i <= m; i++)
    c[i] = 0;

  rec(0, n);

  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, pt[i].f);
  printf("%d\\n", ans);
}

int main() {
  while (~scanf("%d %d %d %d", &m, &n, &a, &b) && m + n + a + b) {
    init();
    solve();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>

using namespace std;

#define CS const static
#define LL long long
#define Rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define PII pair<int, int>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second

CS int NM = 300010, L = 1000001;

struct ptr {
  int x, y, z, f;
} p[NM];

int c[L + 10];

int n, m, A, B;

int a, b, C = ~(1 << 31), M = (1 << 16) - 1;
int r() {
  a = 36969 * (a & M) + (a >> 16);
  b = 18000 * (b & M) + (b >> 16);
  return (C & ((a << 16) + b)) % 1000000;
}

bool cmp1(const ptr &a, const ptr &b) {
  if (a.x != b.x)
    return a.x < b.x;
  return b.y < a.y;
}

bool cmp2(const ptr &a, const ptr &b) { return a.y < b.y; }

void upd(int x, int d) {
  for (; x <= L; x += (x & (-x)))
    c[x] = max(c[x], d);
}

void bak(int x) {
  for (; x <= L; x += (x & (-x)))
    c[x] = 0;
}

int query(int x) {
  int ans = 0;
  for (; x > 0; x -= (x & (-x)))
    ans = max(ans, c[x]);
  return ans;
}

void dfs(int l, int r) {
  if (l == r) {
    p[l].f = max(p[l].f, 1);
    return;
  }
  int mid = (l + r) >> 1;
  dfs(l, mid);
  sort(p + l, p + mid + 1, cmp2);
  sort(p + mid + 1, p + r + 1, cmp2);
  // memset(c,0,sizeof(c)) ;
  int j = 1;
  Rep(i, mid + 1, r) {
    while (p[j].y < p[i].y && j <= mid) {
      upd(p[j].z, p[j].f);
      ++j;
    }
    p[i].f = max(p[i].f, query(p[i].z - 1) + 1);
  }
  Rep(k, 1, j) bak(p[k].z);
  dfs(mid + 1, r);
}

int main() {
  //	freopen("data.txt","r",stdin) ;
  while (cin >> m >> n >> A >> B, n + m) {
    a = A;
    b = B;
    Rep(i, 1, m) {
      scanf("%d %d %d", &p[i].x, &p[i].y, &p[i].z);
      ++p[i].z;
      p[i].f = 0;
    }
    Rep(i, 1, n) {
      ++m;
      p[m].x = r();
      p[m].y = r();
      p[m].z = r();
      ++p[m].z;
      p[i].f = 0;
    }
    sort(p + 1, p + m + 1, cmp1);
    dfs(1, m);
    int ans = p[1].f;
    Rep(i, 2, m) ans = max(ans, p[i].f);
    cout << ans << endl;
  }
  return 0;
}