Psychotic_D_BKL's blog

By Psychotic_D_BKL, history, 8 hours ago, In English

https://www.spoj.com/problems/KQUERY/

this was the question and my code is

include <bits/stdc++.h>

using namespace std;

define int long long

int n; vector<vector> segmentTree; vector a;

void build(int l, int r, int index) { if (l == r) { segmentTree[index].push_back(a[l]); return; } int mid = l + (r — l) / 2; build(l, mid, 2 * index + 1); build(mid + 1, r, 2 * index + 2); merge(segmentTree[2 * index + 1].begin(), segmentTree[2 * index + 1].end(), segmentTree[2 * index + 2].begin(), segmentTree[2 * index + 2].end(), back_inserter(segmentTree[index])); }

int query(int l, int r, int start, int end, int index, int k) { if (l > end || r < start) { return 0; } if (l >= start && r <= end) { return segmentTree[index].end() — upper_bound(segmentTree[index].begin(), segmentTree[index].end(), k); } int mid = l + (r — l) / 2; return query(l, mid, start, end, 2 * index + 1, k) + query(mid + 1, r, start, end, 2 * index + 2, k); }

int32_t main() { cin >> n; segmentTree.resize(4 * n); a.resize(n); for (int i = 0; i < n; i++) { cin >> a[i]; } build(0, n — 1, 0);

int q;
cin >> q;
while (q--) {
    int l, r, k;
    cin >> l >> r >> k;
    cout << query(0, n - 1, l - 1, r - 1, 0, k) << endl;
}

}

can anyone tell me why am i getting tle..

  • Vote: I like it
  • -7
  • Vote: I do not like it