/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 516.0 KiB
#2 Accepted 1ms 324.0 KiB
#3 Accepted 2ms 324.0 KiB
#4 Accepted 209ms 4.43 MiB
#5 Accepted 254ms 13.633 MiB
#6 Accepted 308ms 9.887 MiB
#7 Accepted 128ms 1020.0 KiB
#8 Accepted 224ms 5.535 MiB
#9 Accepted 203ms 5.242 MiB
#10 Accepted 281ms 18.551 MiB
#11 Accepted 43ms 708.0 KiB
#12 Accepted 86ms 756.0 KiB
#13 Accepted 1ms 324.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
 
#define mxSIZE 200000 + 5
#define pii pair<int, int>
#define S second
#define F first
#define int long long
 
/*--------------- BINARY INDEXED TREE. //BEGINS -----------*/
vector<int>BIT;
 
/**
 * @brief This is a utility function which calculate
 * the sum of array[0 ... idx] using BIT data structure.
 *
 * @param idx Index tree node.
 * @return int
 */
int Sum(int idx){
    int _sum = 0;
 
    /* Traversing the ancestors of idx node. */
    while(idx > 0){
        _sum += BIT[idx]; // Add current element in count.
 
        idx -= (idx & -idx); // Move to 'sum' parent node.
    }
    // Return count of smaller element on right side.
    return _sum;
}
 
/**
 * @brief This function updates a node of Binary Indexed Tree(BIT)
 * at given index 'idx'. Given value 'change' is added to BIT[idx] node
 * and its ancestors.
 *
 * @param idx Index tree node.
 * @param change Effective difference that needs to be updated.
 * @return void
 */
void Update(int idx, int change){
 
    /* Traverse over all ancestors of BIT[idx] and add 'change' */
    while(idx < BIT.size()){    
        BIT[idx] += change; // Add 'change' in current node.
 
        idx += (idx & -idx); // Move to 'update' parent node.
    }
}
/*--------------- BINARY INDEXED TREE. //ENDS   -----------*/
 
 
/**
 * @brief This function transforms an array into an array containing
 * elements from 1 to n(Preserving the relative order between the elements). For example {-4, 10, 5} --> {1, 3, 2}
 *
 * @param v Input array.
 */
void transformArray(vector<int> &v){
    int sz = v.size();
    vector<pii>temp(sz);
 
    for(int i=0; i<sz; i++)
        temp[i].F = v[i], temp[i].S = i;
   
    sort(temp.begin(), temp.end());
 
    for(int i=0; i<sz; i++){
        v[temp[i].S] = i+1;
    }
}
 
// Main function which gives arrays containing number of smaller elements on the right
// and greater elements on the left.
void solve(vector<int> &v, int n){
 

    
    // for(int i = 0; i < n; i++){
    //     cout << v[i] << " ";
    // }
    // cout << endl;

    vector<int>leftEqual(n + 1, 0), rightEqual(n + 1, 0);
    map<int,int>mp,mp1;
    for(int i = 0; i < n; i++){
        leftEqual[i] = mp[v[i]];
        mp[v[i]]++;
    }
    for(int i = n - 1; i >= 0; i--){
        rightEqual[i] = mp1[v[i]];
        mp1[v[i]]++;
    }
    // for(int i = 0; i < n; i++){
    //     cout << rightEqual[i] << " ";
    // }
    // cout << endl;

    // Converting array to an array containing elements from [1, n]
    transformArray(v);
    BIT.clear();
 
    /**
     * Create a Binary Indexed Tree of size equal to
     * maxElement + 1. ('+1' so that the elements can directly
     * be used as index)
     */
    BIT.resize(n+1, 0);
 
    // To store count of smaller elements on right
    // of greater elements on left.
    vector<int> cntSmallerRight(n), cntGreaterLeft(n);
 
    /* Calculate count of smaller elements on right */
    for(int i=n-1; i>=0; i--){
        // Get count of elements smaller than v[i].
        cntSmallerRight[i] = Sum(v[i]-1);
 
        // Update current element in the BIT
        Update(v[i], 1);
    }
 
    BIT.clear();
    BIT.resize(n+1, 0);
 
    /* Calculate count of greater elements on left */
    for(int i=0; i<n; i++){
        // Get count of elements greater than v[i].
        cntGreaterLeft[i] = i - Sum(v[i]-1);
       
        // Update current element in the BIT
        Update(v[i], 1);
    }
 
    // cout << "Count of Smaller on right: ";
    // for(int i=0; i<n; i++)cout << cntSmallerRight[i] << " ";
    // cout<<endl;
 
    // cout << "Count of Greater on left: ";
    // for(int i=0; i<n; i++)cout << cntGreaterLeft[i] << " ";
    // cout<<"\n\n";
    

    int q;
    cin >> q;
    while(q--){
        int x;
        cin >> x;
        x--;
        int ans = 0;
        int right_small = cntSmallerRight[x];
        int left_greater = cntGreaterLeft[x];
        ans += (right_small * left_greater);
        int right_greater = ((n - x - 1) - cntSmallerRight[x]) - rightEqual[x];
        int left_smaller = (x - cntGreaterLeft[x]) - leftEqual[x];
        ans += (right_greater * left_smaller);
        cout << ans << endl;
    }
}
 
// Driver Function.
int32_t main(){
    ios::sync_with_stdio(false);cin.tie(0),cin.tie(0);
    int tt = 1;
    cin >> tt;
    while(tt--){
        int n; cin >> n;
        vector<int>v(n);
 
        for(int i=0; i<n; i++)cin >> v[i];
 
        solve(v, n);
    }
    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:44:02
Judged At
2024-11-11 02:45:44
Judged By
Score
100
Total Time
308ms
Peak Memory
18.551 MiB