// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
//#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<pii,int>
#define mpp make_pair
#define fi first
#define se second
const int inf=1e9;
const int mod=1e9+7;
const int maxn=500005;
const int bl=650;
const int maxs=655;
const int maxm=200005;
const int maxq=500005;
const int maxl=20;
const int maxa=250000;
const int root=3;
/*
int power(int a,int n){
int res=1;
while(n){
if(n&1) res=res*a%mod;
a=a*a%mod;n>>=1;
}
return res;
}
const int iroot=power(3,mod-2);
*/
const int base=101;
int n,q,a[maxn],ans[maxn];
vector<pii> que[maxn];
struct node{
int Max,res,lazy=-inf;
node(){};
friend node operator+(node a,node b){
node cur;
cur.Max=max(a.Max,b.Max);
cur.res=max(a.res,b.res);
return cur;
}
};
namespace Segtree{
node tree[4*maxn];
void build(int l,int r,int id){
if(l==r){
tree[id].res=-inf;
tree[id].Max=a[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,id<<1);build(mid+1,r,id<<1|1);
tree[id]=tree[id<<1]+tree[id<<1|1];
}
void getnew(int id,int val){
tree[id].lazy=max(tree[id].lazy,val);
tree[id].res=max(tree[id].res,tree[id].lazy+tree[id].Max);
}
void pushdown(int id){
if(tree[id].lazy==-inf) return;
getnew(id<<1,tree[id].lazy);
getnew(id<<1|1,tree[id].lazy);
tree[id].lazy=-inf;
}
void update(int l,int r,int id,int p,int val){
if(p<=l){getnew(id,val);return;}
pushdown(id);
int mid=(l+r)>>1;
if(p<=mid) update(l,mid,id<<1,p,val);
update(mid+1,r,id<<1|1,p,val);
tree[id]=tree[id<<1]+tree[id<<1|1];
}
int query(int l,int r,int id,int p){
if(l==r) return tree[id].res;
pushdown(id);
int mid=(l+r)>>1;
if(p<=mid) return query(l,mid,id<<1,p);
else return max(tree[id<<1].res,query(mid+1,r,id<<1|1,p));
}
}
void solve(){
cin >> n;
for(int i=1;i<=n;i++) cin >> a[i];
cin >> q;
for(int i=1;i<=q;i++){
int l,r;cin >> l >> r;
que[l].push_back({r,i});
}
vector<int> v;
Segtree::build(1,n,1);
for(int i=n;i>=1;i--){
while(!v.empty() && a[i]>=a[v.back()]){
int p=v.back();v.pop_back();
if(2*p-i<=n) Segtree::update(1,n,1,2*p-i,a[i]+a[p]);
}
if(!v.empty()){
int p=v.back();
if(2*p-i<=n) Segtree::update(1,n,1,2*p-i,a[i]+a[p]);
}
v.push_back(i);
for(auto &[r,id]:que[i]) ans[id]=Segtree::query(1,n,1,r);
}
for(int i=1;i<=q;i++) cout << ans[i] << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;//cin >> test;
while(test--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
35448 KB |
Output is correct |
2 |
Correct |
14 ms |
35460 KB |
Output is correct |
3 |
Correct |
14 ms |
35528 KB |
Output is correct |
4 |
Correct |
14 ms |
35480 KB |
Output is correct |
5 |
Correct |
17 ms |
35488 KB |
Output is correct |
6 |
Correct |
14 ms |
35540 KB |
Output is correct |
7 |
Correct |
15 ms |
35540 KB |
Output is correct |
8 |
Correct |
15 ms |
35452 KB |
Output is correct |
9 |
Correct |
14 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
35448 KB |
Output is correct |
2 |
Correct |
14 ms |
35460 KB |
Output is correct |
3 |
Correct |
14 ms |
35528 KB |
Output is correct |
4 |
Correct |
14 ms |
35480 KB |
Output is correct |
5 |
Correct |
17 ms |
35488 KB |
Output is correct |
6 |
Correct |
14 ms |
35540 KB |
Output is correct |
7 |
Correct |
15 ms |
35540 KB |
Output is correct |
8 |
Correct |
15 ms |
35452 KB |
Output is correct |
9 |
Correct |
14 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35520 KB |
Output is correct |
11 |
Correct |
215 ms |
54152 KB |
Output is correct |
12 |
Correct |
192 ms |
54152 KB |
Output is correct |
13 |
Correct |
171 ms |
54052 KB |
Output is correct |
14 |
Correct |
176 ms |
54152 KB |
Output is correct |
15 |
Correct |
176 ms |
54092 KB |
Output is correct |
16 |
Correct |
171 ms |
53440 KB |
Output is correct |
17 |
Correct |
183 ms |
53444 KB |
Output is correct |
18 |
Correct |
184 ms |
53360 KB |
Output is correct |
19 |
Correct |
174 ms |
53988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
38068 KB |
Output is correct |
2 |
Correct |
60 ms |
39236 KB |
Output is correct |
3 |
Correct |
71 ms |
38064 KB |
Output is correct |
4 |
Correct |
90 ms |
38064 KB |
Output is correct |
5 |
Correct |
92 ms |
38072 KB |
Output is correct |
6 |
Correct |
93 ms |
37408 KB |
Output is correct |
7 |
Correct |
102 ms |
37304 KB |
Output is correct |
8 |
Correct |
112 ms |
37224 KB |
Output is correct |
9 |
Correct |
92 ms |
37576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
35448 KB |
Output is correct |
2 |
Correct |
14 ms |
35460 KB |
Output is correct |
3 |
Correct |
14 ms |
35528 KB |
Output is correct |
4 |
Correct |
14 ms |
35480 KB |
Output is correct |
5 |
Correct |
17 ms |
35488 KB |
Output is correct |
6 |
Correct |
14 ms |
35540 KB |
Output is correct |
7 |
Correct |
15 ms |
35540 KB |
Output is correct |
8 |
Correct |
15 ms |
35452 KB |
Output is correct |
9 |
Correct |
14 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35520 KB |
Output is correct |
11 |
Correct |
215 ms |
54152 KB |
Output is correct |
12 |
Correct |
192 ms |
54152 KB |
Output is correct |
13 |
Correct |
171 ms |
54052 KB |
Output is correct |
14 |
Correct |
176 ms |
54152 KB |
Output is correct |
15 |
Correct |
176 ms |
54092 KB |
Output is correct |
16 |
Correct |
171 ms |
53440 KB |
Output is correct |
17 |
Correct |
183 ms |
53444 KB |
Output is correct |
18 |
Correct |
184 ms |
53360 KB |
Output is correct |
19 |
Correct |
174 ms |
53988 KB |
Output is correct |
20 |
Correct |
92 ms |
38068 KB |
Output is correct |
21 |
Correct |
60 ms |
39236 KB |
Output is correct |
22 |
Correct |
71 ms |
38064 KB |
Output is correct |
23 |
Correct |
90 ms |
38064 KB |
Output is correct |
24 |
Correct |
92 ms |
38072 KB |
Output is correct |
25 |
Correct |
93 ms |
37408 KB |
Output is correct |
26 |
Correct |
102 ms |
37304 KB |
Output is correct |
27 |
Correct |
112 ms |
37224 KB |
Output is correct |
28 |
Correct |
92 ms |
37576 KB |
Output is correct |
29 |
Correct |
685 ms |
65316 KB |
Output is correct |
30 |
Correct |
542 ms |
67264 KB |
Output is correct |
31 |
Correct |
551 ms |
65244 KB |
Output is correct |
32 |
Correct |
595 ms |
65228 KB |
Output is correct |
33 |
Correct |
582 ms |
65316 KB |
Output is correct |
34 |
Correct |
616 ms |
63036 KB |
Output is correct |
35 |
Correct |
586 ms |
62660 KB |
Output is correct |
36 |
Correct |
637 ms |
62656 KB |
Output is correct |
37 |
Correct |
626 ms |
64124 KB |
Output is correct |
38 |
Correct |
449 ms |
70988 KB |
Output is correct |
39 |
Correct |
387 ms |
70920 KB |
Output is correct |
40 |
Correct |
420 ms |
67656 KB |
Output is correct |
41 |
Correct |
437 ms |
67200 KB |
Output is correct |
42 |
Correct |
402 ms |
67148 KB |
Output is correct |
43 |
Correct |
380 ms |
68876 KB |
Output is correct |
44 |
Correct |
425 ms |
70324 KB |
Output is correct |
45 |
Correct |
410 ms |
70340 KB |
Output is correct |
46 |
Correct |
404 ms |
67236 KB |
Output is correct |
47 |
Correct |
422 ms |
66752 KB |
Output is correct |
48 |
Correct |
430 ms |
66788 KB |
Output is correct |
49 |
Correct |
412 ms |
68860 KB |
Output is correct |
50 |
Correct |
483 ms |
68524 KB |
Output is correct |
51 |
Correct |
497 ms |
68444 KB |
Output is correct |
52 |
Correct |
479 ms |
66068 KB |
Output is correct |
53 |
Correct |
484 ms |
65716 KB |
Output is correct |
54 |
Correct |
484 ms |
65620 KB |
Output is correct |
55 |
Correct |
464 ms |
67344 KB |
Output is correct |