Problem Link
My solution
#include<iostream>
using namespace std;
#define MINI -100000000
#define left 2*index+1
#define right 2*index+2
struct node{
long long int mps;
long long int mss;
long long int ms;
long long int sum;
};
int arr[50000];
struct node seg[200000];
struct node t2;
long long int max(long long int a,long long int b){
if(a>b) return a;
else return b;
}
int construct(int l,int r,int index,int *arr,struct node* seg){
if(l==r){
seg[index].mps=arr[l];
seg[index].mss=arr[l];
seg[index].ms=arr[l];
seg[index].sum=arr[l];
return 0;
}
int mid=(l+r)/2;
long long int temp;
construct(l,mid,left,arr,seg);
construct(mid+1,r,right,arr,seg);
seg[index].mps= max(seg.mps,max((seg.sum + seg[right].mps),seg.sum));
seg[index].mss= max(seg[right].mss,max((seg[right].sum + seg.mss),seg[right].sum));
seg[index].sum= seg.sum + seg[right].sum;
seg[index].ms= max(seg[index].mss,max(seg[index].mps,max(seg.mss+seg[right].mps ,max(seg.ms,seg[right].ms))));
return 0;
}
struct node search(int l ,int r,int x,int y,int index,struct node* seg){
if(l>y || r<x) return t2;
else if((l>=x) && (r<=y)) return seg[index];
int mid=(l+r)/2;
struct node temp1= search(l,mid,x,y,left,seg);
struct node temp2= search(mid+1,r,x,y,right,seg);
if((temp1.mss==MINI) && (temp2.mss==MINI))
{
// cout << "both mini" << " " << index << endl;
return t2;
}
else if(temp1.mss==MINI)
{
// cout << "left mini" << " " << index << endl;
return temp2;
}
else if(temp2.mss==MINI)
{
// cout << "right mini" << " " << index << endl;
return temp1;
}
else{
// cout << "index" << " " << index << endl;
struct node t3;
t3.mps= max(temp1.mps,max((temp1.sum + temp2.mps),temp1.sum));
t3.mss= max(temp2.mss,max((temp2.sum + temp1.mss),temp2.sum));
t3.sum= temp1.sum + temp2.sum;
t3.ms= max(t3.mss,max(t3.mps,max(temp1.mss+temp2.mps,max(temp1.ms,temp1.ms))));
return t3;
}
}
int main(){
int n,i,j,m,x,y;
// input n
cin >> n;
int arr[n];
t2.mss=MINI;
t2.mps=MINI;
t2.ms= MINI;
t2.sum= MINI;
struct node ans;
for(i=0;i<n;i++){
// input arr[i]
cin >> arr[i];
}
construct(0,n-1,0,arr,seg);
// input m
cin >> m;
while(m--)
{
// input x and y
cin >> x >> y;
x--; y--;
ans=search(0,n-1,x,y,0,seg);
// output ans.ms
cout << ans.ms << endl;
}
return 0;
}
Can’t find the mistake , plz someone help.