Data structures 计算小于i左边A[i]的值的有效程序

Data structures 计算小于i左边A[i]的值的有效程序,data-structures,tree,array-algorithms,Data Structures,Tree,Array Algorithms,是否有人能提供NlogN复杂度高效程序来计算小于i左边A[i]的值 我知道在n广场怎么办。 如果可能,请提供链接。我想到的一种方法是反转数组O(n),您的问题将归结为查找使用BST并使用O(nlogn)存储每个节点的子节点数的 这也会很有用。我想到的一种方法是反转数组O(n),您的问题归结为找到使用BST并使用O(nlogn)存储每个节点的子节点数的 这也会很有用。我想到的一种方法是反转数组O(n),您的问题归结为找到使用BST并使用O(nlogn)存储每个节点的子节点数的 这也会很有用。我想到

是否有人能提供NlogN复杂度高效程序来计算小于i左边A[i]的值

我知道在n广场怎么办。
如果可能,请提供链接。

我想到的一种方法是反转数组O(n),您的问题将归结为查找使用BST并使用O(nlogn)存储每个节点的子节点数的


这也会很有用。

我想到的一种方法是反转数组O(n),您的问题归结为找到使用BST并使用O(nlogn)存储每个节点的子节点数的


这也会很有用。

我想到的一种方法是反转数组O(n),您的问题归结为找到使用BST并使用O(nlogn)存储每个节点的子节点数的


这也会很有用。

我想到的一种方法是反转数组O(n),您的问题归结为找到使用BST并使用O(nlogn)存储每个节点的子节点数的


这也很有用。

使用索引树。基本上,您可以遍历数组并在索引A[i]处更新索引树。对于每一个数字,答案是你到达它的那一刻在索引树中它前面的总和。这种方法的问题是占用大量内存。如果您的数字可能非常大(或非常小),您可能需要压缩它们。这里是C++代码:

#include<iostream>
using namespace std;
const int MAX_N=1024; //maximum number of numbers
const int MAX_VAL=1023; //maximum value that the numbers take
const int MIN_VAL=-1024; //minimum value that the numbers take
int arr[MAX_N];
int indTree[MAX_VAL-MIN_VAL+1];
int ans[MAX_N];
int n;
void update(int ind, int val) //standard index tree function that updates the value at index ind
{
    while (ind<=MAX_VAL-MIN_VAL+1)
    {
        indTree[ind-1]+=val;
        ind+=ind&-ind;
    }
}
int sum(int ind) //standard index tree function that calculates the sum of element before ind
{
    int sum=0;
    while (ind>0)
    {
        sum+=indTree[ind-1];
        ind-=ind&-ind;
    }
    return sum;
}
void makePositive() //makes all numbers positive
{
    for (int i=0;i<n;++i)
    {
        arr[i]=arr[i]-MIN_VAL+1;
    }
}
void solve()
{
    makePositive();
    for (int i=0;i<n;++i)
    {
        //ans[i]=sum(arr[i]); choose one of the two
        ans[i]=sum(arr[i]-1); //the first one calculates the number of numbers smaller or equal to the current
        //and the second one calculates the number of numbers strictly smaller than the current
        update(arr[i],1);
    }
}
void input()
{
    cin>>n;
    for (int i=0;i<n;++i)
    {
        cin>>arr[i];
    }
}
void output()
{
    for (int i=0;i<n;++i)
    {
        cout<<ans[i]<<" ";
    }
    cout<<endl;
}
int main()
{
    input();
    solve();
    output();
    return 0;
}
#包括
使用名称空间std;
常数int MAX_N=1024//最大数目
常数int MAX_VAL=1023//数字取的最大值
常量int MIN_VAL=-1024//数字取的最小值
整数arr[MAX_N];
intindtree[MAX_VAL-MIN_VAL+1];
int ans[MAX_N];
int n;
void update(int ind,int val)//更新索引ind处的值的标准索引树函数
{
while(ind0)
{
sum+=indTree[ind-1];
ind-=ind&-ind;
}
回报金额;
}
void makePositive()//使所有数字都为正数
{
对于(int i=0;in;
对于(int i=0;i>arr[i];
}
}
无效输出()
{

对于(int i=0;i使用索引树。基本上,您可以遍历数组并在索引A[i]处更新索引树。对于每个数字,答案都是到达它时索引树中它前面的总和。此方法的问题是需要大量内存。如果您的数字可能非常大(或非常小)你可能需要压缩它们。这里是C++代码:

#include<iostream>
using namespace std;
const int MAX_N=1024; //maximum number of numbers
const int MAX_VAL=1023; //maximum value that the numbers take
const int MIN_VAL=-1024; //minimum value that the numbers take
int arr[MAX_N];
int indTree[MAX_VAL-MIN_VAL+1];
int ans[MAX_N];
int n;
void update(int ind, int val) //standard index tree function that updates the value at index ind
{
    while (ind<=MAX_VAL-MIN_VAL+1)
    {
        indTree[ind-1]+=val;
        ind+=ind&-ind;
    }
}
int sum(int ind) //standard index tree function that calculates the sum of element before ind
{
    int sum=0;
    while (ind>0)
    {
        sum+=indTree[ind-1];
        ind-=ind&-ind;
    }
    return sum;
}
void makePositive() //makes all numbers positive
{
    for (int i=0;i<n;++i)
    {
        arr[i]=arr[i]-MIN_VAL+1;
    }
}
void solve()
{
    makePositive();
    for (int i=0;i<n;++i)
    {
        //ans[i]=sum(arr[i]); choose one of the two
        ans[i]=sum(arr[i]-1); //the first one calculates the number of numbers smaller or equal to the current
        //and the second one calculates the number of numbers strictly smaller than the current
        update(arr[i],1);
    }
}
void input()
{
    cin>>n;
    for (int i=0;i<n;++i)
    {
        cin>>arr[i];
    }
}
void output()
{
    for (int i=0;i<n;++i)
    {
        cout<<ans[i]<<" ";
    }
    cout<<endl;
}
int main()
{
    input();
    solve();
    output();
    return 0;
}
#包括
使用名称空间std;
const int MAX_N=1024;//最大数字数
const int MAX_VAL=1023;//数字的最大值
const int MIN_VAL=-1024;//数字取的最小值
整数arr[MAX_N];
intindtree[MAX_VAL-MIN_VAL+1];
int ans[MAX_N];
int n;
void update(int ind,int val)//更新索引ind处的值的标准索引树函数
{
while(ind0)
{
sum+=indTree[ind-1];
ind-=ind&-ind;
}
回报金额;
}
void makePositive()//使所有数字都为正数
{
对于(int i=0;in;
对于(int i=0;i>arr[i];
}
}
无效输出()
{

对于(int i=0;i使用索引树。基本上,您可以遍历数组并在索引A[i]处更新索引树。对于每个数字,答案都是到达它时索引树中它前面的总和。此方法的问题是需要大量内存。如果您的数字可能非常大(或非常小)你可能需要压缩它们。这里是C++代码:

#include<iostream>
using namespace std;
const int MAX_N=1024; //maximum number of numbers
const int MAX_VAL=1023; //maximum value that the numbers take
const int MIN_VAL=-1024; //minimum value that the numbers take
int arr[MAX_N];
int indTree[MAX_VAL-MIN_VAL+1];
int ans[MAX_N];
int n;
void update(int ind, int val) //standard index tree function that updates the value at index ind
{
    while (ind<=MAX_VAL-MIN_VAL+1)
    {
        indTree[ind-1]+=val;
        ind+=ind&-ind;
    }
}
int sum(int ind) //standard index tree function that calculates the sum of element before ind
{
    int sum=0;
    while (ind>0)
    {
        sum+=indTree[ind-1];
        ind-=ind&-ind;
    }
    return sum;
}
void makePositive() //makes all numbers positive
{
    for (int i=0;i<n;++i)
    {
        arr[i]=arr[i]-MIN_VAL+1;
    }
}
void solve()
{
    makePositive();
    for (int i=0;i<n;++i)
    {
        //ans[i]=sum(arr[i]); choose one of the two
        ans[i]=sum(arr[i]-1); //the first one calculates the number of numbers smaller or equal to the current
        //and the second one calculates the number of numbers strictly smaller than the current
        update(arr[i],1);
    }
}
void input()
{
    cin>>n;
    for (int i=0;i<n;++i)
    {
        cin>>arr[i];
    }
}
void output()
{
    for (int i=0;i<n;++i)
    {
        cout<<ans[i]<<" ";
    }
    cout<<endl;
}
int main()
{
    input();
    solve();
    output();
    return 0;
}
#包括
使用名称空间std;
const int MAX_N=1024;//最大数字数
const int MAX_VAL=1023;//数字的最大值
const int MIN_VAL=-1024;//数字取的最小值
整数arr[MAX_N];
intindtree[MAX_VAL-MIN_VAL+1];
int ans[MAX_N];
int n;
void update(int ind,int val)//更新索引ind处的值的标准索引树函数
{
while(ind0)
{
sum+=indTree[ind-1];
ind-=ind&-ind;
}
回报金额;
}
void makePositive()//使所有数字都为正数
{
对于(int i=0;in;
对于(int i=0;i>arr[i];
}
}
无效输出()
{

对于(int i=0;i使用索引树。基本上,您可以遍历数组并在索引A[i]处更新索引树。对于每个数字,答案都是到达它时索引树中它前面的总和。此方法的问题是需要大量内存。如果您的数字可能非常大(或非常小)你可能需要压缩它们。这里是C++代码:

#include<iostream>
using namespace std;
const int MAX_N=1024; //maximum number of numbers
const int MAX_VAL=1023; //maximum value that the numbers take
const int MIN_VAL=-1024; //minimum value that the numbers take
int arr[MAX_N];
int indTree[MAX_VAL-MIN_VAL+1];
int ans[MAX_N];
int n;
void update(int ind, int val) //standard index tree function that updates the value at index ind
{
    while (ind<=MAX_VAL-MIN_VAL+1)
    {
        indTree[ind-1]+=val;
        ind+=ind&-ind;
    }
}
int sum(int ind) //standard index tree function that calculates the sum of element before ind
{
    int sum=0;
    while (ind>0)
    {
        sum+=indTree[ind-1];
        ind-=ind&-ind;
    }
    return sum;
}
void makePositive() //makes all numbers positive
{
    for (int i=0;i<n;++i)
    {
        arr[i]=arr[i]-MIN_VAL+1;
    }
}
void solve()
{
    makePositive();
    for (int i=0;i<n;++i)
    {
        //ans[i]=sum(arr[i]); choose one of the two
        ans[i]=sum(arr[i]-1); //the first one calculates the number of numbers smaller or equal to the current
        //and the second one calculates the number of numbers strictly smaller than the current
        update(arr[i],1);
    }
}
void input()
{
    cin>>n;
    for (int i=0;i<n;++i)
    {
        cin>>arr[i];
    }
}
void output()
{
    for (int i=0;i<n;++i)
    {
        cout<<ans[i]<<" ";
    }
    cout<<endl;
}
int main()
{
    input();
    solve();
    output();
    return 0;
}
#包括
使用名称空间std;
const int MAX_N=1024;//最大数字数
const int MAX_VAL=1023;//数字的最大值
const int MIN_VAL=-1024;//数字取的最小值
整数arr[MAX_N];
intindtree[MAX_VAL-MIN_VAL+1];
int ans[MAX_N];
int n;
void update(int ind,int val)//更新索引ind处的值的标准索引树函数
{
while(ind0)
{
sum+=indTree[ind-1];
ind-=ind&-ind;
}
回报金额;
}
void makePositive()//使所有数字都为正数
{
对于(int i=0;in;
对于(int i=0;i>arr[i];
}
}
无效输出()
{
对于(int i=0;i