Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在一定的数组范围内,我们能用最小段树来回答小于k的元素的数目吗?_C++_Arrays_Algorithm_Data Structures_Segment Tree - Fatal编程技术网

C++ 在一定的数组范围内,我们能用最小段树来回答小于k的元素的数目吗?

C++ 在一定的数组范围内,我们能用最小段树来回答小于k的元素的数目吗?,c++,arrays,algorithm,data-structures,segment-tree,C++,Arrays,Algorithm,Data Structures,Segment Tree,我想用一个最小段树来回答在一个未排序的整数数组中,在一个特定的数组范围l到r中不少于k的元素。在递归函数查询中,如果树元素位于该范围内,且该范围的最小值大于k,即所有元素都大于k,则返回给定树元素的范围;如果树元素仅指向一个数组元素且该数组元素也小于k,则返回0。 请告诉我我的方法有什么问题 在这里,我们将没有元素和没有查询作为输入n和q,然后对于接下来的n行,我们将数组元素作为输入。然后,对于每个查询,我们需要回答[l,r]范围内大于k的元素数量 void buildTree(int *a,i

我想用一个最小段树来回答在一个未排序的整数数组中,在一个特定的数组范围l到r中不少于k的元素。在递归函数查询中,如果树元素位于该范围内,且该范围的最小值大于k,即所有元素都大于k,则返回给定树元素的范围;如果树元素仅指向一个数组元素且该数组元素也小于k,则返回0。 请告诉我我的方法有什么问题

在这里,我们将没有元素和没有查询作为输入n和q,然后对于接下来的n行,我们将数组元素作为输入。然后,对于每个查询,我们需要回答[l,r]范围内大于k的元素数量

void buildTree(int *a,int s,int e,int *tree,int index){
    //Base Case
    if(s==e){
        tree[index]=a[s];
        return;
    }
    //Recursive case
    int mid=(s+e)/2;
    buildTree(a,s,mid,tree,2*index);
    buildTree(a,mid+1,e,tree,2*index+1);
    tree[index]=min(tree[2*index],tree[2*index+1]);
    return;
}
int query(int *tree,int ss,int se,int qs,int qe,int k,int index){
    //Complete Overlap
    if(se==ss&&tree[index]<k)return 0;
    if(ss>=qs && se<=qe&& tree[index]>=k)
     return se-ss+1;
     //No Overlap
    if(ss>qe || qs>se)
     return 0;
     //Partial Overlap
    int mid=(ss+se)/2;
    int leftAns=query(tree,ss,mid,qs,qe,k,2*index);
    int rightAns=query(tree,mid+1,se,qs,qe,k,2*index+1);
    return leftAns+rightAns;
}
void updateNode(int *tree,int ss,int se,int i,int update,int index){
    //No Overlap
    if(i>se||i<ss)
    return;
    //LeafNode
    if(ss==se){
        tree[index]=update;
        return;
    }
    //Partial Overlap
    int mid=(ss+se)/2;
    updateNode(tree,ss,mid,i,update,2*index);
    updateNode(tree,mid+1,se,i,update,2*index+1);
    tree[index]=min(tree[2*index],tree[2*index+1]);
    return;
   }

int main(){
int n,q;
cin>>n>>q;
int arr[n];
for(int i=0;i<n;i++)cin>>arr[i];
int tree[4*n]; 
buildTree(a,0,n-1,tree,1);
for(int i=0;i<q;i++){
int l,r,k;
cin>>l>>r>>k;
cout<<query(tree,0,n-1,l-1,r-1,k,1)<<"\n";
}
return 0;
}
void buildTree(int*a、int-s、int-e、int*tree、int-index){
//基本情况
如果(s==e){
树[索引]=a[s];
返回;
}
//递归案例
int mid=(s+e)/2;
构建树(a、s、mid、tree、2*索引);
构建树(a,mid+1,e,tree,2*index+1);
树[index]=min(树[2*索引],树[2*索引+1]);
返回;
}
int查询(int*tree、int-ss、int-se、int-qs、int-qe、int-k、int-index){
//完全重叠
if(se==ss&&tree[index]=qs&&se=k)
返回se ss+1;
//无重叠
如果(ss>qe | qs>se)
返回0;
//部分重叠
int mid=(ss+se)/2;
int leftAns=query(树、ss、mid、qs、qe、k、2*索引);
int rightAns=query(树,mid+1,se,qs,qe,k,2*index+1);
返回左+右;
}
void updateNode(int*tree、int-ss、int-se、int-i、int-update、int-index){
//无重叠
如果(i>se | i>n>>q;
int-arr[n];
对于(int i=0;i>arr[i];
int树[4*n];
构建树(a,0,n-1,树,1);
对于(inti=0;i>l>>r>>k;

这里看不到任何树。这是已排序的arrray吗?其次,你写了所有代码,不知道它是否能解决问题?在写任何代码之前,你是否先在纸上计划了这个问题?一旦编写了程序,你应该很容易看到你写的代码偏离了原始计划,或者将代码修复到按照你的计划行事,或者意识到计划有缺陷,你需要重新开始。我编辑了这篇文章,这里我们把元素的数量和查询的数量作为输入n和q,然后在接下来的n行中,我们把数组元素作为输入。然后,对于每个查询,我们需要回答[l,r]范围内大于k的元素的数量。