-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 ?
– rafael marques
But I’m using a vector, create it right after typing n, see "Cin>n>m; int vet[n];"
– Wellington Ribeiro
Using the vector class would not be better no, I’ve seen in some codes that they use this class to create vectors
– rafael marques
Yeah, I’ll try this class to see and give an answer.
– Wellington Ribeiro
Usually the Runtime Uri error when it tries to use a memory position that does not exist
– rafael marques
I just remembered that there’s a problem,?
– Wellington Ribeiro
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
– rafael marques
Could you at least explain what you wanted this code to do?
– Victor Stafusa
I’ve already edited and put together what the problem is, sorry I haven’t done it before.
– Wellington Ribeiro