/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Wrong Answer 1ms 532.0 KiB
#3 Wrong Answer 1ms 464.0 KiB
#4 Wrong Answer 130ms 1.91 MiB
#5 Wrong Answer 83ms 6.684 MiB
#6 Wrong Answer 194ms 4.492 MiB
#7 Wrong Answer 92ms 836.0 KiB
#8 Wrong Answer 189ms 4.492 MiB
#9 Wrong Answer 183ms 4.543 MiB
#10 Accepted 182ms 4.531 MiB
#11 Wrong Answer 33ms 676.0 KiB
#12 Wrong Answer 64ms 632.0 KiB
#13 Wrong Answer 1ms 532.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){
 
    // 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.
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:24:21
Judged At
2024-10-03 17:24:21
Judged By
Score
12
Total Time
194ms
Peak Memory
6.684 MiB