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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

#define rep(i, n) for (ll(i) = 0; (i) < (n); ++i)

#define rep1(i, n) for (ll(i) = 1; (i) <= (n); ++i)

#define rrep(i, n) for (ll(i) = (n)-1; (i) >= (0); --i)

#define rrep1(i, n) for (ll(i) = (n); (i) >= (1); --i)

#define pb push_back

#define fr first

#define sc second

ll N, Q;
ll X[200000], L[200000], D[200000];
ll bit[2][200001];

void add(ll b, ll i, ll x) {
  i++;
  while (i <= N) {
    bit[b][i] += x;
    i += i & -i;
  }
}

ll sum(ll b, ll i) {
  ll res = 0;
  i++;
  i = min(i, N);
  while (i > 0) {
    res += bit[b][i];
    i -= i & -i;
  }
  return res;
}

int main()

{

  cin >> N >> Q;
  rrep(i, Q) {
    cin >> X[i] >> D[i] >> L[i];
    if (D[i] == 1)
      X[i]--;
  }
  rep1(i, N - 1) {
    add(0, i, 1);
    add(1, i, 1);
  }
  ll lb, ub, mid;
  rep(i, Q) {
    if (D[i] == 1) {
      lb = -1;
      ub = N;
      while (ub - lb > 1) {
        mid = (ub + lb) / 2;
        if (sum(0, mid) > X[i]) {
          ub = mid;
        } else
          lb = mid;
      }
      add(1, 0, 2 * (-L[i]));
      add(1, ub, 2 * (L[i]));
    } else {
      lb = -1;
      ub = N;
      while (ub - lb > 1) {
        mid = (ub + lb) / 2;
        if (sum(1, mid) >= X[i]) {
          ub = mid;
        } else
          lb = mid;
      }
      add(0, ub, 2 * (L[i]));
    }
  }
  rep(i, N) { cout << (sum(0, i) - sum(1, i)) / 2 << endl; }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <chrono> //1e+9??§?????? auto end= chrono::system_clock::now()-st;  cout<<end.count()%1e+9<<endl;
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)

P ans[200000];
P imos[200001];
void addfr(ll l, ll r, ll val) {
  imos[l].fr += val;
  imos[r].fr -= val;
}
void addsc(ll l, ll r, ll val) {
  imos[l].sc += val;
  imos[r].sc -= val;
}

struct range {
  ll id;
  ll loc;
  ll sz;
  ll fr, sc;
  bool operator<(const range &an) const { return id < an.id; }
  bool operator>(const range &an) const { return id > an.id; }
  bool operator==(const range &an) const { return id == an.id; }
  bool operator!=(const range &an) const { return id != an.id; }
};
map<ll, range> Rv;
map<ll, range>::iterator ite;
set<range> Ranges;
set<P> st1; // fr,id
set<P> st2; // sc,id

ll idcnt = 0;
ll N, Q;
ll sz1, sz2;
ll X[200000], D[200000], L[200000]; // X??´??????D?????????L??§??????

const ll INF = 0xfffffffffff;
queue<P> que;

int main() {
  cin >> N >> Q;
  rep(i, N) {
    ans[i].fr = i;
    ans[i].sc = N - 1 - i;
  }
  rrep(i, Q) {
    cin >> X[i] >> D[i] >> L[i];
    if (D[i] == 1) {
      X[i] = N - X[i];
    }
  }
  Rv[0] = range{0, 0, N, 0, N - 1};
  idcnt++;
  st1.insert(P(Rv[0].fr + Rv[0].sz - 1, 0));
  st2.insert(P(Rv[0].sc, 0));
  rep(i, Q) {
    if (D[i] == 2) { // fr?????????X[i]??\\???  //range{id,loc,sz,fr,sc}
      for (auto ite = st1.lower_bound(P(X[i], -INF)); ite != st1.end(); ++ite) {
        que.push(*ite);
      }
      while (!que.empty()) {
        P p = que.front();
        que.pop();
        st1.erase(p);
        range r = Rv[p.sc];
        st2.erase(P(r.sc, r.id));
        Rv.erase(p.sc);
        if (X[i] <= r.fr) {
          addsc(r.loc, r.loc + r.sz, -L[i] * 2);
          Rv[idcnt] = (range{idcnt, r.loc, r.sz, r.fr, r.sc - L[i] * 2});
          ite = Rv.find(idcnt);
          idcnt++;
          st1.insert(P(ite->sc.fr + ite->sc.sz - 1, idcnt - 1));
          st2.insert(P(ite->sc.sc, idcnt - 1));
        } else {
          sz1 = X[i] - r.fr;
          sz2 = r.sz - sz1;

          addsc(r.loc + sz1, r.loc + r.sz, -L[i] * 2);

          Rv[idcnt] = range{idcnt, r.loc, sz1, r.fr, r.sc};
          ite = Rv.find(idcnt);
          idcnt++;
          st1.insert(P(ite->sc.fr + ite->sc.sz - 1, idcnt - 1));
          st2.insert(P(ite->sc.sc, idcnt - 1));

          Rv[idcnt] = (range{idcnt, r.loc + sz1, sz2, r.fr + sz1,
                             r.sc - sz1 - L[i] * 2});
          ite = Rv.find(idcnt);
          idcnt++;
          st1.insert(P(ite->sc.fr + ite->sc.sz - 1, idcnt - 1));
          st2.insert(P(ite->sc.sc, idcnt - 1));
        }
      }
    } else { // sc ??????X[i]??\\???
      for (auto ite = st2.lower_bound(P(X[i], -INF)); ite != st2.end(); ++ite) {
        que.push(*ite);
      }
      while (!que.empty()) {
        P p = que.front();
        que.pop();
        st2.erase(p);
        range r = Rv[p.sc];
        st1.erase(P(r.fr + r.sz - 1, r.id));
        Rv.erase(p.sc);
        if (X[i] <= r.sc - r.sz + 1) { //
          addfr(r.loc, r.loc + r.sz, -L[i] * 2);
          Rv[idcnt] = (range{idcnt, r.loc, r.sz, r.fr - L[i] * 2, r.sc});
          ite = Rv.find(idcnt);
          idcnt++;
          st1.insert(P(ite->sc.fr + ite->sc.sz - 1, idcnt - 1));
          st2.insert(P(ite->sc.sc, idcnt - 1));
        } else {
          sz1 = r.sc - X[i] + 1;
          sz2 = r.sz - sz1;
          addfr(r.loc, r.loc + sz1, -L[i] * 2);

          Rv[idcnt] = (range{idcnt, r.loc, sz1, r.fr - L[i] * 2, r.sc});
          ite = Rv.find(idcnt);
          idcnt++;

          st1.insert(P(ite->sc.fr + ite->sc.sz - 1, idcnt - 1));
          st2.insert(P(ite->sc.sc, idcnt - 1));

          Rv[idcnt] = (range{idcnt, r.loc + sz1, sz2, r.fr + sz1, r.sc - sz1});
          ite = Rv.find(idcnt);
          idcnt++;
          st1.insert(P(ite->sc.fr + ite->sc.sz - 1, idcnt - 1));
          st2.insert(P(ite->sc.sc, idcnt - 1));
        }
      }
    }
  }
  rep(i, N - 1) {
    imos[i + 1].fr += imos[i].fr;
    imos[i + 1].sc += imos[i].sc;
  }
  rep(i, N) {
    cout << (N - 1 - (ans[i].fr + imos[i].fr + ans[i].sc + imos[i].sc)) / 2
         << endl;
  }
}