#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
#define debug(s) for(auto x: s) cerr << x << " "; cerr << #s << "\n";
struct segtree{
struct node{
ll sm, lazy_sm;
node(){
sm = 0, lazy_sm = 0;
}
node(ll x){
sm = x, lazy_sm = 0;
}
node operator+(node a){
node rt;
rt.sm = sm+a.sm;
return rt;
}
void pushdown_from(node a, int sz){
sm+=a.lazy_sm*sz;
lazy_sm+=a.lazy_sm;
}
};
int k;
vector<node> v;
segtree(int n){
k = 1;
while(k < n) k*=2;
k*=2;
v.resize(k, node());
}
void add(int l, int r, int nd, int a, int b, ll x){
if(a > r || b < l) return;
if(a >= l && b <= r){
v[nd].sm+=(b-a+1)*x;
v[nd].lazy_sm+=x;
return;
}
int md = (a+b)/2;
v[2*nd].pushdown_from(v[nd], md-a+1);
v[2*nd+1].pushdown_from(v[nd], b-md);
v[nd].lazy_sm = 0;
add(l, r, 2*nd, a, md, x);
add(l, r, 2*nd+1, md+1, b, x);
v[nd] = v[2*nd]+v[2*nd+1];
}
void add(int l, int r, int x){
add(l, r, 1, 0, k/2-1, x);
}
ll sum(int l, int r, int nd, int a, int b){
if(a > r || b < l) return 0;
if(a >= l && b <= r) return v[nd].sm;
int md = (a+b)/2;
v[2*nd].pushdown_from(v[nd], md-a+1);
v[2*nd+1].pushdown_from(v[nd], b-md);
v[nd].lazy_sm = 0;
ll rt = sum(l, r, 2*nd, a, md) + sum(l, r, 2*nd+1, md+1, b);
v[nd] = v[2*nd]+v[2*nd+1];
return rt;
}
ll sum(int l, int r){
return sum(l, r, 1, 0, k/2-1);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q; cin >> n >> q;
vector<ll> s(n);
for(ll &x: s) cin >> x;
vector<int> prev(n, -1);
stack<int> st;
for(int i = n-1; i >= 0; i--){
while(!st.empty() && s[i] > s[st.top()]) prev[st.top()] = i, st.pop();
st.push(i);
}
while(!st.empty()) st.pop();
vector<vector<int>> end(n+1);
vector<vector<pair<int, int>>> chng(n+1);
for(int i = 0; i < n; i++){
while(!st.empty() && s[st.top()] <= s[i]){
int x = st.top();
st.pop();
end[i-x-1].pb(x);
if(prev[x] == -1) continue;
if(s[prev[x]] <= s[i]) continue;
chng[i-prev[x]-1].pb({prev[x], i});
}
st.push(i);
}
while(!st.empty()) end[n-st.top()-1].pb(st.top()), st.pop();
vector<vector<array<int, 3>>> queries(n);
for(int i = 0; i < q; i++){
int t, l, r; cin >> t >> l >> r; t--, l--, r--;
queries[t].pb({l, r, i});
}
vector<ll> ans(q, 0);
segtree ss(n), s1(n), s2(n);
for(int i = 0; i < n; i++) ss.add(i, i, s[i]);
for(int i = 0; i+1 < n; i++) s1.add(i, i, max(0LL, s[i]-s[i+1]));
for(int t = 1; t <= n; t++){
for(int in: end[t-1]){
ll x = s1.sum(in, in);
if(in != n-1) ss.add(in+1, in+t-1, x);
s1.add(in, in, -x);
}
for(auto [in, nxt]: chng[t-1]){
ll x = s1.sum(in, in);
ll y = x-(s[in]-s[nxt]);
ss.add(in+1, nxt-1, y);
s1.add(in, in, (s[in]-s[nxt])-x);
}
for(auto [l, r, in]: queries[t-1]){
ans[in]+=ss.sum(l, r);
int mx, a, b;
if(t <= r-l+1){
mx = t;
a = l, b = r-mx+1;
a--, b--;
a = max(a, 0);
}
else{
mx = r-l+1;
a = r-t+1, b = l;
a--, b--;
a = max(a, 0);
}
ans[in]+=s1.sum(a, b)*mx;
for(int i = a-1, c = mx-1; i >= 0 && c > 0; i--, c--) ans[in]+=s1.sum(i, i)*c;
for(int i = b+1, c = mx-1; i < n && c > 0; i++, c--) ans[in]+=s1.sum(i, i)*c;
}
}
for(ll x: ans) cout << x << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
360 KB |
Output is correct |
6 |
Correct |
2 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 |
356 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Execution timed out |
1089 ms |
46620 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
621 ms |
50572 KB |
Output is correct |
3 |
Correct |
603 ms |
50232 KB |
Output is correct |
4 |
Correct |
637 ms |
51252 KB |
Output is correct |
5 |
Correct |
604 ms |
50360 KB |
Output is correct |
6 |
Correct |
605 ms |
50728 KB |
Output is correct |
7 |
Correct |
599 ms |
50780 KB |
Output is correct |
8 |
Correct |
624 ms |
50512 KB |
Output is correct |
9 |
Correct |
621 ms |
50260 KB |
Output is correct |
10 |
Correct |
585 ms |
50036 KB |
Output is correct |
11 |
Correct |
616 ms |
51004 KB |
Output is correct |
12 |
Correct |
603 ms |
50716 KB |
Output is correct |
13 |
Correct |
629 ms |
50868 KB |
Output is correct |
14 |
Correct |
598 ms |
50320 KB |
Output is correct |
15 |
Correct |
643 ms |
50932 KB |
Output is correct |
16 |
Correct |
660 ms |
50588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1091 ms |
47912 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
360 KB |
Output is correct |
6 |
Correct |
2 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 |
356 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Execution timed out |
1089 ms |
46620 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |