//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)
{
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:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Incorrect |
7 ms |
11988 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Incorrect |
7 ms |
11988 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
20768 KB |
Output is correct |
2 |
Correct |
59 ms |
21552 KB |
Output is correct |
3 |
Correct |
55 ms |
20684 KB |
Output is correct |
4 |
Correct |
91 ms |
20680 KB |
Output is correct |
5 |
Correct |
97 ms |
20788 KB |
Output is correct |
6 |
Correct |
92 ms |
20104 KB |
Output is correct |
7 |
Correct |
82 ms |
19948 KB |
Output is correct |
8 |
Correct |
81 ms |
20004 KB |
Output is correct |
9 |
Correct |
84 ms |
20332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Incorrect |
7 ms |
11988 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |