이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld double
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define pb emplace_back
#define X first
#define Y second
const int N = 4e5 + 5;
typedef pair<ll,ll> pll;
struct BIT {
pll T[N];
int upd(int p,pll d) {
for(p += N / 2 ; p < N ; p += p & -p)
T[p].X += d.X,
T[p].Y += d.Y;
return 1;
}
ll get(int p,int x) {
ll s = 0;
ll c = 0;
for(p += N / 2 + 1 ; p > 0 ; p -= p & -p)
s += T[p].X,
c += T[p].Y;
return s * x - c;
}
} pref;
BIT suff;
int a[N];
int l[N];
int r[N];
vector<tuple<int,int,int> > Upd[N];
vector<tuple<int,int,int> > Que[N];
void Add(int l,int r,int v) {
if (l > r) return;
int k = r - l + 1;
Upd[0].pb(l,r, v);
Upd[k].pb(l,r,-v);
}
ll ask(int T,int r) {
ll ans = 0;
ans += suff.get(r,r); r -= T;
ans += pref.get(r,r);
return ans;
}
ll ans[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
int q; cin >> q;
for(int i = 1 ; i <= n ; ++i)
cin >> a[i];
stack<int> st;
for(int i = 1 ; i <= n ; ++i) {
while (st.size() && a[st.top()] <= a[i])
st.pop();
if (st.size()) l[i] = st.top() + 1;
else l[i] = -n;
st.push(i);
}
while (st.size()) st.pop();
for(int i = n ; i >= 1 ; --i) {
while (st.size() && a[st.top()] < a[i])
st.pop();
if (st.size()) r[i] = st.top() - 1;
else r[i] = n;
st.push(i);
}
for(int i = 1 ; i <= n ; ++i) {
Add(l[i] ,r[i] , a[i]);
Add(l[i] ,i - 1,-a[i]);
Add(i + 1,r[i] ,-a[i]);
}
for(int i = 0 ; i < q ; ++i) {
int t; cin >> t;
int l; cin >> l;
int r; cin >> r;
Que[t].pb(l,r,i);
}
for(int t = 0 ; t <= n ; ++t) {
for(auto it : Upd[t]) {
int l, r, v;
tie(l, r, v) = it; ++r;
pref.upd(l,pll( v, 1ll * v * (l - 1)));
suff.upd(r,pll(-v,-1ll * v * (r - 1)));
}
for(auto it : Que[t]) {
int l, r, i;
tie(l, r, i) = it;
ans[i] += ask(t,r);
ans[i] -= ask(t,l - 1);
}
}
for(int i = 0 ; i < q ; ++i)
cout << ans[i] << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |