#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
struct Node{
int v = 0, tag = 0;
};
int n;
vector<Node> st;
#define lc 2 * id + 1
#define rc 2 * id + 2
void addtag(int v, int id){
st[id].v += v;
st[id].tag += v;
}
void push(int id){
addtag(st[id].tag, lc);
addtag(st[id].tag, rc);
st[id].tag = 0;
}
void pull(int id){
st[id].v = max(st[lc].v, st[rc].v);
}
void modify(int l, int r, int v, int L = 1, int R = n, int id = 0){
if(l <= L && R <= r){
addtag(v, id);
return;
}
//cerr << l << " " << r << " " << L << " " << R << "\n";
push(id);
int M = (L + R) / 2;
if(r <= M) modify(l, r, v, L, M, lc);
else if(l > M) modify(l, r, v, M + 1, R, rc);
else{
modify(l, r, v, L, M, lc);
modify(l, r, v, M + 1, R, rc);
}
pull(id);
}
int query(int l, int r, int L = 1, int R = n, int id = 0){
if(l <= L && R <= r){
return st[id].v;
}
push(id);
int M = (L + R) / 2;
if(r <= M) return query(l, r, L, M, lc);
else if(l > M) return query(l, r, M + 1, R, rc);
else return max(query(l, r, L, M, lc), query(l, r, M + 1, R, rc));
}
int main(){
StarBurstStream
cin >> n;
vector<int> a(n + 1);
vector<vector<pii>> ev(n + 1);
deque<int> dq;
st.resize(4 * n);
auto add = [&](int i, int j){
int len = j - i;
int k = i + 2 * len;
ev[i].eb(mp(k, a[i] + a[j]));
};
for(int i = 1; i <= n; i++){
cin >> a[i];
modify(i, i, a[i]);
while(!dq.empty() && a[dq.back()] < a[i]){
add(dq.back(), i);
dq.pob;
}
if(!dq.empty()) add(dq.back(), i);
dq.eb(i);
}
vector<vector<pii>> qry(n + 1);
int q;
cin >> q;
for(int i = 0; i < q; i++){
int l, r;
cin >> l >> r;
qry[l].eb(mp(r, i));
}
map<int, int> mx;
vector<int> ans(q);
mx[1] = 0;
auto upd = [&](int p, int v){
if(p > n) return;
//cerr << "upd " << p << " " << v << "\n";
auto it = mx.upper_bound(p);
it--;
if(v <= it->S) return;
int lp = max(p, it->F);
int nxt = next(it) == mx.end() ? n + 1 : next(it)->F;
modify(lp, nxt - 1, v - it->S);
mx[p] = v;
it = mx.upper_bound(p);
while(it != mx.end() && it->S <= v){
nxt = next(it) == mx.end() ? n + 1 : next(it)->F;
modify(it->F, nxt - 1, v - it->S);
mx[it->F] = v;
it++;
}
//printv(mx, cerr);
};
for(int i = n; i >= 1; i--){
for(pii t : ev[i]){
upd(t.F, t.S);
}
for(pii t : qry[i]){
int r, id;
tie(r, id) = t;
ans[id] = query(i, r);
}
}
for(int i : ans) cout << i << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
233 ms |
19540 KB |
Output is correct |
12 |
Correct |
226 ms |
19348 KB |
Output is correct |
13 |
Correct |
2569 ms |
19408 KB |
Output is correct |
14 |
Correct |
250 ms |
19508 KB |
Output is correct |
15 |
Correct |
254 ms |
19540 KB |
Output is correct |
16 |
Correct |
295 ms |
18820 KB |
Output is correct |
17 |
Correct |
255 ms |
18796 KB |
Output is correct |
18 |
Correct |
258 ms |
18708 KB |
Output is correct |
19 |
Correct |
319 ms |
19368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1834 ms |
35352 KB |
Output is correct |
2 |
Correct |
221 ms |
34092 KB |
Output is correct |
3 |
Execution timed out |
4034 ms |
26148 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
233 ms |
19540 KB |
Output is correct |
12 |
Correct |
226 ms |
19348 KB |
Output is correct |
13 |
Correct |
2569 ms |
19408 KB |
Output is correct |
14 |
Correct |
250 ms |
19508 KB |
Output is correct |
15 |
Correct |
254 ms |
19540 KB |
Output is correct |
16 |
Correct |
295 ms |
18820 KB |
Output is correct |
17 |
Correct |
255 ms |
18796 KB |
Output is correct |
18 |
Correct |
258 ms |
18708 KB |
Output is correct |
19 |
Correct |
319 ms |
19368 KB |
Output is correct |
20 |
Correct |
1834 ms |
35352 KB |
Output is correct |
21 |
Correct |
221 ms |
34092 KB |
Output is correct |
22 |
Execution timed out |
4034 ms |
26148 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |