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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

struct Segtree {
  ll a[N_SIZE];
  ll seg[N_SIZE * 2];
  ll lazy[N_SIZE * 2];
  ll gt;
  void init(int n) {
    gt = 1;
    while (n > gt)
      gt *= 2;
    REP(i, n) a[i] = 0;
    REP(i, N_SIZE) {
      seg[i] = 0;
      lazy[i] = 0;
    }
    REP(i, n) seg[i + gt - 1] = a[i];
  }

  void lazy_evaluate(int k, int l, int r) {
    seg[k] += lazy[k] * (r - l);
    if (gt > k) {
      lazy[2 * k + 1] += lazy[k];
      lazy[2 * k + 2] += lazy[k];
    }
    lazy[k] = 0;
  }

  //[a,b)
  void update(int a, int b, int x) { update(a, b, 0, 0, gt, x); }
  void update(int a, int b, int k, int l, int r, int x) {
    if (r <= a || b <= l) {
      return;
    }
    if (a <= l && r <= b) {
      lazy[k] += x;
    } else {
      update(a, b, 2 * k + 1, l, (l + r) / 2, x);
      update(a, b, 2 * k + 2, (l + r) / 2, r, x);
      if (gt > k) {
        seg[k] = seg[2 * k + 1] + lazy[2 * k + 1] * (r - l) / 2 +
                 seg[2 * k + 2] + lazy[2 * k + 2] * (r - l) / 2;
      }
    }
  }

  //[a,b???min
  ll query(int a, int b) { return query(a, b, 0, 0, gt); }
  ll query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b) {
      ll add = (r - l) * lazy[k];
      // lazy_evaluate(k, l, r);
      return seg[k] + add;
    } else {
      lazy_evaluate(k, l, r);
      return query(a, b, 2 * k + 1, l, (l + r) / 2) +
             query(a, b, 2 * k + 2, (l + r) / 2, r);
    }
  }

  void print() {
    REP(i, gt * 2) cout << "!" << i << " " << seg[i] << " " << lazy[i] << endl;
  }
};

int n, q;
Segtree tr;

int main() {
  cin >> n >> q;
  tr.init(n);
  REP(i, q) {
    bool f;
    int a, b, c;
    cin >> f;
    if (!f) {
      cin >> a >> b >> c;
      a--;
      b--;
      tr.update(a, b + 1, c);
    } else {
      cin >> a >> b;
      a--;
      b--;
      cout << tr.query(a, b + 1) << endl;
    }
    // tr.print();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

struct Segtree {
  ll a[N_SIZE];
  ll seg[N_SIZE * 2];
  ll lazy[N_SIZE * 2];
  ll gt;
  void init(int n) {
    gt = 1;
    while (n > gt)
      gt *= 2;
    REP(i, n) a[i] = 0;
    REP(i, N_SIZE) {
      seg[i] = 0;
      lazy[i] = 0;
    }
    REP(i, n) seg[i + gt - 1] = a[i];
  }

  void lazy_evaluate(int k) {
    seg[k] += lazy[k];
    if (gt > k) {
      lazy[2 * k + 1] += lazy[k];
      lazy[2 * k + 2] += lazy[k];
    }
    lazy[k] = 0;
  }

  //[a,b)
  void update(int a, int b, int x) { update(a, b, 0, 0, gt, x); }
  void update(int a, int b, int k, int l, int r, int x) {
    if (r <= a || b <= l) {
      return;
    }
    if (a <= l && r <= b) {
      lazy[k] += x;
    } else {
      lazy_evaluate(k);
      update(a, b, 2 * k + 1, l, (l + r) / 2, x);
      update(a, b, 2 * k + 2, (l + r) / 2, r, x);
      if (gt > k)
        seg[k] = seg[2 * k + 1] + seg[2 * k + 2];
    }
  }

  //[a,b???min
  ll query(int a, int b) { return query(a, b, 0, 0, gt); }
  ll query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    lazy_evaluate(k);
    if (gt > k) {
      return seg[k] = query(a, b, 2 * k + 1, l, (l + r) / 2) +
                      query(a, b, 2 * k + 2, (l + r) / 2, r);
    } else
      return seg[k];
  }

  void print() {
    REP(i, gt * 2) cout << "!" << i << " " << seg[i] << " " << lazy[i] << endl;
  }
};

int n, q;
Segtree tr;

int main() {
  cin >> n >> q;
  tr.init(n);
  REP(i, q) {
    bool f;
    int a, b, c;
    cin >> f;
    if (!f) {
      cin >> a >> b >> c;
      a--;
      b--;
      tr.update(a, b + 1, c);
    } else {
      cin >> a >> b;
      a--;
      b--;
      cout << tr.query(a, b + 1) << endl;
    }
    // tr.print();
  }
}