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

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const int INF = 1000000000;

vector<vector<int>> T;
int l, Size;

void Stworz(int n, vector<int> a) {
  int p = 1;
  while (p < n)
    p *= 2;
  l = p;
  Size = 2 * p - 1;
  vector<int> x;
  for (int i = 0; i <= Size; ++i)
    T.push_back(x);
  for (int i = 0; i < n; ++i)
    T[i + l].push_back(a[i]);
  for (int i = l - 1; i >= 1; --i) {
    for (int j = 0; j < T[2 * i].size(); ++j)
      T[i].push_back(T[2 * i][j]);
    for (int j = 0; j < T[2 * i + 1].size(); ++j)
      T[i].push_back(T[2 * i + 1][j]);
    sort(T[i].begin(), T[i].end());
  }
}

int Ile(vector<int> &a, int x) {
  int l = 0, p = a.size() - 1, s, r = -1;
  while (l <= p) {
    s = (l + p) / 2;
    if (a[s] >= x) {
      r = s;
      p = s - 1;
    } else
      l = s + 1;
  }
  if (r == -1)
    return 0;
  return a.size() - r;
}

int Pyt(int a, int b) {
  int ogr = b + 1;
  a += l;
  b += l;
  int wyn = Ile(T[a], ogr);
  if (a != b)
    wyn += Ile(T[b], ogr);
  while (a / 2 != b / 2) {
    if (a % 2 == 0)
      wyn += Ile(T[a + 1], ogr);
    if (b % 2 == 1)
      wyn += Ile(T[b - 1], ogr);
    a /= 2;
    b /= 2;
  }
  return wyn;
}

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    --a[i];
  }
  vector<int> nast(n, INF);
  vector<int> b(n);
  for (int i = n - 1; i >= 0; --i) {
    b[i] = nast[a[i]];
    nast[a[i]] = i;
  }
  Stworz(n, b);
  for (int i = 0; i < q; ++i) {
    int l, r;
    scanf("%d %d", &l, &r);
    --l;
    --r;
    printf("%d\\n", Pyt(l, r));
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int INF = 1000000000;

vector<vector<int>> T;
int l, Size;

void Stworz(int n, vector<int> a) {
  int p = 1;
  while (p < n)
    p *= 2;
  l = p;
  Size = 2 * p - 1;
  vector<int> x;
  for (int i = 0; i <= Size; ++i)
    T.push_back(x);
  for (int i = 0; i < n; ++i)
    T[i + l].push_back(a[i]);
  for (int i = l - 1; i >= 1; --i) {
    for (int j = 0; j < T[2 * i].size(); ++j)
      T[i].push_back(T[2 * i][j]);
    for (int j = 0; j < T[2 * i + 1].size(); ++j)
      T[i].push_back(T[2 * i + 1][j]);
    sort(T[i].begin(), T[i].end());
  }
}

int Ile(vector<int> &a, int x) {
  int l = 0, p = a.size() - 1, s, r = -1;
  while (l <= p) {
    s = (l + p) / 2;
    if (a[s] >= x) {
      r = s;
      p = s - 1;
    } else
      l = s + 1;
  }
  if (r == -1)
    return 0;
  return a.size() - r;
}

int Pyt(int a, int b) {
  int ogr = b + 1;
  a += l;
  b += l;
  int wyn = Ile(T[a], ogr);
  if (a != b)
    wyn += Ile(T[b], ogr);
  while (a / 2 != b / 2) {
    if (a % 2 == 0)
      wyn += Ile(T[a + 1], ogr);
    if (b % 2 == 1)
      wyn += Ile(T[b - 1], ogr);
    a /= 2;
    b /= 2;
  }
  return wyn;
}

int main() {
  ios_base::sync_with_stdio(0);
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
  }
  vector<int> nast(n, INF);
  vector<int> b(n);
  for (int i = n - 1; i >= 0; --i) {
    b[i] = nast[a[i]];
    nast[a[i]] = i;
  }
  Stworz(n, b);
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    cout << Pyt(l, r) << endl;
  }
  return 0;
}