You are not logged in. Please login at www.codechef.com to post your questions!

×

SPOJ runtime error (SIGXFSZ)

I am getting runtime error (SIGXFSZ) in the question 1790. Binary Search Heap Construction.

Here is my code.

#include<iostream>
#include<cstdio>
using namespace std;
class name
{
    public:
    int p;
    string l;
    name* left;
    name* right;
    name* parent;
    name(int tp,string tl,name *pa)
    {
        parent=pa;
        p=tp;
        l=tl;
        left=NULL;
        right=NULL;
    }
};
void print(name *p);
name* rotate(name* p,name* parent,name* root)
{
    if (p->parent==NULL) return root;
    if (p->p>p->parent->p)
    {
        if (p->parent->left==p)
        {
            p->parent->left=p->right;
            if (p->right)
                p->right->parent=p->parent;
            p->right=p->parent;
            if (p->parent->parent)
                p->parent->parent->left=p;
            p->parent=p->parent->parent;
            p->right->parent=p;

        }
        else
        {
            p->parent->right=p->left;
            if (p->left)
                p->left->parent=p->parent;
            p->left=p->parent;
            if (p->parent->parent)
                p->parent->parent->right=p;
            p->parent=p->parent->parent;
            p->left->parent=p;
        }
        if (p->parent==NULL)
        {
            root=p;
            return root;
        }
        else
            return rotate(p,p->parent,root);
    }
    return root;
}
class treap
{
    public:
    name* root;
    treap()
    {
        root=NULL;
    }
    void insert(int tp,string tl)
    {
        if (root==NULL)
        {
            root=new name(tp,tl,NULL);
        }
        else
        {
            name* p=root;
            while (1)
            {
                if (tl<p->l&&p->left==NULL)
                {
                    p->left=new name(tp,tl,p);
                    rotate(p->left,p->left->parent,root);
                    return;
                }
                else if (tl<p->l&&p->left!=NULL)
                {
                    p=p->left;
                }
                else if (tl>p->l&&p->right==NULL)
                {
                    p->right=new name(tp,tl,p);
                    root=rotate(p->right,p->right->parent,root);    
                    return;
                }
                else if (tl>p->l&&p->right!=NULL)
                {
                    p=p->right;
                }
            }
        }
    }
};
void print(name *p)
{
    printf("(");
    if (p->left!=NULL)
    {
        print(p->left);
    }
    printf("%s/%d",p->l.c_str(),p->p);
    if (p->right!=NULL)
    {
        print(p->right);        
    }
    printf(")");
}
string in()
{
    char c,ans[100];
    scanf("%c",&c);
    int k=0;
    while (c==' ')scanf("%c",&c);   
    while (c!='/')
    {
        ans[k++]=c;
        scanf("%c",&c); 
    }
    ans[k]=0;
    return string(ans);
}
int main()
{
    int n,i,tp;
    string tl;
    scanf("%d",&n);
    while (n!=0)
    {
        treap tree;
        for (i=0;i<n;i++)
        {

            tl=in();

            scanf("%d",&tp);
            tree.insert(tp,tl);
        }
        print(tree.root);
        printf("\n");
        scanf("%d",&n);
    }
    return 0;
}

Please Help.

asked 18 Sep '14, 18:18

m_garg's gravatar image

3★m_garg
31125
accept rate: 0%


I am still stuck on this question.

Please help.

link

answered 22 Sep '14, 18:59

m_garg's gravatar image

3★m_garg
31125
accept rate: 0%

i have the same problem did you find any solution ? please answer me

link

answered 08 Jun '16, 06:34

yahya_switto's gravatar image

0★yahya_switto
1
accept rate: 0%

Just check ur code, how it will allocate memory.

link

answered 12 Oct '16, 20:50

atom_10's gravatar image

0★atom_10
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,136
×532
×228
×5

question asked: 18 Sep '14, 18:18

question was seen: 3,415 times

last updated: 12 Oct '16, 20:50