#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 |
14 ms |
23896 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
11 ms |
23896 KB |
Output is correct |
5 |
Correct |
9 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23896 KB |
Output is correct |
8 |
Correct |
9 ms |
23896 KB |
Output is correct |
9 |
Correct |
10 ms |
23816 KB |
Output is correct |
10 |
Correct |
15 ms |
23968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23896 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
11 ms |
23896 KB |
Output is correct |
5 |
Correct |
9 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23896 KB |
Output is correct |
8 |
Correct |
9 ms |
23896 KB |
Output is correct |
9 |
Correct |
10 ms |
23816 KB |
Output is correct |
10 |
Correct |
15 ms |
23968 KB |
Output is correct |
11 |
Correct |
170 ms |
46828 KB |
Output is correct |
12 |
Correct |
185 ms |
46692 KB |
Output is correct |
13 |
Correct |
177 ms |
46752 KB |
Output is correct |
14 |
Correct |
170 ms |
46676 KB |
Output is correct |
15 |
Correct |
179 ms |
46928 KB |
Output is correct |
16 |
Correct |
171 ms |
46184 KB |
Output is correct |
17 |
Correct |
179 ms |
46240 KB |
Output is correct |
18 |
Correct |
198 ms |
46040 KB |
Output is correct |
19 |
Correct |
205 ms |
46672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
47444 KB |
Output is correct |
2 |
Correct |
73 ms |
47704 KB |
Output is correct |
3 |
Correct |
70 ms |
46160 KB |
Output is correct |
4 |
Correct |
116 ms |
47484 KB |
Output is correct |
5 |
Correct |
115 ms |
47440 KB |
Output is correct |
6 |
Correct |
109 ms |
47444 KB |
Output is correct |
7 |
Correct |
111 ms |
47448 KB |
Output is correct |
8 |
Correct |
112 ms |
47328 KB |
Output is correct |
9 |
Correct |
117 ms |
47444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23896 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
11 ms |
23896 KB |
Output is correct |
5 |
Correct |
9 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23896 KB |
Output is correct |
8 |
Correct |
9 ms |
23896 KB |
Output is correct |
9 |
Correct |
10 ms |
23816 KB |
Output is correct |
10 |
Correct |
15 ms |
23968 KB |
Output is correct |
11 |
Correct |
170 ms |
46828 KB |
Output is correct |
12 |
Correct |
185 ms |
46692 KB |
Output is correct |
13 |
Correct |
177 ms |
46752 KB |
Output is correct |
14 |
Correct |
170 ms |
46676 KB |
Output is correct |
15 |
Correct |
179 ms |
46928 KB |
Output is correct |
16 |
Correct |
171 ms |
46184 KB |
Output is correct |
17 |
Correct |
179 ms |
46240 KB |
Output is correct |
18 |
Correct |
198 ms |
46040 KB |
Output is correct |
19 |
Correct |
205 ms |
46672 KB |
Output is correct |
20 |
Correct |
113 ms |
47444 KB |
Output is correct |
21 |
Correct |
73 ms |
47704 KB |
Output is correct |
22 |
Correct |
70 ms |
46160 KB |
Output is correct |
23 |
Correct |
116 ms |
47484 KB |
Output is correct |
24 |
Correct |
115 ms |
47440 KB |
Output is correct |
25 |
Correct |
109 ms |
47444 KB |
Output is correct |
26 |
Correct |
111 ms |
47448 KB |
Output is correct |
27 |
Correct |
112 ms |
47328 KB |
Output is correct |
28 |
Correct |
117 ms |
47444 KB |
Output is correct |
29 |
Correct |
663 ms |
100432 KB |
Output is correct |
30 |
Correct |
519 ms |
101456 KB |
Output is correct |
31 |
Correct |
561 ms |
97364 KB |
Output is correct |
32 |
Correct |
677 ms |
100436 KB |
Output is correct |
33 |
Correct |
650 ms |
100504 KB |
Output is correct |
34 |
Correct |
626 ms |
100176 KB |
Output is correct |
35 |
Correct |
657 ms |
99924 KB |
Output is correct |
36 |
Correct |
690 ms |
99924 KB |
Output is correct |
37 |
Correct |
659 ms |
100472 KB |
Output is correct |
38 |
Correct |
517 ms |
103028 KB |
Output is correct |
39 |
Correct |
491 ms |
103252 KB |
Output is correct |
40 |
Correct |
483 ms |
101748 KB |
Output is correct |
41 |
Correct |
482 ms |
101196 KB |
Output is correct |
42 |
Correct |
472 ms |
101204 KB |
Output is correct |
43 |
Correct |
485 ms |
102184 KB |
Output is correct |
44 |
Correct |
505 ms |
103508 KB |
Output is correct |
45 |
Correct |
513 ms |
103288 KB |
Output is correct |
46 |
Correct |
483 ms |
101820 KB |
Output is correct |
47 |
Correct |
504 ms |
101968 KB |
Output is correct |
48 |
Correct |
510 ms |
101968 KB |
Output is correct |
49 |
Correct |
503 ms |
103000 KB |
Output is correct |
50 |
Correct |
541 ms |
103504 KB |
Output is correct |
51 |
Correct |
551 ms |
103504 KB |
Output is correct |
52 |
Correct |
534 ms |
102744 KB |
Output is correct |
53 |
Correct |
542 ms |
102976 KB |
Output is correct |
54 |
Correct |
526 ms |
102736 KB |
Output is correct |
55 |
Correct |
561 ms |
103512 KB |
Output is correct |