Runtime error, c++, I don’t know why// is this problem ( https://www.urionlinejudge.com.br/judge/problems/view/2857)

Asked

Viewed 79 times

-4

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

#define MAXN 100100

//int vet[MAXN];
int N;

void atualiza(int x, int v,int[],int);
int soma(int x,int [],int);

int main()
{
    int n, m, x,y,a,b,c,d;
    int acum=0;
    int tam;


    cin>>n>>m;
    int vet[n];
    N=n;

    tam=sizeof(vet);
    //cout<<"!"<<tam<<endl;

    memset(vet, 0, sizeof(vet));


    for(int i=0;i<m;i++)
    {

        cin>>n;


        if(n==1)
        {
            cin>>x>>y;

            atualiza(x,y,vet,N);

        }
        else
        {
            cin>>a>>b>>c>>d;

            if(b==c and a<c)
            {
                d=soma(d,vet,N);
                a=soma(a-1,vet,N);

                cout<<d-a<<endl;
                acum=0;
            }
            else if(b>c and a<c)
            {
                a=soma(a-1,vet,N);
                b=soma(c-1,vet,N);

                acum+=b-a;

                c=soma(b-1,vet,N);
                d=soma(d,vet,N);

                acum+=d-c;



                cout<<acum<<endl;
                acum=0;

            }
            else if(b>c and a>c)
            {
                if(b>a)
                {

                    a=soma(a,vet,N);
                    c=soma(c-1,vet,N);

                    acum+=a-c;

                    b=soma(b-1,vet,N);
                    d=soma(d,vet,N);

                    acum+=d-b;



                    cout<<acum<<endl;
                    acum=0;
                }

            }
            else if((b>c and a>c))
            {
                if(b==a)
                {
                    a=soma(a-1,vet,N);
                    c=soma(c-1,vet,N);

                    acum+=a-c;

                    b=soma(b-1,vet,N);
                    d=soma(d,vet,N);

                    acum+=d-b;



                    cout<<acum<<endl;
                    acum=0;
                }
            }
            else if((b>c and a==c))
            {

                if(a<=d and b<=d)
                {

                    d=soma(d,vet,N);
                    c=soma(c-1,vet,N);
                    acum=d-c;

                    cout<<acum<<endl;
                    acum=0;
                }   
            }
            else if(b<c and a<c)
            {
                a=soma(a-1,vet,N);
                b=soma(b,vet,N);

                acum+=b-a;

                c=soma(c-1,vet,N);
                d=soma(d,vet,N);

                acum+=d-c;

                cout<<acum<<endl;
                acum=0;

            }
            else if(a==b and c==d)
            {
                if(b<c)
                {


                    acum+=vet[a];
                    acum-=vet[a-1];
                    acum+=vet[c];
                    acum-=vet[c-1];

                    cout<<acum<<endl;
                    acum=0;

                }


            }
            else if((a==b and c==d) )
            {
                if(a==c)
                {

                    acum+=vet[a];
                    acum-=vet[a-1];



                    cout<<acum<<endl;
                    acum=0;
                }


            }

            else{

                c=soma(c-1,vet,N);
                b=soma(b,vet,N);
                acum=b-c;

                cout<<acum<<endl;
                acum=0;


            }


        }

    }


    return 0;
}
void atualiza(int x, int v, int vet[],int N){

    while(x <= N){ // nosso teto, que é quando vamos parar de rodar o algoritmo
        vet[x] += v; // adicionamos v frutas a arvore[x], como devemos
        x += (x & -x);  // atualizamos o valor de x adicionado ele ao seu bit menos significante
    }

}
int soma(int x,int vet[],int N){

    int s = 0;

    if(x<0)
        x++;

    while(x > 0){ // vamos reduzindo x até acabarmos (quando chegamos a zero)
        s += vet[x]; // adicionamos o pedaço de árvore atual à soma
        x -= (x & -x);  // removemos o bit menos significante
    }

    return s;
}
  • I don’t program in c++, but I wouldn’t have to use vector ?

  • But I’m using a vector, create it right after typing n, see "Cin>n>m; int vet[n];"

  • Using the vector class would not be better no, I’ve seen in some codes that they use this class to create vectors

  • Yeah, I’ll try this class to see and give an answer.

  • Usually the Runtime Uri error when it tries to use a memory position that does not exist

  • I just remembered that there’s a problem,?

  • look at this link, I did a search, in this example I will read 10 values and if it is negative I show 1 https://ideone.com/d6W6FB, I am using vector

  • 1

    Could you at least explain what you wanted this code to do?

  • I’ve already edited and put together what the problem is, sorry I haven’t done it before.

Show 4 more comments

1 answer

0

The size of a vector has to be a constant, you can allocate the vector dynamically by doing :

int *n;
cin >> tam;
n = new int[tam];

No further use of the memset, because the vector will be filled with 0.

  • Okay, I’ll test it like this.

  • do not forget to make a del [] n when no longer need the array, if I have somehow contributed an up in the answer.

  • In case, I’ll need it by the end of the program itself.

  • @Wellingtonribeiro but before your program closes free up the memory space used by the n array, it’s good programming practice.

  • I understand, I will do it, but even using the comment this way, the values are coming out now with a value less, as if before leave 8 now left 7. kkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.