| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
大牛们帮帮忙,AC了但析构函数有问题,造成内存泄漏,怎么改?#include <stdio.h>
#define N 50000
int array[N+1];
struct Value{
int max;
int min;
};
class Linetree{
private:
int x;
int y;
int valid;
struct Value v;
Linetree* lchild;
Linetree* rchild;
public:
Linetree(int,int);
~Linetree();
struct Value find(int,int);
};
Linetree::Linetree(int l,int r){
x=l;
y=r;
if(l!=r-1){
int m=(l+r)/2;
lchild=new Linetree(l,m);
rchild=new Linetree(m,r);
valid=0;
}else {
lchild=rchild=NULL;
if(array[l]<array[r]){ v.max=array[r]; v.min=array[l]; }
else { v.max=array[l]; v.min=array[r]; }
valid=1;
}
}
Linetree::~Linetree(){
if(lchild)lchild->~Linetree();
if(rchild)rchild->~Linetree();
if(lchild||rchild)delete this;
}
struct Value Linetree::find(int l,int r){
struct Value tmp1,tmp2;
tmp1.max=tmp2.max=0;
tmp1.min=tmp2.min=2000000;
if(l==r){
tmp1.max=tmp1.min=array[l];
return tmp1;
}
if( l<=x && y<=r ){
if(valid){
return v;
}else {
tmp1=lchild->find(l,r);
tmp2=rchild->find(l,r);
v.max=tmp1.max>tmp2.max?tmp1.max:tmp2.max;
v.min=tmp1.min<tmp2.min?tmp1.min:tmp2.min;
valid=1;
return v;
}
}else {
if( l < (x+y)/2 ){
tmp1=lchild->find(l,r);
}
if( (x+y)/2<r ){
tmp2=rchild->find(l,r);
}
tmp1.max=tmp1.max>tmp2.max?tmp1.max:tmp2.max;
tmp1.min=tmp1.min<tmp2.min?tmp1.min:tmp2.min;
return tmp1;
}
}
int main(){
int i,n,q,l1,l2;
struct Value t;
freopen("F:\\pku_test.txt","r",stdin);
scanf("%d%d",&n,&q);
for(i=1;i<=n;i++)
scanf("%d",&array[i]);
Linetree *s=new Linetree(1,n);
for(i=1;i<=q;i++){
scanf("%d%d",&l1,&l2);
t=s->find(l1,l2);
printf("%d\n",t.max-t.min);
}
s->~Linetree();//runtime error!!
return 0;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
用线段树做的,自己构造的析构函数,小规模的用例没发现问题但提交就runtime error
我把s->Linetree()注释掉就能AC,但是内存却会泄漏,不知道怎么修改~Linetree()函数?
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator