This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
5
5 2 1 5 3
3
1 4
2 5
1 5
*/
#include<bits/stdc++.h>
using namespace std;
const int N=5e5 + 100;
const int mod=1e9 + 7;
#define int long long
const int inf=1e18;
#define pii pair<int, int>
#define f first
#define s second
#define mp make_pair
#define FOR(i, n) for(int i=1;i<=n;i++)
#define TRACE(x) cerr << #x << " = " << x << endl
//Trace prints the name of the variable and the value.
int n, arr[N], q, tree[4*N];pii range[N];
int ql, qr;
int rmax(int i, int j)
{
if(arr[i]<arr[j]) return j;return i;
}
void build(int i=1, int l=1, int r=n)
{
if(l==r)
{
tree[i]=l;return;
}
int mid=(l+r)>>1;
build(2*i, l, mid);build(2*i+1, mid+1, r);
tree[i]=rmax(tree[2*i], tree[2*i+1]);
}
void update(int i=1, int l=1, int r=n)
{
if(l>qr||r<ql) return;
if(l==r)
{
return;
}
int mid=(l+r)>>1;update(2*i, l, mid);update(2*i+1, mid+1, r);
tree[i]=rmax(tree[2*i], tree[2*i+1]);
}
int query(int i=1, int l=1, int r=n)
{
if(l>qr||r<ql) return 0;
if(l>=ql&&r<=qr) return tree[i];
int mid=(l+r)>>1;
return rmax(query(2*i, l, mid), query(2*i+1, mid+1, r));
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n;arr[0]=0;
for(int i=1;i<=n;i++) cin>>arr[i];
build();
cin>>q;
stack< pii > rvals;
vector< int > vals;vector<int> curset;
for(int i=1;i<=q;i++)
{
vals.clear();
cin>>range[i].f>>range[i].s;ql=range[i].f;qr=range[i].s;int temp=query();
while(arr[temp]!=0&&vals.size()<5)
{
vals.push_back(temp);rvals.push(mp(temp, arr[temp]));arr[temp]=0;ql=qr=temp;update();ql=range[i].f;qr=range[i].s;
temp=query();
}
sort(vals.begin(), vals.end());
int tn=vals.size();
int ans=0;
while(!rvals.empty())
{
arr[rvals.top().f]=rvals.top().s;ql=qr=rvals.top().f;update();rvals.pop();
}
for(int i=0;i<(1<<tn);i++)
{
// cout<<i<<": ";
for(int j=0;j<tn;j++)
{
if((i&(1<<j)))
{
curset.push_back(vals[j]);
}
}
if(curset.size()==3)
{
if(curset[1]-curset[0]<=curset[2]-curset[1])
{
// cout<<"OK "<<arr[curset[0]]<<" "<<arr[curset[1]]<<" "<<arr[curset[2]]<<" #";
ans=max(ans, arr[curset[1]] + arr[curset[0]] + arr[curset[2]]);
}
}
/* for(auto j:curset)
{
cout<<j<<" ";
}
cout<<endl;*/
curset.clear();
}
cout<<ans<<endl;
}
}
Compilation message (stderr)
jumps.cpp: In function 'long long int rmax(long long int, long long int)':
jumps.cpp:26:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(arr[i]<arr[j]) return j;return i;
^~
jumps.cpp:26:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(arr[i]<arr[j]) return j;return i;
^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |