ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1126. Magnetic Storms

access violation error
Posted by Debagnik Roy 17 Nov 2018 12:50
#include<bits/stdc++.h>
using namespace std;
typedef struct HeapNode
{
    int *harr;
    int size;
}node;

int leftChild(int n)
{
    return (2*n+1);
}
int rightChild(int n)
{
    return (2*n+2);
}
int getParent(int n)
{
    return (n-1)/2;
}
void maxHeapify(int n,node* nd)
{
    int l=leftChild(n);
    int r=rightChild(n);
    int largest;

    if(l < nd->size && nd->harr[l] > nd->harr[n])
        largest=l;
    else
        largest=n;

    if(r < nd->size && nd->harr[r] > nd->harr[largest])
        largest=r;
    if(largest!=n)
    {
        int t=nd->harr[n];
        nd->harr[n] = nd->harr[largest];
        nd->harr[largest] =t;
        maxHeapify(largest,nd);
    }
}
void buildMaxHeap(node* node)
{
    int i;
    int x=node->size-1;
    x=x/2;
    for(i=x;i>=0;i--)
        {
            maxHeapify(i,node);
        }
}

node* createHeap(int size)
{
    node *n=(node*)malloc(sizeof(node));
    n->size=size;
    n->harr=(int*)malloc(sizeof(int)*size);
}


void addElement(int n,node *nd)
{
    int i=++nd->size;
    nd->harr=(int*)realloc(nd->harr,sizeof(int)*i);
    nd->harr[i-1]=n;
    buildMaxHeap(nd);
}

void deleteAt(int pos,node* nd)
{
    int i;
    nd->harr[pos-1]=nd->harr[nd->size-1];
    --nd->size;
    nd->harr=(int*)realloc(nd->harr,sizeof(int)*nd->size);
    i=pos-1;
    buildMaxHeap(nd);
}

int search(node *nd,int value)
{
        int i=0;
        for(i=0;i<nd->size;i++)
        {
            if(nd->harr[i]==value)
                return i;
        }
}
int peek(node *nd)
{
    return nd->harr[0];
}


int main()
{
    int m;
    cin>>m;
    node * Node=createHeap(m);
    int *arr=(int*)malloc(sizeof(int)*m);
    int *arr2=(int*)malloc(sizeof(int)*50001);
    int n=0;

    int i=0,j=0;
    while (n!=-1 && i<m)
        {
            cin>>n;
            arr[i++]=n;
            arr2[j++]=n;
        }
    Node->harr=arr;
    buildMaxHeap(Node);

    cout<<peek(Node)<<endl;

    int s=search(Node,arr2[0]);
    deleteAt(s+1,Node);

    int c=0,r=m-1;
    while(n!=-1)
    {
        c++;
        cin>>n;
        if(n==-1)break;
        arr2[r+c]=n;
        addElement(n,Node);
        cout<<peek(Node)<<endl;

        s=search(Node,arr2[c]);
        deleteAt(s+1,Node);
    }
    return 0;
}
this is my code... works fine in my compiler... can anyone say what is this access violation error for?