#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
const int MM = 2e5+5, NN = 1000, SZ = 300, 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], lasttime[MM];
//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);
memset(ls, -0x3f, sizeof ls);
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
slope[i/SZ].emplace_back(i-i, a[i]);
slope[rs[i]/SZ].emplace_back(rs[i]-i, -a[i]);
for(int j = i/SZ; j <= rs[i]/SZ; j++){
if(j != i/SZ){
//start
slope[j].emplace_back(j*SZ-i, a[i]);
}
if(j != rs[i]/SZ){
//end
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]
slope[i/SZ].emplace_back(i-ls[i], -a[i]);
slope[rs[i]/SZ].emplace_back(rs[i]-ls[i], a[i]);
for(int j = i/SZ; j <= rs[i]/SZ; j++){
if(j != i/SZ){
//start
slope[j].emplace_back(j*SZ-ls[i], -a[i]);
}
if(j != rs[i]/SZ){
//end
slope[j].emplace_back(j*SZ+SZ-ls[i], a[i]);
}
}
}
for(int k = 1; k < LOG; k++){
for(int i = 0; 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]));
reverse(all(slope[i]));
}
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);
memset(lasttime, -1, sizeof lasttime);
for(int tt = 0; tt < qs; tt++){
auto [t, l, r, id] = q[tt];
for(int i = 0; i <= n/SZ; i++){
while(size(slope[i]) and slope[i].back().first <= t){
auto [x, m] = slope[i].back();
x--;
slope[i].pop_back();
cursum[i] += curslope[i]*(x-lasttime[i]);
lasttime[i] = x;
curslope[i] += m;
}
cursum[i] += curslope[i]*(t-lasttime[i]);
lasttime[i] = t;
}
r++;
int li = ((l+SZ-1)/SZ), ri = ((r)/SZ);
if(li >= ri){
for(int i = l; i < r; i++)
ans[id] += qu(max(1, i-t), i);
}
else{
for(int i = l; i < li*SZ; i++)
ans[id] += qu(max(1, i-t), i);
for(int i = ri*SZ; i < r; i++)
ans[id] += qu(max(1, i-t), i);
for(int i = li; i < ri; i++)
ans[id] += cursum[i];
}
}
for(int i = 0; i < qs; i++)
cout<<ans[i]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2796 KB |
Output is correct |
2 |
Correct |
2 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2796 KB |
Output is correct |
4 |
Correct |
2 ms |
2796 KB |
Output is correct |
5 |
Correct |
2 ms |
2796 KB |
Output is correct |
6 |
Correct |
2 ms |
2796 KB |
Output is correct |
7 |
Correct |
2 ms |
2796 KB |
Output is correct |
8 |
Correct |
2 ms |
2796 KB |
Output is correct |
9 |
Correct |
2 ms |
2796 KB |
Output is correct |
10 |
Correct |
2 ms |
2796 KB |
Output is correct |
11 |
Correct |
2 ms |
2796 KB |
Output is correct |
12 |
Correct |
2 ms |
2796 KB |
Output is correct |
13 |
Correct |
2 ms |
2796 KB |
Output is correct |
14 |
Correct |
2 ms |
2796 KB |
Output is correct |
15 |
Correct |
2 ms |
2796 KB |
Output is correct |
16 |
Correct |
2 ms |
2796 KB |
Output is correct |
17 |
Correct |
2 ms |
2796 KB |
Output is correct |
18 |
Correct |
2 ms |
2796 KB |
Output is correct |
19 |
Correct |
2 ms |
2796 KB |
Output is correct |
20 |
Correct |
2 ms |
2796 KB |
Output is correct |
21 |
Correct |
2 ms |
2796 KB |
Output is correct |
22 |
Correct |
2 ms |
2796 KB |
Output is correct |
23 |
Correct |
2 ms |
2796 KB |
Output is correct |
24 |
Correct |
2 ms |
2796 KB |
Output is correct |
25 |
Correct |
2 ms |
2796 KB |
Output is correct |
26 |
Correct |
2 ms |
2796 KB |
Output is correct |
27 |
Correct |
2 ms |
2796 KB |
Output is correct |
28 |
Correct |
2 ms |
2796 KB |
Output is correct |
29 |
Correct |
2 ms |
2796 KB |
Output is correct |
30 |
Correct |
2 ms |
2796 KB |
Output is correct |
31 |
Correct |
2 ms |
2796 KB |
Output is correct |
32 |
Correct |
2 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2796 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
50412 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2796 KB |
Output is correct |
2 |
Correct |
917 ms |
52588 KB |
Output is correct |
3 |
Correct |
962 ms |
51692 KB |
Output is correct |
4 |
Correct |
738 ms |
53996 KB |
Output is correct |
5 |
Correct |
965 ms |
52204 KB |
Output is correct |
6 |
Execution timed out |
1098 ms |
50716 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1095 ms |
49388 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2796 KB |
Output is correct |
2 |
Correct |
2 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2796 KB |
Output is correct |
4 |
Correct |
2 ms |
2796 KB |
Output is correct |
5 |
Correct |
2 ms |
2796 KB |
Output is correct |
6 |
Correct |
2 ms |
2796 KB |
Output is correct |
7 |
Correct |
2 ms |
2796 KB |
Output is correct |
8 |
Correct |
2 ms |
2796 KB |
Output is correct |
9 |
Correct |
2 ms |
2796 KB |
Output is correct |
10 |
Correct |
2 ms |
2796 KB |
Output is correct |
11 |
Correct |
2 ms |
2796 KB |
Output is correct |
12 |
Correct |
2 ms |
2796 KB |
Output is correct |
13 |
Correct |
2 ms |
2796 KB |
Output is correct |
14 |
Correct |
2 ms |
2796 KB |
Output is correct |
15 |
Correct |
2 ms |
2796 KB |
Output is correct |
16 |
Correct |
2 ms |
2796 KB |
Output is correct |
17 |
Correct |
2 ms |
2796 KB |
Output is correct |
18 |
Correct |
2 ms |
2796 KB |
Output is correct |
19 |
Correct |
2 ms |
2796 KB |
Output is correct |
20 |
Correct |
2 ms |
2796 KB |
Output is correct |
21 |
Correct |
2 ms |
2796 KB |
Output is correct |
22 |
Correct |
2 ms |
2796 KB |
Output is correct |
23 |
Correct |
2 ms |
2796 KB |
Output is correct |
24 |
Correct |
2 ms |
2796 KB |
Output is correct |
25 |
Correct |
2 ms |
2796 KB |
Output is correct |
26 |
Correct |
2 ms |
2796 KB |
Output is correct |
27 |
Correct |
2 ms |
2796 KB |
Output is correct |
28 |
Correct |
2 ms |
2796 KB |
Output is correct |
29 |
Correct |
2 ms |
2796 KB |
Output is correct |
30 |
Correct |
2 ms |
2796 KB |
Output is correct |
31 |
Correct |
2 ms |
2796 KB |
Output is correct |
32 |
Correct |
2 ms |
2816 KB |
Output is correct |
33 |
Execution timed out |
1090 ms |
50412 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |