#include <iostream>
#include <vector>
using namespace std;
long long seg[4*1000000];
vector<long long> vettore;
void update(int ind, int low, int high, int l,int differenza){
if(high<l || low>l)return;
seg[ind]+=differenza;
int mid=(low+high)/2;
update(2*ind+1,low,mid,l,differenza);
update(2*ind+2,mid+1,high,l,differenza); }
void build(int ind, int low, int high){
if(low==high){
seg[ind]=vettore[low];
return;
}
int mid=(low+high)/2;
build(2*ind+1,low,mid);
build(2*ind+2,mid+1,high);
seg[ind]=seg[2*ind+1]+seg[2*ind+2];
}
void init(vector<long long> a) {
vettore.resize(a.size());
for(int i=0;i<a.size();i++)vettore[i]=a[i];
build(0,0,a.size()-1);
}
long long query(int ind, int low, int high, int l, int r){
if(low>=l&&high<=r){
return seg[ind];
}
if(high<l || low>r)return 0;
int mid=(low+high)/2;
long long left=query(2*ind+1,low,mid,l,r);
long long right=query(2*ind+2,mid+1,high,l,r);
return left+right;
}
long long get_sum(int l, int r) {
r--;
return query(0,0,vettore.size()-1,l,r);
}
void set_range(int l, int r, long long x) {
for(int i=l;i<r;i++){
int differenza=x-vettore[i];
update(0,0,vettore.size()-1,i,differenza);
vettore[i]=x;
}
}
int main(){
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
vector<long long> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
init(a);
for (int i = 0; i < q; i++) {
int op, l, r;
long long x;
cin >> op;
cin >> l >> r;
if (op == 2 or op == 3 or op == 5)
cin >> x;
if (op == 1) cout << get_sum(l, r) << "\n";
if (op == 2) add(l, r, x);
if (op == 3) set_range(l, r, x);
if (op == 4) cout << get_min(l, r) << "\n";
if (op == 5) cout << lower_bound(l, r, x) << "\n";
}
return 0;
}
What I have tried:
The commented functions have to be implemented, but that isn't the point. Can you help me fixing the error? I'm trying to implementing a segment tree, but I know I have to study. I searched the error code online, i know what is, but I don't know how to fix it. If i debug my code, it says segmentation fault but I don't know how it is possible. Can you help me with my error?