In this HackerEarth Reversing elements problem solution, You are given an array A of size N and Q queries. For each query, you are given two indices of the array L and R. The subarray generated from L to R is reversed. Your task is to determine the maximum sum of the subarrays.

## HackerEarth Reversing elements problem solution.

```#include <bits/stdc++.h>
#include <tr1/unordered_map>
using namespace std;
using namespace std::tr1;
#define opt ios_base::sync_with_stdio(0)
#define lli long long int
#define ulli unsigned long long int
#define I int
#define S string
#define D double
#define rep(i,a,b) for(i=a;i<b;i++)
#define repr(i,a,b) for(i=a;i>b;i--)
#define in(n) scanf("%lld",&n)
#define in2(a,b) scanf("%lld %lld",&a,&b)
#define in3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define out(n) printf("%lld\n",n)
#define inu(a) scanf("%lld",&a)
#define outu(a) printf("%llu",a)
#define ins(s) scanf("%s",&s)
#define outs(s) printf("%s",s)
#define mod 1000000007
#define inf 100000000000000
typedef long long       ll;
typedef pair<lli,lli>  plli;
typedef vector<lli>     vlli;
typedef vector<ulli>    vulli;
typedef vector<ll>      vll;
typedef vector<string>  vs;
typedef vector<plli>     vplli;
#define MM(a,x)  memset(a,x,sizeof(a));
#define ALL(x)   (x).begin(), (x).end()
#define P(x)       cerr<<"{"#x<<" = "<<(x)<<"}"<<endl;
#define P2(x,y)       cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<"}"<<endl;
#define P3(x,y,z)  cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<", "#z" = "<<(z)<<"}"<<endl;
#define P4(x,y,z,w)cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<", "#z" = "<<(z)<<", "#w" = "<<(w)<<"}"<<endl;
#define PP(x,i)     cerr<<"{"#x"["<<i<<"] = "<<x[i]<<"}"<<endl;
#define TM(a,b)     cerr<<"{"#a" -> "#b": "<<1000*(b-a)/CLOCKS_PER_SEC<<"ms}\n";
#define UN(v)    sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define sz() size()
#define nl cout<<"\n"
#define MX1 100005
#define MX2 1000005
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
lli dx[]={0,0,-1,1,-1,-1,1,1};
lli dy[]={1,-1,0,0,1,-1,-1,1};
lli power(lli a,lli b)
{
lli value;
if(b==0)
{
return 1;
}
else if(b%2==0)
{
value=power(a,b/2)%mod;
return(value*value)%mod;
}
else
{
value=power(a,b/2)%mod;
return ((a*value)%mod*(value))%mod;
}
}
struct node
{
lli sum,prefix,suffix,ans;
};
node tree[400005];
lli a[100004],max_start_here[100005],max_end_here[100005],max_pre[100005],max_suf[100005],b[100005];
node combine(node n1,node n2)
{
node temp;
if(n1.sum==-inf)
{
return n2;
}
if(n2.sum==-inf)
{
return n1;
}
temp.sum=n1.sum+n2.sum;
temp.prefix=max(n1.prefix,n1.sum+n2.prefix);
temp.suffix=max(n2.suffix,n2.sum+n1.suffix);
temp.ans=max(n1.ans,max(n2.ans,n1.suffix+n2.prefix));
return temp;
}
void build(lli n,lli s,lli e)
{
if(s==e)
{
tree[n].prefix=a[s];
tree[n].sum=a[s];
tree[n].suffix=a[s];
tree[n].ans=a[s];
}
else
{
lli mid=(s+e)/2;
build(2*n,s,mid);
build(2*n+1,mid+1,e);
tree[n]=combine(tree[2*n],tree[2*n+1]);
}
}
node query(lli n,lli s,lli e,lli l,lli r)
{
if(s>e or e<l or s>r)
{
return {-inf,-inf,-inf,-inf};
}
if(s>=l and e<=r)
{
return tree[n];
}
lli mid=(s+e)/2;
node n1=query(2*n,s,mid,l,r);
node n2=query(2*n+1,mid+1,e,l,r);
node ne=combine(n1,n2);

return ne;
}
int main()
{

#ifndef ONLINE_JUDGE
#endif*/
opt;
lli n,i,q;
cin>>n>>q;
rep(i,1,1+n)
{
cin>>a[i];
b[i]=a[i];
}
rep(i,1,1+n)
{
b[i]=b[i-1]+a[i];
}
max_end_here[1]=a[1];
rep(i,2,1+n)
{
max_end_here[i]=max(a[i],max_end_here[i-1]+a[i]);
}
max_start_here[n]=a[n];
repr(i,n-1,0)
{
max_start_here[i]=max(a[i],a[i]+max_start_here[i+1]);
}
max_pre[1]=max_end_here[1];
rep(i,2,1+n)
{
max_pre[i]=max(max_pre[i-1],max_end_here[i]);
}
max_suf[n]=max_start_here[n];
repr(i,n-1,0)
{
max_suf[i]=max(max_suf[i+1],max_start_here[i]);
}
build(1,1,n);
while(q--)
{
lli l,r;
cin>>l>>r;
node n1=query(1,1,n,l,r);
if(l==1 and r==n)
{
cout<<n1.ans<<endl;
}
else if(l==1)
{
lli ans=n1.ans;
ans=max(ans,n1.prefix+max_start_here[r+1]);
ans=max(ans,max_suf[r+1]);
cout<<ans<<endl;
}
else if(r==n)
{
lli ans=n1.ans;
ans=max(ans,n1.suffix+max_end_here[l-1]);
ans=max(ans,max_pre[l-1]);
cout<<ans<<endl;
}
else
{
lli ans=n1.ans;
ans=max(ans,n1.prefix+max_start_here[r+1]);
ans=max(ans,n1.suffix+max_end_here[l-1]);
ans=max(ans,n1.sum+max(0LL,max_start_here[r+1])+max(0LL,max_end_here[l-1]));
ans=max(ans,max_pre[l-1]);
ans=max(ans,max_suf[r+1]);
cout<<ans<<endl;
}

}
}```

### Second solution

```#include <bits/stdc++.h>
using namespace std;

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); --i)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int FFTMOD = 119 << 23 | 1;
const int INF = (int) 1e9 + 23111992;
const ll LINF = (ll) 1e18 + 23111992;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
inline ull isqrt(ull k) {ull r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}
#define db(x) cerr << #x << " = " << (x) << " ";
#define endln cerr << "\n";
mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int myrand() {return abs((int) mt());}

struct node_t {
long long sum, prf, suf, ans;
node_t() : ans(-LINF) {}
node_t(int val) : sum(val), prf(val), suf(val), ans(val) {}
friend node_t operator + (const node_t& a, const node_t& b) {
if (a.ans == -LINF) return b;
if (b.ans == -LINF) return a;
node_t res;
res.sum = a.sum + b.sum;
res.prf = max(a.prf, a.sum + b.prf);
res.suf = max(b.suf, a.suf + b.sum);
res.ans = max(a.suf + b.prf, max(a.ans, b.ans));
return res;
}
};

const int maxn = 1e5 + 5;
int n, q;
node_t st[maxn << 2];

void upd(int p, int i, int L, int R, int val) {
if (i < L || R < i) return;
if (L == R) {
st[p] = node_t(val);
return;
}
upd(p << 1, i, L, L + R >> 1, val);
upd(p << 1 | 1, i, (L + R >> 1) + 1, R, val);
st[p] = st[p << 1] + st[p << 1 | 1];
}
node_t query(int p, int l, int r, int L, int R) {
if (r < L || R < l) return node_t();
if (l <= L && R <= r) return st[p];
return query(p << 1, l, r, L, L + R >> 1) + query(p << 1 | 1, l, r, (L + R >> 1) + 1, R);
}

void chemthan() {
cin >> n >> q;
assert(1 <= n && n <= 1e5);
assert(1 <= q && q <= 1e5);
FOR(i, 0, n) {
int x; cin >> x;
assert(-1e6 <= x && x <= +1e6);
upd(1, i, 0, n - 1, x);
}
while (q--) {
int l, r; cin >> l >> r; l--, r--;
assert(0 <= l && l <= r && r < n);
node_t res = query(1, l, r, 0, n - 1);
swap(res.prf, res.suf);
res = query(1, 0, l - 1, 0, n - 1) + res + query(1, r + 1, n - 1, 0, n - 1);
cout << res.ans << "\n";
}
}

int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0), cin.tie(0);
if (argc > 1) {
assert(freopen(argv[1], "r", stdin));
}
if (argc > 2) {
assert(freopen(argv[2], "wb", stdout));
}
chemthan();
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}```