/* I can do this all day */
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define Mp make_pair
#define point complex
#define endl '\n'
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file_io freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);
#define mashtali return cout << "Hello, World!", 0;
const int N = 1e6 + 10;
const int LOG = 20;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
const double pi = acos(-1);
const ld eps = 1e-18;
const ld one = 1.;
ll pw(ll a, ll b, ll M, ll ret = 1) { if(a == 0) return 0; a %= M; while(b) { ret = (b & 1? ret * a % M : ret), a = a * a % M, b >>= 1; } return ret % M; }
mt19937 rng(time(nullptr));
int n, q, A[N];
vector < int > vec[N];
vector < pii > Q[N];
ll Ans[N], Mn[N << 2], Mx[N << 2], seg[N << 2], lz[N << 2];
void build(int v = 1, int tl = 1, int tr = n)
{
if(tl == tr)
{
seg[v] = A[tl];
return;
}
int mid = (tl + tr) >> 1;
build(v << 1, tl, mid), build(v << 1 | 1, mid + 1, tr);
seg[v] = max(seg[v << 1], seg[v << 1 | 1]);
}
void Push(int v, ll x)
{
Mn[v] += x;
Mx[v] += x;
lz[v] += x;
seg[v] += x;
}
void S(int v, int tl, int tr)
{
if(tl == tr) return;
Push(v << 1, lz[v]);
Push(v << 1 | 1, lz[v]);
lz[v] = 0;
}
void upd(int l, int r, ll x, int v = 1, int tl = 1, int tr = n)
{
S(v, tl, tr);
if(l > tr || r < tl || l > r || Mn[v] >= x) return;
if(l <= tl && tr <= r && Mn[v] == Mx[v])
{
Push(v, x - Mn[v]);
return;
}
int mid = (tl + tr) >> 1;
upd(l, r, x, v << 1, tl, mid), upd(l, r, x, v << 1 | 1, mid + 1, tr);
seg[v] = max({seg[v << 1], seg[v << 1 | 1]});
Mn[v] = min(Mn[v << 1], Mn[v << 1 | 1]);
Mx[v] = max(Mx[v << 1], Mx[v << 1 | 1]);
}
ll get(int l, int r, int v = 1, int tl = 1, int tr = n)
{
S(v, tl, tr);
if(l > tr || r < tl || l > r) return -inf;
if(l <= tl && tr <= r) return seg[v];
int mid = (tl + tr) >> 1;
return max(get(l, r, v << 1, tl, mid), get(l, r, v << 1 | 1, mid + 1, tr));
}
int main()
{
fast_io;
vector < int > S;
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> A[i];
while(SZ(S) && A[S.back()] < A[i])
{
vec[S.back()].push_back(i);
S.pop_back();
}
if(SZ(S))
{
vec[S.back()].push_back(i);
}
S.push_back(i);
}
cin >> q;
for(int i = 1; i <= q; i ++)
{
int fir, sec;
cin >> fir >> sec;
Q[fir].push_back({sec, i});
}
build();
for(int i = n; ~(i - 1); i --)
{
for(auto id : vec[i])
{
///printf("i = %d id = %d\n", i, id);
upd(id * 2 - i, n, A[i] + A[id]);
}
for(auto cu : Q[i])
{
int id = cu.S, r = cu.F;
Ans[id] = get(i + 2, r);
}
/*printf("i = %d\n", i);
for(int j = 1; j <= n; j ++)
{
for(int k = j; k <= n; k ++)
{
printf("j = %d k = %d get = %lld\n", j, k, get(j, k));
}
}
printf("\n");
*/
}
for(int i = 1; i <= q; i ++)
{
cout << Ans[i] << endl;
}
return 0;
}
/* check corner case(n = 1?), watch for negetive index or overflow */
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
47300 KB |
Output is correct |
2 |
Correct |
23 ms |
47264 KB |
Output is correct |
3 |
Correct |
24 ms |
47224 KB |
Output is correct |
4 |
Correct |
24 ms |
47308 KB |
Output is correct |
5 |
Correct |
24 ms |
47272 KB |
Output is correct |
6 |
Correct |
24 ms |
47300 KB |
Output is correct |
7 |
Correct |
24 ms |
47344 KB |
Output is correct |
8 |
Correct |
24 ms |
47336 KB |
Output is correct |
9 |
Correct |
25 ms |
47272 KB |
Output is correct |
10 |
Correct |
24 ms |
47248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
47300 KB |
Output is correct |
2 |
Correct |
23 ms |
47264 KB |
Output is correct |
3 |
Correct |
24 ms |
47224 KB |
Output is correct |
4 |
Correct |
24 ms |
47308 KB |
Output is correct |
5 |
Correct |
24 ms |
47272 KB |
Output is correct |
6 |
Correct |
24 ms |
47300 KB |
Output is correct |
7 |
Correct |
24 ms |
47344 KB |
Output is correct |
8 |
Correct |
24 ms |
47336 KB |
Output is correct |
9 |
Correct |
25 ms |
47272 KB |
Output is correct |
10 |
Correct |
24 ms |
47248 KB |
Output is correct |
11 |
Correct |
289 ms |
68648 KB |
Output is correct |
12 |
Correct |
349 ms |
68540 KB |
Output is correct |
13 |
Correct |
299 ms |
68572 KB |
Output is correct |
14 |
Correct |
305 ms |
68684 KB |
Output is correct |
15 |
Correct |
301 ms |
68628 KB |
Output is correct |
16 |
Correct |
286 ms |
67896 KB |
Output is correct |
17 |
Correct |
286 ms |
67868 KB |
Output is correct |
18 |
Correct |
287 ms |
67860 KB |
Output is correct |
19 |
Correct |
282 ms |
68416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
233 ms |
72696 KB |
Output is correct |
2 |
Correct |
123 ms |
72452 KB |
Output is correct |
3 |
Correct |
127 ms |
73248 KB |
Output is correct |
4 |
Correct |
226 ms |
72692 KB |
Output is correct |
5 |
Correct |
214 ms |
72776 KB |
Output is correct |
6 |
Correct |
214 ms |
72012 KB |
Output is correct |
7 |
Correct |
216 ms |
72068 KB |
Output is correct |
8 |
Correct |
207 ms |
71924 KB |
Output is correct |
9 |
Correct |
215 ms |
72200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
47300 KB |
Output is correct |
2 |
Correct |
23 ms |
47264 KB |
Output is correct |
3 |
Correct |
24 ms |
47224 KB |
Output is correct |
4 |
Correct |
24 ms |
47308 KB |
Output is correct |
5 |
Correct |
24 ms |
47272 KB |
Output is correct |
6 |
Correct |
24 ms |
47300 KB |
Output is correct |
7 |
Correct |
24 ms |
47344 KB |
Output is correct |
8 |
Correct |
24 ms |
47336 KB |
Output is correct |
9 |
Correct |
25 ms |
47272 KB |
Output is correct |
10 |
Correct |
24 ms |
47248 KB |
Output is correct |
11 |
Correct |
289 ms |
68648 KB |
Output is correct |
12 |
Correct |
349 ms |
68540 KB |
Output is correct |
13 |
Correct |
299 ms |
68572 KB |
Output is correct |
14 |
Correct |
305 ms |
68684 KB |
Output is correct |
15 |
Correct |
301 ms |
68628 KB |
Output is correct |
16 |
Correct |
286 ms |
67896 KB |
Output is correct |
17 |
Correct |
286 ms |
67868 KB |
Output is correct |
18 |
Correct |
287 ms |
67860 KB |
Output is correct |
19 |
Correct |
282 ms |
68416 KB |
Output is correct |
20 |
Correct |
233 ms |
72696 KB |
Output is correct |
21 |
Correct |
123 ms |
72452 KB |
Output is correct |
22 |
Correct |
127 ms |
73248 KB |
Output is correct |
23 |
Correct |
226 ms |
72692 KB |
Output is correct |
24 |
Correct |
214 ms |
72776 KB |
Output is correct |
25 |
Correct |
214 ms |
72012 KB |
Output is correct |
26 |
Correct |
216 ms |
72068 KB |
Output is correct |
27 |
Correct |
207 ms |
71924 KB |
Output is correct |
28 |
Correct |
215 ms |
72200 KB |
Output is correct |
29 |
Correct |
1265 ms |
128048 KB |
Output is correct |
30 |
Correct |
949 ms |
127644 KB |
Output is correct |
31 |
Correct |
958 ms |
129472 KB |
Output is correct |
32 |
Correct |
1184 ms |
128164 KB |
Output is correct |
33 |
Correct |
1194 ms |
128148 KB |
Output is correct |
34 |
Correct |
1146 ms |
125832 KB |
Output is correct |
35 |
Correct |
1167 ms |
125820 KB |
Output is correct |
36 |
Correct |
1199 ms |
125760 KB |
Output is correct |
37 |
Correct |
1165 ms |
127020 KB |
Output is correct |
38 |
Correct |
884 ms |
133732 KB |
Output is correct |
39 |
Correct |
853 ms |
133952 KB |
Output is correct |
40 |
Correct |
830 ms |
130512 KB |
Output is correct |
41 |
Correct |
821 ms |
129920 KB |
Output is correct |
42 |
Correct |
830 ms |
129996 KB |
Output is correct |
43 |
Correct |
866 ms |
131680 KB |
Output is correct |
44 |
Correct |
905 ms |
133152 KB |
Output is correct |
45 |
Correct |
982 ms |
133148 KB |
Output is correct |
46 |
Correct |
902 ms |
129988 KB |
Output is correct |
47 |
Correct |
887 ms |
129640 KB |
Output is correct |
48 |
Correct |
892 ms |
129628 KB |
Output is correct |
49 |
Correct |
898 ms |
131656 KB |
Output is correct |
50 |
Correct |
1028 ms |
131280 KB |
Output is correct |
51 |
Correct |
969 ms |
131272 KB |
Output is correct |
52 |
Correct |
1054 ms |
128844 KB |
Output is correct |
53 |
Correct |
1065 ms |
128496 KB |
Output is correct |
54 |
Correct |
1004 ms |
128516 KB |
Output is correct |
55 |
Correct |
974 ms |
130096 KB |
Output is correct |