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

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))

using namespace std;

typedef pair<int, int> pii;

int BIT[100010];

void Add(int i, int x) {
  while (i) {
    BIT[i] += x;
    i -= (i & -i);
  }
}

int Get(int i) {
  int ans = 0;
  while (i < 100010) {
    ans += BIT[i];
    i += (i & -i);
  }
  return ans;
}

int main() {
  int n, q;
  scanf("%d %d", &n, &q);

  for (int i = 0; i < q; i++) {
    int c, s, t, v;
    scanf("%d", &c);

    switch (c) {
    case 0:
      scanf("%d %d %d", &s, &t, &v);
      Add(t, v);
      Add(s - 1, -v);
      break;
    case 1:
      scanf("%d", &s);
      cout << Get(s) << endl;
      break;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))

using namespace std;

typedef pair<int, int> pii;

class OwnNode {
public:
  int num;
  int l, r;
  int left, right;

  OwnNode(int l, int r) : l(l), r(r), num(0), left(-1), right(-1) {}
};

vector<OwnNode> nodes;

int Build(int l, int r) {
  int id = nodes.size();
  nodes.push_back(OwnNode(l, r));
  int m = (l + r) / 2;
  if (l + 1 != r) {
    nodes[id].left = Build(l, m);
    nodes[id].right = Build(m, r);
  }
  return id;
}

void Update(int id, int s, int t, int v) {
  if (t <= nodes[id].l || nodes[id].r <= s) {
    return;
  }

  nodes[id].num += v * (min(t, nodes[id].r) - max(s, nodes[id].l));

  int m = (nodes[id].l + nodes[id].r) / 2;
  if (nodes[id].right != -1) {
    if (m < t) {
      Update(nodes[id].right, s, t, v);
    }
  }
  if (nodes[id].left != -1) {
    if (s < m) {
      Update(nodes[id].left, s, t, v);
    }
  }
}

int Find(int id, int s, int t) {
  if (t <= nodes[id].l || nodes[id].r <= s) {
    return 0;
  }
  if (s <= nodes[id].l && nodes[id].r <= t) {
    return nodes[id].num;
  }
  int num = 0;
  int m = (nodes[id].l + nodes[id].r) / 2;
  if (nodes[id].right != -1) {
    if (m < t) {
      num += Find(nodes[id].right, s, t);
    }
  }
  if (nodes[id].left != -1) {
    if (s < m) {
      num += Find(nodes[id].left, s, t);
    }
  }
  return num;
}

int main() {
  nodes.reserve(200000);
  int n, q;
  vector<int> ans;
  scanf("%d %d", &n, &q);

  Build(0, n);

  for (int i = 0; i < q; i++) {
    int c, s, t, v;
    scanf("%d", &c);

    switch (c) {
    case 0:
      scanf("%d %d %d", &s, &t, &v);
      Update(0, s - 1, t, v);
      break;
    case 1:
      scanf("%d", &s);
      ans.push_back(Find(0, s - 1, s));
      break;
    }
  }

  for (auto &n : ans) {
    cout << n << endl;
  }

  return 0;
}