/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 328.0 KiB
#2 Accepted 2ms 328.0 KiB
#3 Accepted 2ms 332.0 KiB
#4 Accepted 193ms 3.02 MiB
#5 Accepted 852ms 15.125 MiB
#6 Accepted 370ms 10.566 MiB
#7 Accepted 82ms 952.0 KiB
#8 Accepted 202ms 9.938 MiB
#9 Accepted 212ms 9.828 MiB
#10 Accepted 228ms 12.746 MiB
#11 Accepted 31ms 616.0 KiB
#12 Accepted 58ms 588.0 KiB
#13 Accepted 2ms 332.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-12-17 11:33:08
Judged By
Score
100
Total Time
852ms
Peak Memory
15.125 MiB