Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 33333;
- int arr[maxn];
- int next_idx[maxn];
- vector<int> segment_tree[3 * maxn];
- void build(int L, int R, int position) {
- if(L == R) {
- segment_tree[position].push_back(next_idx[L]);
- }
- else {
- int middle = (L + R) / 2;
- build(L, middle, 2 * position);
- build(middle + 1, R, 2 * position + 1);
- merge(segment_tree[2 * position].begin(), segment_tree[2 * position].end(), segment_tree[2 * position + 1].begin(), segment_tree[2 * position + 1].end(), back_inserter(segment_tree[position]));
- }
- }
- int query(int L, int R, int position, int i, int j, int x) {
- // L R i L R j L R
- if(i <= L and R <= j) {
- return lower_bound(segment_tree[position].begin(), segment_tree[position].end(), x) - segment_tree[position].begin();
- }
- if(R < i or j < L) {
- return 0;
- }
- int middle = (L + R) / 2;
- return query(L, middle, 2 * position, i, j, x) + query(middle + 1, R, 2 * position + 1, i, j, x);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- vector<pair<int, int> > v;
- for(int i = 0; i < n; i++) {
- cin >> arr[i];
- v.push_back(make_pair(arr[i], i));
- }
- sort(v.begin(), v.end());
- for(int i = 0; i < n; i++){
- if(i + 1 < n) {
- if(v[i].first == v[i + 1].first) {
- next_idx[v[i].second] = v[i + 1].second;
- }
- else {
- next_idx[v[i].second] = n;
- }
- }
- else {
- next_idx[v[i].second] = n;
- }
- }
- int q;
- cin >> q;
- build(0, n, 1);
- for(int i = 0; i < q; i++) {
- int a, b;
- cin >> a >> b;
- a--;
- b--;
- cout << (b - a + 1) - query(0, n, 1, a, b, b + 1) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement