/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 336.0 KiB
#2 Accepted 2ms 332.0 KiB
#3 Accepted 2ms 488.0 KiB
#4 Accepted 153ms 3.453 MiB
#5 Accepted 662ms 20.426 MiB
#6 Accepted 286ms 11.199 MiB
#7 Accepted 64ms 864.0 KiB
#8 Accepted 185ms 10.922 MiB
#9 Accepted 194ms 10.617 MiB
#10 Accepted 207ms 11.535 MiB
#11 Accepted 25ms 684.0 KiB
#12 Accepted 46ms 776.0 KiB
#13 Accepted 2ms 556.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-12-17 11:33:14
Judged By
Score
100
Total Time
662ms
Peak Memory
20.426 MiB