Submission #1309310

#TimeUsernameProblemLanguageResultExecution timeMemory
1309310aaaaaaaaIndex (COCI21_index)C++17
60 / 110
2597 ms72416 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
const int mxN = 2e5 + 10;
int a[mxN], cseen[mxN], st[mxN], en[mxN], p[mxN];
map<int, set<int>> mp_l, mp_r;
vector<pair<int, int>> query;

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);
    int n, q;
    cin >> n >> q;

    for(int i = 1; i <= n; ++i){
        cin >> a[i];
    }

    for(int i = 1, l, r; i <= q; ++i){
        cin >> l >> r;
        mp_l[l].insert(i);
        mp_r[r].insert(i);
        query.push_back({l, r});
        st[i] = 1, en[i] = n + 1;
        p[i] = (st[i] + en[i]) / 2;
    }
    
    vector<int> ans(q + 1, 0);

    for(int _ = 0; _ < 17; ++_){
        pbds oset;
        for(int i = 1; i <= n; ++i){
            for(auto it : mp_l[i]){
                if(st[it] > en[it]) {
                    cseen[it] = 0;
                    continue;
                }
                cseen[it] = -oset.order_of_key(p[it] - 1);
            }
            oset.insert(a[i]);
            for(auto it : mp_r[i]){
                if(st[it] > en[it]) {
                    cseen[it] = 0;
                    continue;
                }
                cseen[it] += oset.order_of_key(p[it] - 1);
                if(cseen[it] >= p[it]){
                    st[it] = p[it] + 1;
                    ans[it] = p[it];
                }else{
                    en[it] = p[it] - 1;
                }
                cseen[it] = 0;
            }
        }   
        for(int i = 1; i <= q; ++i){
            if(st[i] <= en[i]){
                p[i] = (st[i] + en[i]) / 2;
            }else{
                mp_l[query[i - 1].first].erase(i);
                mp_r[query[i - 1].second].erase(i);
            }
        }
    }

    for(int i = 1; i <= q; ++i) cout << ans[i] << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...