/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 536.0 KiB
#2 Wrong Answer 1ms 536.0 KiB
#3 Wrong Answer 1ms 512.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
 
 
/*--------------- 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){
 
    // 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];
        int left_smaller = x - cntGreaterLeft[x];
        ans += (right_greater * left_smaller);
        cout << ans << endl;
    }
}
 
// Driver Function.
int main(){
    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:18:48
Judged At
2024-12-17 11:32:59
Judged By
Score
4
Total Time
1ms
Peak Memory
536.0 KiB