Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int maxn = 1e5 + 10;
- vector<int> segment_tree[3 * maxn];
- int arr[maxn];
- void build(int L, int R, int position) {
- if(L == R) {
- segment_tree[position].push_back(arr[L]);
- }
- else {
- int mid = (L + R) / 2;
- build(L, mid, 2 * position);
- build(mid + 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) {
- int idx = lower_bound(segment_tree[position].begin(), segment_tree[position].end(), x) - segment_tree[position].begin();
- if(idx >= 0 and idx < segment_tree[position].size()) {
- if(segment_tree[position][idx] == x) {
- return 1;
- }
- }
- return 0;
- }
- if(R < i or j < L) {
- return 0;
- }
- int mid = (L + R) / 2;
- return max(query(L, mid, 2 * position, i, j, x), query(mid + 1, R, 2 * position + 1, i, j, x));
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> arr[i];
- }
- build(0, n, 1);
- while(true) {
- int i, j, x;
- cin >> i >> j >> x;
- cout << query(0, n, 1, i - 1, j - 1, x) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement