// #include "rect.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const ll INF = 1e18 + 69;
struct SegmentTree{
struct Node{
ll val, lazy, ma;
Node(){ma = 0; lazy = val = -INF;}
};
int n;
vector<Node> a;
void build_tree(int l, int r, int id, vector<int> &b){
if (l == r){a[id].val = b[l] - INF; a[id].ma = b[l]; return;}
int mid = (l + r) >> 1;
build_tree(l, mid, id * 2, b);
build_tree(mid + 1, r, id * 2 + 1, b);
a[id].ma = max(a[id * 2].ma, a[id * 2 + 1].ma);
a[id].val = max(a[id * 2].val, a[id * 2 + 1].val);
}
SegmentTree(int _n, vector<int> b){
n = _n;
a.resize(n*4+4);
build_tree(1, n, 1, b);
}
void update_node(int id, ll val){
maximize(a[id].lazy, val);
maximize(a[id].val, a[id].ma + val);
}
void down(int id){
update_node(id * 2, a[id].lazy);
update_node(id * 2 + 1, a[id].lazy);
}
void update(int u, int v, ll val, int l, int r, int id){
if (u <= l && r <= v){
update_node(id, val);
return;
}
down(id);
int mid = (l + r) >> 1;
if (u <= mid) update(u, v, val, l, mid, id * 2);
if (v > mid) update(u, v, val, mid + 1, r, id * 2 + 1);
a[id].val = max(a[id * 2].val, a[id * 2 + 1].val);
}
void update(int u, int v, ll val){
update(u, v, val, 1, n, 1);
}
ll get(int u, int v, int l, int r, int id){
if (u <= l && r <= v) return a[id].val;
down(id);
int mid = (l + r) >> 1;
ll ans = -INF;
if (u <= mid) maximize(ans, get(u, v, l, mid, id * 2));
if (v > mid) maximize(ans, get(u, v, mid + 1, r, id * 2 + 1));
return ans;
}
ll get(int u, int v){
return get(u, v, 1, n, 1);
}
};
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
int n; cin >> n;
vector<int> a(n+1);
for(int i = 1; i<=n; ++i) cin >> a[i];
SegmentTree segg(n, a);
vector<vector<int>> sigma(n+1);
vector<int> st;
for(int i = n; i>=1; --i){
while(st.size() && a[st.back()] <= a[i]){
sigma[i].push_back(st.back());
st.pop_back();
}
if (st.size()) sigma[i].push_back(st.back());
st.push_back(i);
}
int q; cin >> q;
vector<vector<pair<int, int>>> queries(n+1);
vector<ll> ans(q);
for(int i = 0; i<q; ++i){
int l, r; cin >> l >> r;
queries[l].push_back({r, i});
}
for(int i = n; i>=1; --i){
for(int j: sigma[i]){
int k = j + (j - i);
if (k > n) continue;
segg.update(k, n, a[i] + a[j]);
}
for(pair<int, int> j: queries[i]){
ans[j.second] = segg.get(1, j.first);
}
}
printArr(ans, "\n", "");
cerr << "Time elapsed: " << clock() - start << " ms\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
139 ms |
25684 KB |
Output is correct |
12 |
Correct |
144 ms |
25628 KB |
Output is correct |
13 |
Correct |
149 ms |
25832 KB |
Output is correct |
14 |
Correct |
136 ms |
25680 KB |
Output is correct |
15 |
Correct |
138 ms |
25852 KB |
Output is correct |
16 |
Correct |
141 ms |
25168 KB |
Output is correct |
17 |
Correct |
157 ms |
24908 KB |
Output is correct |
18 |
Correct |
157 ms |
24912 KB |
Output is correct |
19 |
Correct |
135 ms |
25680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
37440 KB |
Output is correct |
2 |
Correct |
63 ms |
37956 KB |
Output is correct |
3 |
Correct |
69 ms |
37424 KB |
Output is correct |
4 |
Correct |
106 ms |
37436 KB |
Output is correct |
5 |
Correct |
106 ms |
37444 KB |
Output is correct |
6 |
Correct |
114 ms |
36928 KB |
Output is correct |
7 |
Correct |
109 ms |
36680 KB |
Output is correct |
8 |
Correct |
104 ms |
36888 KB |
Output is correct |
9 |
Correct |
106 ms |
37184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
139 ms |
25684 KB |
Output is correct |
12 |
Correct |
144 ms |
25628 KB |
Output is correct |
13 |
Correct |
149 ms |
25832 KB |
Output is correct |
14 |
Correct |
136 ms |
25680 KB |
Output is correct |
15 |
Correct |
138 ms |
25852 KB |
Output is correct |
16 |
Correct |
141 ms |
25168 KB |
Output is correct |
17 |
Correct |
157 ms |
24908 KB |
Output is correct |
18 |
Correct |
157 ms |
24912 KB |
Output is correct |
19 |
Correct |
135 ms |
25680 KB |
Output is correct |
20 |
Correct |
115 ms |
37440 KB |
Output is correct |
21 |
Correct |
63 ms |
37956 KB |
Output is correct |
22 |
Correct |
69 ms |
37424 KB |
Output is correct |
23 |
Correct |
106 ms |
37436 KB |
Output is correct |
24 |
Correct |
106 ms |
37444 KB |
Output is correct |
25 |
Correct |
114 ms |
36928 KB |
Output is correct |
26 |
Correct |
109 ms |
36680 KB |
Output is correct |
27 |
Correct |
104 ms |
36888 KB |
Output is correct |
28 |
Correct |
106 ms |
37184 KB |
Output is correct |
29 |
Correct |
619 ms |
122924 KB |
Output is correct |
30 |
Correct |
479 ms |
123972 KB |
Output is correct |
31 |
Correct |
523 ms |
122076 KB |
Output is correct |
32 |
Correct |
679 ms |
122544 KB |
Output is correct |
33 |
Correct |
604 ms |
122564 KB |
Output is correct |
34 |
Correct |
595 ms |
120496 KB |
Output is correct |
35 |
Correct |
590 ms |
119984 KB |
Output is correct |
36 |
Correct |
627 ms |
119992 KB |
Output is correct |
37 |
Correct |
591 ms |
121516 KB |
Output is correct |
38 |
Correct |
450 ms |
128240 KB |
Output is correct |
39 |
Correct |
430 ms |
128432 KB |
Output is correct |
40 |
Correct |
436 ms |
124980 KB |
Output is correct |
41 |
Correct |
443 ms |
124592 KB |
Output is correct |
42 |
Correct |
428 ms |
124592 KB |
Output is correct |
43 |
Correct |
433 ms |
126380 KB |
Output is correct |
44 |
Correct |
481 ms |
127664 KB |
Output is correct |
45 |
Correct |
453 ms |
127688 KB |
Output is correct |
46 |
Correct |
446 ms |
124588 KB |
Output is correct |
47 |
Correct |
441 ms |
124276 KB |
Output is correct |
48 |
Correct |
446 ms |
124076 KB |
Output is correct |
49 |
Correct |
434 ms |
126348 KB |
Output is correct |
50 |
Correct |
488 ms |
125920 KB |
Output is correct |
51 |
Correct |
491 ms |
125868 KB |
Output is correct |
52 |
Correct |
495 ms |
123352 KB |
Output is correct |
53 |
Correct |
486 ms |
123052 KB |
Output is correct |
54 |
Correct |
478 ms |
122944 KB |
Output is correct |
55 |
Correct |
488 ms |
124640 KB |
Output is correct |