Problem p03819 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <cassert>
#include <ccomplex>
#include <clocale>
#include <cmath>
#include <complex>
#include <ctype.h>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <valarray>
#include <vector>
#include <wchar.h>

using namespace std;
typedef long long int LL;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(), (c).end())
#define BACKSORT(c) sort((c).begin(), (c).end(), std::greater<LL>())
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int xcomp(vector<LL> &a, vector<LL> &b) { return a[0] < b[0]; }

#define XSORT(c) sort((c).begin(), (c).end(), xcomp)

#define ALL(x) (x).begin(), (x).end()
LL mod = 1000000007;

vector<LL> v(262144, 0);

void add(LL l, LL r) {
  if (l > r) {

  } else if (l == r) {
    v[l - 1]++;
  } else {
    LL gomi = 131072;
    LL nazo = 0;
    LL a = l - 1;
    LL b = 2;
    while (true) {
      if ((l - 1) + b - 1 > r - 1) {
        break;
      }
      if ((l - 1) % b != 0) {
        break;
      }
      a = a / 2;
      b *= 2;
      nazo += gomi;
      gomi = gomi / 2;
    }
    v[a + nazo]++;
    l;
    r;
    b;
    if ((l - 1) + b / 2 <= r - 1) {
      add((l - 1) + b / 2 + 1, r);
    }
  }
}

LL up(LL n, LL m) {
  if (n % m == 0) {
    return n / m;
  } else {
    return n / m + 1;
  }
}
LL down(LL n, LL m) {
  if (n % m == 0) {
    return n / m;
  } else {
    return n / m;
  }
}

int main() {
  LL N, M;
  cin >> N >> M;

  REP(i, N) {
    LL l, r;
    cin >> l >> r;
    if (r - l + 1 <= 15) {
      vector<bool> flag(100001, false);
      REP(i, r - l + 1) {
        LL kuso = 1;
        while (kuso * kuso <= l + i) {
          if ((l + i) % kuso == 0) {
            if (!flag[kuso - 1]) {
              flag[kuso - 1] = true;
              v[kuso - 1]++;
            }
            if (!flag[(l + i) / kuso - 1]) {
              flag[(l + i) / kuso - 1] = true;
              v[(l + i) / kuso - 1]++;
            }
          }
          kuso++;
        }
      }
    } else {

      LL nn = 1;
      LL min = r + 1;
      bool flag = true;
      while (flag) {
        LL ll, rr;
        if (down(r, nn) >= min) {
          rr = min - 1;
        } else {

          rr = down(r, nn);
        }
        if (up(l, nn) <= r - l + 1) {
          ll = 1;
          flag = false;
        } else {
          ll = up(l, nn);
          if (l == 1) {
            flag = false;
          }
        }
        min = ll;
        add(ll, rr);
        nn++;
        if (2 <= ll && ll <= 100 && ll == rr) {
          flag = false;
          REP(wanwan, ll - 1) {
            LL jj = wanwan + 1;
            if (r - l + 1 >= jj) {
              v[jj - 1]++;
            } else {
              if (r / jj != l / jj) {
                v[jj - 1]++;
              } else {
                if (r % jj == 0 || l % jj == 0) {
                  v[jj - 1]++;
                }
              }
            }
          }
        } else

            if (2 <= ll && ll <= 1000 && nn >= 100) {
          flag = false;
          REP(wanwan, ll - 1) {
            LL jj = wanwan + 1;
            if (r - l + 1 >= jj) {
              v[jj - 1]++;
            } else {
              if (r / jj != l / jj) {
                v[jj - 1]++;
              } else {
                if (r % jj == 0 || l % jj == 0) {
                  v[jj - 1]++;
                }
              }
            }
          }
        }
      }
    }
  }

  LL hoge = 262143;
  LL uwaa = 1;
  REP(i, 17) {
    hoge -= uwaa;
    REP(j, uwaa) {
      v[hoge - 2 * uwaa + 2 * j] += v[hoge + j];
      v[hoge - 2 * uwaa + 2 * j + 1] += v[hoge + j];
      v[hoge + j] = 0;
    }
    uwaa *= 2;
  }

  REP(i, M) { cout << v[i] << endl; }
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cassert>
#include <ccomplex>
#include <clocale>
#include <cmath>
#include <complex>
#include <ctype.h>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <valarray>
#include <vector>
#include <wchar.h>

using namespace std;
typedef long long int LL;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(), (c).end())
#define BACKSORT(c) sort((c).begin(), (c).end(), std::greater<LL>())
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int xcomp(vector<LL> &a, vector<LL> &b) { return a[0] < b[0]; }

#define XSORT(c) sort((c).begin(), (c).end(), xcomp)

#define ALL(x) (x).begin(), (x).end()
LL mod = 1000000007;

vector<LL> v(262144, 0);

void add(LL l, LL r) {
  if (l > r) {

  } else if (l == r) {
    v[l - 1]++;
  } else {
    LL gomi = 131072;
    LL nazo = 0;
    LL a = l - 1;
    LL b = 2;
    while (true) {
      if ((l - 1) + b - 1 > r - 1) {
        break;
      }
      if ((l - 1) % b != 0) {
        break;
      }
      a = a / 2;
      b *= 2;
      nazo += gomi;
      gomi = gomi / 2;
    }
    v[a + nazo]++;
    l;
    r;
    b;
    if ((l - 1) + b / 2 <= r - 1) {
      add((l - 1) + b / 2 + 1, r);
    }
  }
}

LL up(LL n, LL m) {
  if (n % m == 0) {
    return n / m;
  } else {
    return n / m + 1;
  }
}
LL down(LL n, LL m) {
  if (n % m == 0) {
    return n / m;
  } else {
    return n / m;
  }
}

int main() {
  LL N, M;
  cin >> N >> M;

  REP(i, N) {
    LL l, r;
    cin >> l >> r;
    if (false) { // r - l + 1 <= 15
      vector<bool> flag(100001, false);
      REP(i, r - l + 1) {
        LL kuso = 1;
        while (kuso * kuso <= l + i) {
          if ((l + i) % kuso == 0) {
            if (!flag[kuso - 1]) {
              flag[kuso - 1] = true;
              v[kuso - 1]++;
            }
            if (!flag[(l + i) / kuso - 1]) {
              flag[(l + i) / kuso - 1] = true;
              v[(l + i) / kuso - 1]++;
            }
          }
          kuso++;
        }
      }
    } else {

      LL nn = 1;
      LL min = r + 1;
      bool flag = true;
      while (flag) {
        LL ll, rr;
        if (down(r, nn) >= min) {
          rr = min - 1;
        } else {

          rr = down(r, nn);
        }
        if (up(l, nn) <= r - l + 1) {
          ll = 1;
          flag = false;
        } else {
          ll = up(l, nn);
          if (l == 1) {
            flag = false;
          }
        }
        min = ll;
        add(ll, rr);
        nn++;
        if (2 <= ll && ll <= 100 && ll == rr) {
          flag = false;
          REP(wanwan, ll - 1) {
            LL jj = wanwan + 1;
            if (r - l + 1 >= jj) {
              v[jj - 1]++;
            } else {
              if (r / jj != l / jj) {
                v[jj - 1]++;
              } else {
                if (r % jj == 0 || l % jj == 0) {
                  v[jj - 1]++;
                }
              }
            }
          }
        } else

            if (2 <= ll && ll <= 1000 && nn >= 100) {
          flag = false;
          REP(wanwan, ll - 1) {
            LL jj = wanwan + 1;
            if (r - l + 1 >= jj) {
              v[jj - 1]++;
            } else {
              if (r / jj != l / jj) {
                v[jj - 1]++;
              } else {
                if (r % jj == 0 || l % jj == 0) {
                  v[jj - 1]++;
                }
              }
            }
          }
        }
      }
    }
  }

  LL hoge = 262143;
  LL uwaa = 1;
  REP(i, 17) {
    hoge -= uwaa;
    REP(j, uwaa) {
      v[hoge - 2 * uwaa + 2 * j] += v[hoge + j];
      v[hoge - 2 * uwaa + 2 * j + 1] += v[hoge + j];
      v[hoge + j] = 0;
    }
    uwaa *= 2;
  }

  REP(i, M) { cout << v[i] << endl; }
}