/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 133ms 3.07 MiB
#5 Accepted 491ms 15.02 MiB
#6 Accepted 212ms 10.434 MiB
#7 Accepted 54ms 788.0 KiB
#8 Accepted 127ms 10.02 MiB
#9 Accepted 152ms 9.816 MiB
#10 Accepted 158ms 12.562 MiB
#11 Accepted 20ms 532.0 KiB
#12 Accepted 38ms 660.0 KiB
#13 Accepted 1ms 532.0 KiB

Code

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace __gnu_pbds; 
using namespace std;
 
typedef pair<long long,long long> PLL;
typedef long long LL;
 
#define all(v) v.begin(),v.end()
#define faster {ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>    

const LL mod = 1e9 + 7;
const int N = 2e5 + 10;
const LL inf = 1e9;

void solve(int test){
    int n, q; cin>>n;
    vector<int> a(n);
    ordered_multiset l, r;
    for(auto &i: a){
        cin>>i;
        r.insert(i);
    }
    cin>>q;
    vector<int> qrs[n];
    for(int i = 0; i < q; i++){
        int u; cin>>u; qrs[--u].push_back(i);
    }
    vector<LL> ans(q);
    for(int i = 0; i < n; i++){
        int x = a[i];
        int pos = r.order_of_key(x);
        r.erase(r.find_by_order(pos));
        int left1 = l.order_of_key(x), left2 = l.size() - l.order_of_key(x + 1);
        int right1 = r.size() - r.order_of_key(x + 1), right2 = r.order_of_key(x);
        LL cur = (1LL * left1 * right1) ;
        cur += (1LL * left2 * right2) ;
        for(auto id: qrs[i]) ans[id] = cur;
        l.insert(x);
    }
    for(auto u: ans) cout<<u<<"\n";
}

signed main(){
    faster
    int t = 1; 
    cin>>t;
    for(int i = 1; i <= t; i++){
        solve(i);
    }
    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:12:39
Judged At
2024-10-03 17:12:39
Judged By
Score
100
Total Time
491ms
Peak Memory
15.02 MiB