#ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#endif
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
const int MM = 2e5+5, NN = 400, SZ = 500, LOG = 18;
struct query{
int t, l, r, id;
bool operator<(const query &o) const{
return t < o.t;
}
} q[MM];
int n, qs;
ll a[MM], ans[MM], sp[LOG][MM];
ll qu(int l, int r){
int k = __lg(r-l+1);
return max(sp[k][l], sp[k][r-(1<<k)+1]);
}
stack<pair<int, ll>> st;
vector<pair<int, ll>> slope[NN];
ll curslope[NN], cursum[NN];
//at time i, slope changes by x
//changes sum by something
int ls[MM], rs[MM];
//rs how far will cover
//ls how far until covered
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>qs;
for(int i = 1; i <= n; i++){
cin>>a[i];
sp[0][i] = a[i];
while(size(st) and st.top().second <= a[i]){
auto [j, v] = st.top();
st.pop();
rs[j] = i;
}
if(size(st)){
auto [j, v] = st.top();
ls[i] = j;
}
st.emplace(i, a[i]);
}
while(size(st)){
auto [j, v] = st.top();
st.pop();
rs[j] = n+1;
}
for(int i = 1; i <= n; i++){
//this one overwrites
int id = i/SZ;
int rid = rs[i]/SZ;
slope[id].emplace_back(i-i, a[i]);
slope[rid].emplace_back(rs[i]-i, -a[i]);
for(int j = id; j <= rid; j++){
if(j != id)
slope[j].emplace_back(j*SZ-i, a[i]);
if(j != rid)
slope[j].emplace_back(j*SZ+SZ-i, -a[i]);
}
//when another starts to cover a[i]
//and when it ends covering a[i]
// i-ls[i] to rs[i]-ls[i]
if(!ls[i])
continue;
slope[id].emplace_back(i-ls[i], -a[i]);
slope[rid].emplace_back(rs[i]-ls[i], a[i]);
for(int j = id; j <= rid; j++){
if(j != id)
slope[j].emplace_back(j*SZ-ls[i], -a[i]);
if(j != rid)
slope[j].emplace_back(j*SZ+SZ-ls[i], a[i]);
}
}
for(int k = 1; k <= __lg(n); k++){
for(int i = 1; i+(1<<k)-1 <= n; i++){
sp[k][i] = max(sp[k-1][i], sp[k-1][i+(1<<(k-1))]);
}
}
for(int i = 0; i <= n/SZ; i++){
sort(all(slope[i]), greater<pair<int, ll>>());
}
for(int i = 0; i < qs; i++){
cin>>q[i].t>>q[i].l>>q[i].r;
q[i].id = i;
}
sort(q, q+qs);
int t = -1;
for(int tt = 0; tt < qs; tt++){
auto [nt, l, r, qi] = q[tt];
for(int j = t+1; j <= nt; j++){
for(int i = 0; i <= n/SZ; i++){
while(size(slope[i]) and slope[i].back().first <= j){
curslope[i] += slope[i].back().second;
slope[i].pop_back();
}
cursum[i] += curslope[i];
}
}
t = nt;
r++;
int li = ((l+SZ-1)/SZ), ri = ((r)/SZ);
if(li >= ri){
for(int i = l; i < r; i++)
ans[qi] += qu(max(1, i-t), i);
}
else{
for(int i = l; i < li*SZ; i++)
ans[qi] += qu(max(1, i-t), i);
for(int i = ri*SZ; i < r; i++)
ans[qi] += qu(max(1, i-t), i);
for(int i = li; i < ri; i++)
ans[qi] += cursum[i];
}
}
for(int i = 0; i < qs; i++)
cout<<ans[i]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
728 ms |
49676 KB |
Output is correct |
3 |
Correct |
767 ms |
49260 KB |
Output is correct |
4 |
Correct |
708 ms |
49516 KB |
Output is correct |
5 |
Correct |
763 ms |
50412 KB |
Output is correct |
6 |
Correct |
729 ms |
49860 KB |
Output is correct |
7 |
Correct |
749 ms |
50028 KB |
Output is correct |
8 |
Correct |
734 ms |
50668 KB |
Output is correct |
9 |
Correct |
772 ms |
50452 KB |
Output is correct |
10 |
Correct |
746 ms |
49012 KB |
Output is correct |
11 |
Correct |
747 ms |
50284 KB |
Output is correct |
12 |
Correct |
732 ms |
49132 KB |
Output is correct |
13 |
Correct |
679 ms |
50028 KB |
Output is correct |
14 |
Correct |
705 ms |
50436 KB |
Output is correct |
15 |
Correct |
794 ms |
50380 KB |
Output is correct |
16 |
Correct |
688 ms |
50156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
414 ms |
49004 KB |
Output is correct |
3 |
Correct |
416 ms |
48108 KB |
Output is correct |
4 |
Correct |
384 ms |
50156 KB |
Output is correct |
5 |
Correct |
374 ms |
48504 KB |
Output is correct |
6 |
Correct |
456 ms |
49004 KB |
Output is correct |
7 |
Correct |
381 ms |
49004 KB |
Output is correct |
8 |
Correct |
354 ms |
48492 KB |
Output is correct |
9 |
Correct |
381 ms |
48108 KB |
Output is correct |
10 |
Correct |
374 ms |
47452 KB |
Output is correct |
11 |
Correct |
441 ms |
49644 KB |
Output is correct |
12 |
Correct |
376 ms |
49332 KB |
Output is correct |
13 |
Correct |
415 ms |
49428 KB |
Output is correct |
14 |
Correct |
369 ms |
48236 KB |
Output is correct |
15 |
Correct |
368 ms |
49516 KB |
Output is correct |
16 |
Correct |
427 ms |
49260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
733 ms |
44908 KB |
Output is correct |
2 |
Correct |
752 ms |
49220 KB |
Output is correct |
3 |
Correct |
749 ms |
50668 KB |
Output is correct |
4 |
Correct |
768 ms |
48876 KB |
Output is correct |
5 |
Correct |
743 ms |
49080 KB |
Output is correct |
6 |
Correct |
728 ms |
49516 KB |
Output is correct |
7 |
Correct |
737 ms |
50412 KB |
Output is correct |
8 |
Correct |
745 ms |
49772 KB |
Output is correct |
9 |
Correct |
748 ms |
49176 KB |
Output is correct |
10 |
Correct |
726 ms |
49900 KB |
Output is correct |
11 |
Correct |
745 ms |
49388 KB |
Output is correct |
12 |
Correct |
755 ms |
49516 KB |
Output is correct |
13 |
Correct |
784 ms |
49388 KB |
Output is correct |
14 |
Correct |
743 ms |
49516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
728 ms |
49676 KB |
Output is correct |
34 |
Correct |
767 ms |
49260 KB |
Output is correct |
35 |
Correct |
708 ms |
49516 KB |
Output is correct |
36 |
Correct |
763 ms |
50412 KB |
Output is correct |
37 |
Correct |
729 ms |
49860 KB |
Output is correct |
38 |
Correct |
749 ms |
50028 KB |
Output is correct |
39 |
Correct |
734 ms |
50668 KB |
Output is correct |
40 |
Correct |
772 ms |
50452 KB |
Output is correct |
41 |
Correct |
746 ms |
49012 KB |
Output is correct |
42 |
Correct |
747 ms |
50284 KB |
Output is correct |
43 |
Correct |
732 ms |
49132 KB |
Output is correct |
44 |
Correct |
679 ms |
50028 KB |
Output is correct |
45 |
Correct |
705 ms |
50436 KB |
Output is correct |
46 |
Correct |
794 ms |
50380 KB |
Output is correct |
47 |
Correct |
688 ms |
50156 KB |
Output is correct |
48 |
Correct |
414 ms |
49004 KB |
Output is correct |
49 |
Correct |
416 ms |
48108 KB |
Output is correct |
50 |
Correct |
384 ms |
50156 KB |
Output is correct |
51 |
Correct |
374 ms |
48504 KB |
Output is correct |
52 |
Correct |
456 ms |
49004 KB |
Output is correct |
53 |
Correct |
381 ms |
49004 KB |
Output is correct |
54 |
Correct |
354 ms |
48492 KB |
Output is correct |
55 |
Correct |
381 ms |
48108 KB |
Output is correct |
56 |
Correct |
374 ms |
47452 KB |
Output is correct |
57 |
Correct |
441 ms |
49644 KB |
Output is correct |
58 |
Correct |
376 ms |
49332 KB |
Output is correct |
59 |
Correct |
415 ms |
49428 KB |
Output is correct |
60 |
Correct |
369 ms |
48236 KB |
Output is correct |
61 |
Correct |
368 ms |
49516 KB |
Output is correct |
62 |
Correct |
427 ms |
49260 KB |
Output is correct |
63 |
Correct |
733 ms |
44908 KB |
Output is correct |
64 |
Correct |
752 ms |
49220 KB |
Output is correct |
65 |
Correct |
749 ms |
50668 KB |
Output is correct |
66 |
Correct |
768 ms |
48876 KB |
Output is correct |
67 |
Correct |
743 ms |
49080 KB |
Output is correct |
68 |
Correct |
728 ms |
49516 KB |
Output is correct |
69 |
Correct |
737 ms |
50412 KB |
Output is correct |
70 |
Correct |
745 ms |
49772 KB |
Output is correct |
71 |
Correct |
748 ms |
49176 KB |
Output is correct |
72 |
Correct |
726 ms |
49900 KB |
Output is correct |
73 |
Correct |
745 ms |
49388 KB |
Output is correct |
74 |
Correct |
755 ms |
49516 KB |
Output is correct |
75 |
Correct |
784 ms |
49388 KB |
Output is correct |
76 |
Correct |
743 ms |
49516 KB |
Output is correct |
77 |
Correct |
849 ms |
54872 KB |
Output is correct |
78 |
Correct |
829 ms |
56044 KB |
Output is correct |
79 |
Correct |
854 ms |
56032 KB |
Output is correct |
80 |
Correct |
803 ms |
55020 KB |
Output is correct |
81 |
Correct |
834 ms |
55148 KB |
Output is correct |
82 |
Correct |
838 ms |
55816 KB |
Output is correct |
83 |
Correct |
834 ms |
55404 KB |
Output is correct |
84 |
Correct |
812 ms |
54420 KB |
Output is correct |
85 |
Correct |
806 ms |
56044 KB |
Output is correct |
86 |
Correct |
881 ms |
54904 KB |
Output is correct |
87 |
Correct |
814 ms |
56300 KB |
Output is correct |
88 |
Correct |
835 ms |
56184 KB |
Output is correct |
89 |
Correct |
795 ms |
54208 KB |
Output is correct |
90 |
Correct |
818 ms |
56044 KB |
Output is correct |
91 |
Correct |
824 ms |
55020 KB |
Output is correct |
92 |
Correct |
846 ms |
54512 KB |
Output is correct |
93 |
Correct |
794 ms |
55532 KB |
Output is correct |
94 |
Correct |
830 ms |
56556 KB |
Output is correct |
95 |
Correct |
827 ms |
56172 KB |
Output is correct |
96 |
Correct |
807 ms |
55552 KB |
Output is correct |
97 |
Correct |
850 ms |
55532 KB |
Output is correct |
98 |
Correct |
723 ms |
54412 KB |
Output is correct |
99 |
Correct |
726 ms |
55148 KB |
Output is correct |
100 |
Correct |
744 ms |
55532 KB |
Output is correct |
101 |
Correct |
713 ms |
54588 KB |
Output is correct |
102 |
Correct |
717 ms |
55860 KB |
Output is correct |