#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX 500005
int nArr, numQuery;
int A[MAX];
int ans[MAX];
vector<int> candidates[MAX];
vector<pair<int, int>> Q[MAX];
struct Node{
int a, f;
Node(){}
Node(int _a, int _f): a(_a), f(_f){}
friend Node operator + (const Node& a, const Node& b){
Node res;
res.a = max(a.a, b.a);
res.f = max(a.f, b.f);
return res;
}
};
struct SegmentTree{
vector<int> lz;
vector<Node> st;
int n;
SegmentTree(int _n = 0){
this -> n = _n;
lz.resize((n << 2) + 5, 0);
st.resize((n << 2) + 5);
}
#define lson (nd << 1)
#define rson (nd << 1 | 1)
void build(int nd, int l, int r){
if(l == r){
st[nd].f = st[nd].a = A[l];
return;
}
int m = (l + r) >> 1;
build(lson, l, m);
build(rson, m + 1, r);
st[nd] = st[lson] + st[rson];
}
void pushDown(int nd){
maximize(lz[lson], lz[nd]);
maximize(lz[rson], lz[nd]);
maximize(st[lson].f, st[lson].a + lz[nd]);
maximize(st[rson].f, st[rson].a + lz[nd]);
return;
}
void upd(int nd, int l, int r, int u, int v, int val){
if (u > r || v < l) return;
if (u <= l && v >= r){
maximize(st[nd].f, st[nd].a + val);
maximize(lz[nd], val);
return;
}
int m = (l + r) >> 1;
pushDown(nd);
upd(lson, l, m, u, v, val);
upd(rson, m + 1, r, u, v, val);
st[nd] = st[lson] + st[rson];
}
int query(int nd, int l, int r, int u, int v){
if (u > r || v < l) return 0;
if (u <= l && v >= r) return st[nd].f;
int m = (l + r) >> 1;
pushDown(nd);
int ql = query(lson, l, m, u, v);
int qr = query(rson, m + 1, r, u, v);
return max(ql, qr);
}
void upd(int l, int r, int v){
upd(1, 1, n, l, r, v);
}
int query(int l, int r){
return query(1, 1, n, l, r);
}
};
void process(void){
cin >> nArr;
for (int i = 1; i <= nArr; ++i) cin >> A[i];
cin >> numQuery;
stack<int> st;
for (int i = nArr; i >= 1; --i){
while(st.size()){
candidates[i].emplace_back(st.top());
if(A[st.top()] > A[i]) break;
st.pop();
}
st.push(i);
}
SegmentTree myit(nArr);
myit.build(1, 1, nArr);
for (int i = 1; i <= numQuery; ++i){
int l, r; cin >> l >> r;
Q[l].emplace_back(r, i);
}
for (int l = nArr; l >= 1; --l){
for (int &r : candidates[l]){
if (2 * r - l <= nArr){
assert(2 * r - l > r);
myit.upd(2 * r - l, nArr, A[l] + A[r]);
}
}
for (pair<int, int>&x : Q[l]){
ans[x.second] = myit.query(l, x.first);
}
}
for (int i = 1; i <= numQuery; ++i){
cout << ans[i] << "\n";
}
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23952 KB |
Output is correct |
4 |
Correct |
11 ms |
23876 KB |
Output is correct |
5 |
Correct |
10 ms |
23980 KB |
Output is correct |
6 |
Correct |
9 ms |
23980 KB |
Output is correct |
7 |
Correct |
11 ms |
23848 KB |
Output is correct |
8 |
Correct |
11 ms |
23900 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23952 KB |
Output is correct |
4 |
Correct |
11 ms |
23876 KB |
Output is correct |
5 |
Correct |
10 ms |
23980 KB |
Output is correct |
6 |
Correct |
9 ms |
23980 KB |
Output is correct |
7 |
Correct |
11 ms |
23848 KB |
Output is correct |
8 |
Correct |
11 ms |
23900 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23840 KB |
Output is correct |
11 |
Correct |
188 ms |
51628 KB |
Output is correct |
12 |
Correct |
175 ms |
51440 KB |
Output is correct |
13 |
Correct |
181 ms |
51536 KB |
Output is correct |
14 |
Correct |
221 ms |
51432 KB |
Output is correct |
15 |
Correct |
186 ms |
51540 KB |
Output is correct |
16 |
Correct |
186 ms |
51028 KB |
Output is correct |
17 |
Correct |
171 ms |
51028 KB |
Output is correct |
18 |
Correct |
172 ms |
50772 KB |
Output is correct |
19 |
Correct |
176 ms |
51540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
48976 KB |
Output is correct |
2 |
Correct |
74 ms |
49540 KB |
Output is correct |
3 |
Correct |
74 ms |
47956 KB |
Output is correct |
4 |
Correct |
118 ms |
49232 KB |
Output is correct |
5 |
Correct |
119 ms |
49244 KB |
Output is correct |
6 |
Correct |
114 ms |
48464 KB |
Output is correct |
7 |
Correct |
113 ms |
48216 KB |
Output is correct |
8 |
Correct |
112 ms |
48464 KB |
Output is correct |
9 |
Correct |
114 ms |
48724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23952 KB |
Output is correct |
4 |
Correct |
11 ms |
23876 KB |
Output is correct |
5 |
Correct |
10 ms |
23980 KB |
Output is correct |
6 |
Correct |
9 ms |
23980 KB |
Output is correct |
7 |
Correct |
11 ms |
23848 KB |
Output is correct |
8 |
Correct |
11 ms |
23900 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23840 KB |
Output is correct |
11 |
Correct |
188 ms |
51628 KB |
Output is correct |
12 |
Correct |
175 ms |
51440 KB |
Output is correct |
13 |
Correct |
181 ms |
51536 KB |
Output is correct |
14 |
Correct |
221 ms |
51432 KB |
Output is correct |
15 |
Correct |
186 ms |
51540 KB |
Output is correct |
16 |
Correct |
186 ms |
51028 KB |
Output is correct |
17 |
Correct |
171 ms |
51028 KB |
Output is correct |
18 |
Correct |
172 ms |
50772 KB |
Output is correct |
19 |
Correct |
176 ms |
51540 KB |
Output is correct |
20 |
Correct |
120 ms |
48976 KB |
Output is correct |
21 |
Correct |
74 ms |
49540 KB |
Output is correct |
22 |
Correct |
74 ms |
47956 KB |
Output is correct |
23 |
Correct |
118 ms |
49232 KB |
Output is correct |
24 |
Correct |
119 ms |
49244 KB |
Output is correct |
25 |
Correct |
114 ms |
48464 KB |
Output is correct |
26 |
Correct |
113 ms |
48216 KB |
Output is correct |
27 |
Correct |
112 ms |
48464 KB |
Output is correct |
28 |
Correct |
114 ms |
48724 KB |
Output is correct |
29 |
Correct |
700 ms |
111500 KB |
Output is correct |
30 |
Correct |
562 ms |
112720 KB |
Output is correct |
31 |
Correct |
549 ms |
108368 KB |
Output is correct |
32 |
Correct |
680 ms |
111444 KB |
Output is correct |
33 |
Correct |
679 ms |
111440 KB |
Output is correct |
34 |
Correct |
657 ms |
109136 KB |
Output is correct |
35 |
Correct |
663 ms |
108964 KB |
Output is correct |
36 |
Correct |
672 ms |
108884 KB |
Output is correct |
37 |
Correct |
632 ms |
110432 KB |
Output is correct |
38 |
Correct |
511 ms |
114256 KB |
Output is correct |
39 |
Correct |
520 ms |
114232 KB |
Output is correct |
40 |
Correct |
496 ms |
110928 KB |
Output is correct |
41 |
Correct |
482 ms |
110312 KB |
Output is correct |
42 |
Correct |
477 ms |
110416 KB |
Output is correct |
43 |
Correct |
522 ms |
112028 KB |
Output is correct |
44 |
Correct |
548 ms |
114512 KB |
Output is correct |
45 |
Correct |
517 ms |
114512 KB |
Output is correct |
46 |
Correct |
489 ms |
111188 KB |
Output is correct |
47 |
Correct |
531 ms |
110976 KB |
Output is correct |
48 |
Correct |
563 ms |
110936 KB |
Output is correct |
49 |
Correct |
539 ms |
112928 KB |
Output is correct |
50 |
Correct |
609 ms |
114800 KB |
Output is correct |
51 |
Correct |
640 ms |
114692 KB |
Output is correct |
52 |
Correct |
624 ms |
112096 KB |
Output is correct |
53 |
Correct |
603 ms |
111956 KB |
Output is correct |
54 |
Correct |
608 ms |
111756 KB |
Output is correct |
55 |
Correct |
658 ms |
113440 KB |
Output is correct |