답안 #955161

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
955161 2024-03-29T14:19:06 Z shezitt Index (COCI21_index) C++14
0 / 110
4 ms 2912 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <cassert>

using namespace std;

using ll = long long;
#define int ll
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

#define fore(i, a, b) for(int i=a; i<b; ++i)

const int N = 2e5+5;

int p[N], n, q;

int mp[N];

struct Node {
    Node *left, *right;
    int val;

    Node() {
        left = nullptr;
        right = nullptr;
        val = 0;
    }

};

int arr[N];

Node* version[N];

void init(Node *cur, int tl, int tr){
    if(tl == tr){
        cur->val = arr[tl];
        return;
    }

    int tm = (tl + tr) / 2;

    cur->left = new Node();
    cur->right = new Node();

    init(cur->left, tl, tm);
    init(cur->right, tm+1, tr);

    cur->val = cur->left->val + cur->right->val;

}

void update(Node *cur, Node *prev, int tl, int tr, int idx, int val){
    if(tl == tr){
        arr[tl] = val;
        cur->val = val;
        return;
    }

    int tm = (tl + tr) / 2;

    if(idx <= tm){
        cur->left = new Node();
        cur->right = prev->right;
        update(cur->left, prev->left, tl, tm, idx, val);
    } else {
        cur->left = prev->left;
        cur->right = new Node();
        update(cur->right, prev->right, tm+1, tr, idx, val);
    }

    cur->val = cur->left->val + cur->right->val;

}

void update(Node *cur, Node *prev, int idx, int val){
    update(cur, prev, 0, n-1, idx, val);
}

int query(Node *cur, int tl, int tr, int l, int r){
    if(l <= tl && tr <= r){
        return cur->val;
    }

    if(r < tl or tr < l){
        return 0;
    }

    int tm = (tl + tr) / 2;

    int p1 = query(cur->left, tl, tm, l, r);
    int p2 = query(cur->right, tm+1, tr, l, r);

    return p1 + p2;

}

int query(Node *cur, int l, int r){
    return query(cur, 0, n-1, l, r);
}

signed main(){
    cin >> n >> q;

    fore(i, 0, n){
        cin >> p[i];
    }

    vector<int> sorted;
    sorted.insert(sorted.begin(), p, p+n);

    sort(all(sorted));
    sorted.erase(unique(all(sorted)), sorted.end());

    fore(i, 0, sz(sorted)){
        mp[sorted[i]] = i;
    }

    version[0] = new Node();
    init(version[0], 0, n-1);

    fore(i, 0, n){
        version[i+1] = new Node();
        int key = mp[p[i]];
        int prevVal = query(version[i], key, key);
        update(version[i+1], version[i], key, prevVal + 1);
    }

    fore(i, 0, q){
        int l, r;
        cin >> l >> r;

        // maximo h tal que histo(h, n) >= h

        int low = 0, high = n-1;
        int res = -1;
        while(low <= high){
            int mid = low + (high - low) / 2;

            int cnt = r - l + 1;

            // restar menores a mid
            if(mid > 0){
                cnt -= query(version[r], 0, mid-1) - query(version[l-1], 0, mid-1);
            }

            if(cnt >= sorted[mid]){
                res = mid+1;
                low = mid+1;
            } else {
                high = mid-1;
            }

        }

        assert(res > -1);
        cout << res << endl;

    }

}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2912 KB Output isn't correct
2 Halted 0 ms 0 KB -