This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
struct querry{
ll x, y, id;
};
ll sfunc(querry a, querry b){
if(a.x/300 != b.x/300)
return a.x < b.x;
return a.y < b.y;
}
ll n;
ll a[500009];
ll q;
querry qu[500009];
ll ans[500009];
int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n;
for(ll i = 0; i < n; ++i)
cin >> a[i];
cin >> q;
for(ll i = 0; i < q; ++i){
cin >> qu[i].x >> qu[i].y;
--qu[i].x;
--qu[i].y;
qu[i].id = i;
}
sort(qu, qu+q, sfunc);
ll l = 0, r = -1;
set<pll, greater<pll>> s;
for(ll i = 0; i < q; ++i){
while(r < qu[i].y){
++r;
s.insert({a[r], r});
}
while(l > qu[i].x){
--l;
s.insert({a[l], l});
}
while(r > qu[i].y){
s.erase({a[r], r});
--r;
}
while(l < qu[i].x){
s.erase({a[l], l});
++l;
}
vector<pll> v;
auto it = s.begin();
for(ll j = 0; j < 30 && it != s.end(); ++j){
v.pb({(*it).ss, (*it).ff});
++it;
}
sort(v.begin(), v.end());
for(ll i1 = 0; i1 < v.size(); ++i1)
for(ll i2 = i1+1; i2 < v.size(); ++i2)
for(ll i3 = i2+1; i3 < v.size(); ++i3)
if(v[i2].ff-v[i1].ff <= v[i3].ff-v[i2].ff)
ans[qu[i].id] = max(ans[qu[i].id], v[i1].ss+v[i2].ss+v[i3].ss);
}
for(ll i = 0; i < q; ++i)
cout << ans[i] << '\n';
}
Compilation message (stderr)
jumps.cpp: In function 'int main()':
jumps.cpp:69:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ll i1 = 0; i1 < v.size(); ++i1)
~~~^~~~~~~~~~
jumps.cpp:70:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ll i2 = i1+1; i2 < v.size(); ++i2)
~~~^~~~~~~~~~
jumps.cpp:71:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ll i3 = i2+1; i3 < v.size(); ++i3)
~~~^~~~~~~~~~
# | 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... |