|
| 1 | +#include <bits/stdc++.h> |
| 2 | + |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +#define all(v) (v).begin(), (v).end() |
| 6 | +#define LEFT(n) (n << 1) |
| 7 | +#define RIGHT(n) ( (n << 1)|1 ) |
| 8 | + |
| 9 | +const int INSERTION = 0, QUERY = 1, MAX_N = 1e6 + 5, OLD_LAST_OCCURENCE = 0, NEW_LAST_OCCURENCE = 1; |
| 10 | +int tree[3*MAX_N]; |
| 11 | + |
| 12 | +struct info |
| 13 | +{ |
| 14 | + int end; |
| 15 | + int type; |
| 16 | + int position, value; |
| 17 | + int left, right, query_no; |
| 18 | + |
| 19 | + info(){ end = type = left = right = query_no = position = -1; } |
| 20 | +}; |
| 21 | + |
| 22 | +int compare_ends(const info &A, const info &B) |
| 23 | +{ |
| 24 | + if(A.end < B.end) |
| 25 | + return true; |
| 26 | + else if(A.end == B.end) |
| 27 | + return (A.type == INSERTION); |
| 28 | + |
| 29 | + return false; |
| 30 | +} |
| 31 | + |
| 32 | +void insert(int n, int left, int right, int position, int position_type) |
| 33 | +{ |
| 34 | + if(right < position || position < left) |
| 35 | + return; |
| 36 | + |
| 37 | + if(left == right) |
| 38 | + { |
| 39 | + if(position_type == OLD_LAST_OCCURENCE) |
| 40 | + tree[n] = 0; |
| 41 | + else if(position_type == NEW_LAST_OCCURENCE) |
| 42 | + tree[n] = 1; |
| 43 | + |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + int mid = (left + right) >> 1; |
| 48 | + |
| 49 | + if(position <= mid) |
| 50 | + insert(LEFT(n), left, mid, position, position_type); |
| 51 | + else if(position > mid) |
| 52 | + insert(RIGHT(n), mid + 1, right, position, position_type); |
| 53 | + |
| 54 | + tree[n] = tree[LEFT(n)] + tree[RIGHT(n)]; |
| 55 | +} |
| 56 | + |
| 57 | +int query(int n, int left, int right, int query_left, int query_right) |
| 58 | +{ |
| 59 | + if(query_right < left || right < query_left) |
| 60 | + return 0; |
| 61 | + |
| 62 | + if(query_left <= left && right <= query_right) |
| 63 | + return tree[n]; |
| 64 | + |
| 65 | + int mid = (left + right) >> 1; |
| 66 | + |
| 67 | + int left_answer = query(LEFT(n), left, mid, query_left, query_right); |
| 68 | + int right_answer = query(RIGHT(n), mid + 1, right, query_left, query_right); |
| 69 | + |
| 70 | + return (left_answer + right_answer); |
| 71 | +} |
| 72 | + |
| 73 | +int main() |
| 74 | +{ |
| 75 | + memset(tree, 0, sizeof(tree)); |
| 76 | + |
| 77 | + vector <info> event; |
| 78 | + |
| 79 | + int no_of_elements; |
| 80 | + scanf("%d", &no_of_elements); |
| 81 | + for(int i = 1; i <= no_of_elements; i++) |
| 82 | + { |
| 83 | + info current_event; |
| 84 | + current_event.type = INSERTION; |
| 85 | + current_event.end = current_event.position = i; |
| 86 | + scanf("%d", ¤t_event.value); |
| 87 | + |
| 88 | + event.push_back(current_event); |
| 89 | + } |
| 90 | + |
| 91 | + int no_of_queries; |
| 92 | + scanf("%d", &no_of_queries); |
| 93 | + for(int i = 1; i <= no_of_queries; i++) |
| 94 | + { |
| 95 | + info current_event; |
| 96 | + current_event.type = QUERY; |
| 97 | + |
| 98 | + scanf("%d %d", ¤t_event.left, ¤t_event.right); |
| 99 | + current_event.end = current_event.right; |
| 100 | + current_event.query_no = i; |
| 101 | + |
| 102 | + event.push_back(current_event); |
| 103 | + } |
| 104 | + |
| 105 | + sort(all(event), compare_ends); |
| 106 | + |
| 107 | + int no_of_events = no_of_elements + no_of_queries; |
| 108 | + |
| 109 | + map <int, int> last_occurence; |
| 110 | + vector <int> answer(no_of_queries + 1); |
| 111 | + |
| 112 | + for(int i = 0; i < no_of_events; i++) |
| 113 | + { |
| 114 | + if(event[i].type == INSERTION) |
| 115 | + { |
| 116 | + int element = event[i].value; |
| 117 | + |
| 118 | + if(last_occurence.count(element) == 1) |
| 119 | + { |
| 120 | + insert(1, 1, no_of_elements, last_occurence[element], OLD_LAST_OCCURENCE); |
| 121 | + } |
| 122 | + |
| 123 | + insert(1, 1, no_of_elements, event[i].position, NEW_LAST_OCCURENCE); |
| 124 | + last_occurence[element] = event[i].position; |
| 125 | + } |
| 126 | + else if(event[i].type == QUERY) |
| 127 | + { |
| 128 | + answer[event[i].query_no] = query(1, 1, no_of_elements, event[i].left, event[i].right); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + for(int i = 1; i <= no_of_queries; i++) |
| 133 | + printf("%d\n", answer[i]); |
| 134 | + |
| 135 | + return 0; |
| 136 | +} |
0 commit comments