//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 5e5;
int a[N + 1], res[N + 1], n, q;
vector<pair<int, int> > query[N + 1];
struct SegmentTree
{
int add[4 * N + 1], mx[4 * N + 1], lazy[4 * N + 1];
void buildtree(int v, int TreeL, int TreeR)
{
add[v] = lazy[v] = 0;
if (TreeL == TreeR)
{
mx[v] = a[TreeL];
return ;
}
int mid = (TreeL + TreeR)/2;
buildtree(v * 2, TreeL, mid);
buildtree(v * 2 + 1, mid + 1, TreeR);
mx[v] = max(mx[2 * v], mx[2 * v + 1]);
}
void down(int v)
{
add[2 * v] = max(add[2 * v], lazy[v] + mx[2 * v]);
add[2 * v + 1] = max(add[2 * v + 1], lazy[v] + mx[2 * v + 1]);
lazy[2 * v] = max(lazy[2 * v], lazy[v]);
lazy[2 * v + 1] = max(lazy[2 * v + 1], lazy[v]);
}
void upd(int v, int TreeL, int TreeR, int L, int R, int val)
{
//cerr << v << '\n';
if (L > R)
{
return ;
}
if (TreeL == L && TreeR == R)
{
lazy[v] = max(lazy[v], val);
add[v] = max(add[v], lazy[v] + mx[v]);
return ;
}
int mid = (TreeL + TreeR)/2;
down(v);
upd(v * 2, TreeL, mid, L, min(R, mid), val);
upd(v * 2 + 1, mid + 1, TreeR, max(L, mid + 1), R, val);
add[v] = max(add[2 * v], add[2 * v + 1]);
}
int get(int v, int TreeL, int TreeR, int L, int R)
{
if (L > R)
{
return 0;
}
if (TreeL == L && TreeR == R)
{
return max(add[v], lazy[v] + mx[v]);
}
down(v);
int mid = (TreeL + TreeR)/2;
return max(get(v * 2, TreeL, mid, L, min(R, mid)), get(v * 2 + 1, mid + 1, TreeR, max(L, mid + 1), R));
}
} tree;
void read()
{
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;
query[l].emplace_back(r, i);
}
tree.buildtree(1, 1, n);
}
void solve()
{
stack<int> st;
for (int l = n; l >= 1; -- l)
{
while(!st.empty() && a[l] >= a[st.top()])
{
tree.upd(1, 1, n, 2 * st.top() - l, n, a[l] + a[st.top()]);
//cerr << l << ' ' << st.top() << '\n';
st.pop();
}
if (!st.empty())
{
//cerr << l << ' ' << st.top() << '\n';
tree.upd(1, 1, n, 2 * st.top() - l, n, a[l] + a[st.top()]);
}
st.push(l);
for (auto &[r, i] : query[l])
{
res[i] = tree.get(1, 1, n, l, r);
}
}
for (int i = 1; i <= q; ++ i)
{
cout << res[i] << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
//freopen(TASK".OUT", "w", stdout);
}
int t = 1;
bool typetest = false;
if (typetest)
{
cin >> t;
}
for (int __ = 1; __ <= t; ++ __)
{
//cout << "Case " << __ << ": ";
read();
solve();
}
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12104 KB |
Output is correct |
2 |
Correct |
7 ms |
12048 KB |
Output is correct |
3 |
Correct |
7 ms |
12080 KB |
Output is correct |
4 |
Correct |
10 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12088 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
11988 KB |
Output is correct |
10 |
Correct |
6 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12104 KB |
Output is correct |
2 |
Correct |
7 ms |
12048 KB |
Output is correct |
3 |
Correct |
7 ms |
12080 KB |
Output is correct |
4 |
Correct |
10 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12088 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
11988 KB |
Output is correct |
10 |
Correct |
6 ms |
11988 KB |
Output is correct |
11 |
Correct |
272 ms |
28172 KB |
Output is correct |
12 |
Correct |
357 ms |
28192 KB |
Output is correct |
13 |
Correct |
261 ms |
28252 KB |
Output is correct |
14 |
Correct |
237 ms |
28256 KB |
Output is correct |
15 |
Correct |
318 ms |
28364 KB |
Output is correct |
16 |
Correct |
235 ms |
27724 KB |
Output is correct |
17 |
Correct |
246 ms |
27728 KB |
Output is correct |
18 |
Correct |
246 ms |
27576 KB |
Output is correct |
19 |
Correct |
299 ms |
28204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
19280 KB |
Output is correct |
2 |
Correct |
67 ms |
20104 KB |
Output is correct |
3 |
Correct |
64 ms |
19264 KB |
Output is correct |
4 |
Correct |
108 ms |
19276 KB |
Output is correct |
5 |
Correct |
107 ms |
19280 KB |
Output is correct |
6 |
Correct |
119 ms |
19276 KB |
Output is correct |
7 |
Correct |
109 ms |
19284 KB |
Output is correct |
8 |
Correct |
107 ms |
19280 KB |
Output is correct |
9 |
Correct |
106 ms |
19276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12104 KB |
Output is correct |
2 |
Correct |
7 ms |
12048 KB |
Output is correct |
3 |
Correct |
7 ms |
12080 KB |
Output is correct |
4 |
Correct |
10 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12088 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
11988 KB |
Output is correct |
10 |
Correct |
6 ms |
11988 KB |
Output is correct |
11 |
Correct |
272 ms |
28172 KB |
Output is correct |
12 |
Correct |
357 ms |
28192 KB |
Output is correct |
13 |
Correct |
261 ms |
28252 KB |
Output is correct |
14 |
Correct |
237 ms |
28256 KB |
Output is correct |
15 |
Correct |
318 ms |
28364 KB |
Output is correct |
16 |
Correct |
235 ms |
27724 KB |
Output is correct |
17 |
Correct |
246 ms |
27728 KB |
Output is correct |
18 |
Correct |
246 ms |
27576 KB |
Output is correct |
19 |
Correct |
299 ms |
28204 KB |
Output is correct |
20 |
Correct |
106 ms |
19280 KB |
Output is correct |
21 |
Correct |
67 ms |
20104 KB |
Output is correct |
22 |
Correct |
64 ms |
19264 KB |
Output is correct |
23 |
Correct |
108 ms |
19276 KB |
Output is correct |
24 |
Correct |
107 ms |
19280 KB |
Output is correct |
25 |
Correct |
119 ms |
19276 KB |
Output is correct |
26 |
Correct |
109 ms |
19284 KB |
Output is correct |
27 |
Correct |
107 ms |
19280 KB |
Output is correct |
28 |
Correct |
106 ms |
19276 KB |
Output is correct |
29 |
Correct |
854 ms |
45164 KB |
Output is correct |
30 |
Correct |
696 ms |
56108 KB |
Output is correct |
31 |
Correct |
703 ms |
53964 KB |
Output is correct |
32 |
Correct |
846 ms |
54084 KB |
Output is correct |
33 |
Correct |
818 ms |
54096 KB |
Output is correct |
34 |
Correct |
843 ms |
51952 KB |
Output is correct |
35 |
Correct |
775 ms |
51656 KB |
Output is correct |
36 |
Correct |
780 ms |
51516 KB |
Output is correct |
37 |
Correct |
823 ms |
52988 KB |
Output is correct |
38 |
Correct |
567 ms |
59724 KB |
Output is correct |
39 |
Correct |
548 ms |
59760 KB |
Output is correct |
40 |
Correct |
545 ms |
56528 KB |
Output is correct |
41 |
Correct |
528 ms |
55936 KB |
Output is correct |
42 |
Correct |
546 ms |
56036 KB |
Output is correct |
43 |
Correct |
555 ms |
57740 KB |
Output is correct |
44 |
Correct |
590 ms |
59140 KB |
Output is correct |
45 |
Correct |
587 ms |
59228 KB |
Output is correct |
46 |
Correct |
573 ms |
56076 KB |
Output is correct |
47 |
Correct |
562 ms |
55652 KB |
Output is correct |
48 |
Correct |
607 ms |
55756 KB |
Output is correct |
49 |
Correct |
563 ms |
57804 KB |
Output is correct |
50 |
Correct |
653 ms |
57268 KB |
Output is correct |
51 |
Correct |
652 ms |
55792 KB |
Output is correct |
52 |
Correct |
637 ms |
54980 KB |
Output is correct |
53 |
Correct |
634 ms |
54512 KB |
Output is correct |
54 |
Correct |
678 ms |
54480 KB |
Output is correct |
55 |
Correct |
634 ms |
55952 KB |
Output is correct |