/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 316.0 KiB
#2 Accepted 1ms 536.0 KiB
#3 Accepted 1ms 536.0 KiB
#4 Accepted 100ms 3.457 MiB
#5 Accepted 391ms 20.246 MiB
#6 Accepted 171ms 11.27 MiB
#7 Accepted 40ms 836.0 KiB
#8 Accepted 109ms 10.703 MiB
#9 Accepted 160ms 10.52 MiB
#10 Accepted 153ms 11.523 MiB
#11 Accepted 15ms 580.0 KiB
#12 Accepted 29ms 704.0 KiB
#13 Accepted 1ms 532.0 KiB

Code

#include<bits/stdc++.h>
#define endl        '\n'
#define F           first
#define S           second
#define pb          push_back
#define yes         cout<<"YES\n"
#define no          cout<<"NO\n"
#define all(x)      x.begin(),x.end()
#define allr(x)     x.rbegin(),x.rend()
#define error1(x)   cerr<<#x<<" = "<<(x)<<endl
#define error2(a,b) cerr<<"("<<#a<<", "<<#b<<") = ("<<(a)<<", "<<(b)<<")\n";
#define coutall(v)  for(auto &it: v) cout << it << " "; cout << endl;
using namespace std;
using ll = long long;
using ld = long double;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T, typename R> using ordered_map = tree<T, R, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// *s.find_by_order(k): K-th element in a set (counting from zero).
// s.order_of_key(k): Number of items strictly smaller than k. (same as lower_bound of k)
// less_equal<T> => for ordered_multiset or, ordered_multimap

void solve() {
    ll n, ans = 0;
    cin >> n;
    vector<ll> v(n), mn1(n), mx1(n);
    ordered_set<ll> st;
    for (int i = 0; i < n; i++) {
        ll x; cin >> x;
        v[i] = x;
        mn1[i] = st.order_of_key(x);
        mx1[i] = ll(st.size()) - st.order_of_key(x + 1);
        st.insert(x);
    }
    // coutall(mn1);
    // coutall(mx1);
    vector<ll> mn2(n), mx2(n);
    st.clear();
    for (int i = n - 1; i >= 0; i--) {
        ll x = v[i];
        mn2[i] = st.order_of_key(x);
        mx2[i] = ll(st.size()) - st.order_of_key(x + 1);
        st.insert(x);
    }
    // coutall(mn2);
    // coutall(mx2);
    ll q;
    cin >> q;
    while(q--) {
        ll i; cin >> i;
        --i;
        cout << (mn1[i] * mx2[i]) + (mn2[i] * mx1[i]) << endl;
    }
    return;
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1079 Roy and Query (Easy Version)
Contest
Brain Booster #6
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 17:10:48
Judged At
2024-10-03 17:10:48
Judged By
Score
100
Total Time
391ms
Peak Memory
20.246 MiB