#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d))
#define szof(x) ((int)(x).size())
#define vi vector<int>
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
const int INF = 2147483647;
const int LNF = INF*INF;
const int MOD = 1000000007;
const int mod = 998244353;
const double eps = 1e-12;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
const int MAX = 500010;
int n, q;
int arr[MAX];
int ql[MAX], qr[MAX];
vector<pii> pairs;
vector<vi> ev;
// modify : time, 2, suf. pos, val
// query : time = L, 1, R, id
int res[MAX];
int
#define V st[cidx]
#define LC st[cidx*2]
#define RC st[cidx*2+1]
struct Node{
int sl, sr;
int Max;
int change;
int tag;
bool cantag;
};
Node st[4*MAX];
void build(int cidx, int cl, int cr){
V.sl = cl;
V.sr = cr;
V.change = 0;
V.tag = 0;
V.cantag = 1;
if(cl < cr){
int mid = (cl + cr) / 2;
build(cidx*2, cl, mid);
build(cidx*2+1, mid+1, cr);
V.Max = max(LC.Max, RC.Max);
}
else{
V.Max = arr[cl];
}
}
void push(int cidx){
if(V.tag == 0) return;
V.Max += V.tag;
assert(V.cantag == 1);
V.change += V.tag;
if(V.sl < V.sr){
LC.tag += V.tag;
RC.tag += V.tag;
}
V.tag = 0;
}
void pull(int cidx){
push(cidx);
if(V.sl < V.sr){
push(cidx*2);
push(cidx*2+1);
V.Max = max(LC.Max, RC.Max);
V.change = min(LC.change, RC.change);
if(LC.change == RC.change and LC.cantag and RC.cantag) V.cantag = 1;
else V.cantag = 0;
}
}
void modify(int cidx, int ml, int mr, int val){
if(mr < V.sl or V.sr < ml) return;
pull(cidx);
if(ml <= V.sl and V.sr <= mr){
if(V.change < val and V.cantag){
V.tag += val - V.change;
return;
}
else if(val <= V.change){
return;
}
else{
modify(cidx*2, ml, mr, val);
modify(cidx*2+1, ml, mr, val);
pull(cidx);
}
}
else{
modify(cidx*2, ml, mr, val);
modify(cidx*2+1, ml, mr, val);
pull(cidx);
}
}
int query(int cidx, int ql, int qr){
if(qr < V.sl or V.sr < ql) return -INF;
pull(cidx);
if(ql <= V.sl and V.sr <= qr) return V.Max;
return max( query(cidx*2, ql, qr) , query(cidx*2 + 1, ql, qr) );
}
void debug(){
/*
cerr<<"st : "<<endl;
FOR(cidx, 1, 4*n, 1){
if(V.sl == 0) continue;
cerr<<"["<<V.sl<<", "<<V.sr<<"] ";
cerr<<"change = "<<V.change<<", tag = "<<V.tag<<endl;
}
cerr<<endl;
*/
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
FOR(i, 1, n, 1){
cin>>arr[i];
}
cin>>q;
FOR(i, 1, q, 1){
cin>>ql[i]>>qr[i];
}
vi mq;
mq.pb(n);
for(int i = n-1; i >= 1; i--){
while(!mq.empty() and arr[mq.back()] < arr[i]){
pairs.eb(i, mq.back());
mq.pop_back();
}
if(!mq.empty()) pairs.eb(i, mq.back());
mq.pb(i);
}
/*
cerr<<"pairs : "<<endl;
for(pii p : pairs){
cerr<<"("<<p.F<<", "<<p.S<<") "<<endl;
}
cerr<<endl;
*/
for(pii p : pairs){
if(2 * p.S - p.F > n) continue;
ev.pb({p.F, 2, 2 * p.S - p.F, arr[p.F] + arr[p.S]});
}
FOR(i, 1, q, 1){
ev.pb({ql[i], 1, qr[i], i});
}
sort(ev.begin(), ev.end());
reverse(ev.begin(), ev.end());
build(1, 1, n);
for(vi E : ev){
int type = E[1];
if(type == 2){ // modify
int L = E[2];
int val = E[3];
modify(1, L, n, val);
//cerr<<"modify ["<<L<<", "<<n<<"] val = "<<val<<endl;
//debug();
}
else if(type == 1){ // query
int L = E[0];
int R = E[2];
int id = E[3];
res[id] = query(1, L, R);
//cerr<<"query ["<<L<<", "<<R<<"] = "<<res[id]<<endl;
}
}
FOR(i, 1, q, 1){
cout<<res[i]<<'\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
666 ms |
54504 KB |
Output is correct |
12 |
Correct |
666 ms |
54028 KB |
Output is correct |
13 |
Correct |
753 ms |
54036 KB |
Output is correct |
14 |
Correct |
689 ms |
54584 KB |
Output is correct |
15 |
Correct |
686 ms |
54536 KB |
Output is correct |
16 |
Correct |
659 ms |
53904 KB |
Output is correct |
17 |
Correct |
650 ms |
53784 KB |
Output is correct |
18 |
Correct |
671 ms |
53852 KB |
Output is correct |
19 |
Correct |
674 ms |
54464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
398 ms |
61564 KB |
Output is correct |
2 |
Correct |
178 ms |
45844 KB |
Output is correct |
3 |
Correct |
191 ms |
45492 KB |
Output is correct |
4 |
Correct |
387 ms |
62728 KB |
Output is correct |
5 |
Correct |
377 ms |
62724 KB |
Output is correct |
6 |
Correct |
390 ms |
62008 KB |
Output is correct |
7 |
Correct |
394 ms |
61968 KB |
Output is correct |
8 |
Correct |
385 ms |
62020 KB |
Output is correct |
9 |
Correct |
432 ms |
62284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
666 ms |
54504 KB |
Output is correct |
12 |
Correct |
666 ms |
54028 KB |
Output is correct |
13 |
Correct |
753 ms |
54036 KB |
Output is correct |
14 |
Correct |
689 ms |
54584 KB |
Output is correct |
15 |
Correct |
686 ms |
54536 KB |
Output is correct |
16 |
Correct |
659 ms |
53904 KB |
Output is correct |
17 |
Correct |
650 ms |
53784 KB |
Output is correct |
18 |
Correct |
671 ms |
53852 KB |
Output is correct |
19 |
Correct |
674 ms |
54464 KB |
Output is correct |
20 |
Correct |
398 ms |
61564 KB |
Output is correct |
21 |
Correct |
178 ms |
45844 KB |
Output is correct |
22 |
Correct |
191 ms |
45492 KB |
Output is correct |
23 |
Correct |
387 ms |
62728 KB |
Output is correct |
24 |
Correct |
377 ms |
62724 KB |
Output is correct |
25 |
Correct |
390 ms |
62008 KB |
Output is correct |
26 |
Correct |
394 ms |
61968 KB |
Output is correct |
27 |
Correct |
385 ms |
62020 KB |
Output is correct |
28 |
Correct |
432 ms |
62284 KB |
Output is correct |
29 |
Correct |
2160 ms |
202516 KB |
Output is correct |
30 |
Correct |
1482 ms |
163284 KB |
Output is correct |
31 |
Correct |
1634 ms |
159448 KB |
Output is correct |
32 |
Correct |
2128 ms |
202520 KB |
Output is correct |
33 |
Correct |
2182 ms |
202528 KB |
Output is correct |
34 |
Correct |
2126 ms |
200284 KB |
Output is correct |
35 |
Correct |
2136 ms |
199888 KB |
Output is correct |
36 |
Correct |
2156 ms |
200008 KB |
Output is correct |
37 |
Correct |
2170 ms |
201328 KB |
Output is correct |
38 |
Correct |
1638 ms |
202488 KB |
Output is correct |
39 |
Correct |
1633 ms |
202612 KB |
Output is correct |
40 |
Correct |
1640 ms |
199296 KB |
Output is correct |
41 |
Correct |
1628 ms |
198848 KB |
Output is correct |
42 |
Correct |
1611 ms |
198676 KB |
Output is correct |
43 |
Correct |
1612 ms |
200492 KB |
Output is correct |
44 |
Correct |
1709 ms |
202688 KB |
Output is correct |
45 |
Correct |
1716 ms |
202588 KB |
Output is correct |
46 |
Correct |
1706 ms |
199312 KB |
Output is correct |
47 |
Correct |
1707 ms |
199024 KB |
Output is correct |
48 |
Correct |
1691 ms |
199032 KB |
Output is correct |
49 |
Correct |
1732 ms |
201164 KB |
Output is correct |
50 |
Correct |
1843 ms |
202596 KB |
Output is correct |
51 |
Correct |
1844 ms |
202772 KB |
Output is correct |
52 |
Correct |
1805 ms |
200408 KB |
Output is correct |
53 |
Correct |
1841 ms |
199992 KB |
Output is correct |
54 |
Correct |
1833 ms |
199752 KB |
Output is correct |
55 |
Correct |
1833 ms |
201540 KB |
Output is correct |