This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e5 + 100;
int n, q;
ll a[N];
int p[N], r[N];
ll st[6 * N], si[6 * N], sd[6 * N], que[6 * N];
ll ans[6 * N];
bool comp1(int i, int j) {
if(st[i] == st[j])
return (i < j);
return (st[i] < st[j]);
}
bool comp2(int i, int j) {
if(si[i] == si[j])
return (i < j);
return (si[i] < si[j]);
}
int shift;
struct seg {
ll t[4 * N] = {0}, t2[4 * N] = {0};
void clear(ll v = 1, ll tl = 0, ll tr = n + 4) {
if(tl == tr) {
t[v] = 0;
t2[v] = 0;
return;
}
ll m = (tl + tr) >> 1ll;
clear(v * 2, tl, m);
clear(v * 2 + 1, m + 1, tr);
t[v] = 0;
t2[v] = 0;
}
void upd(ll p, ll z, ll z2, ll v = 1, ll tl = 0, ll tr = n + 4) {
if(tl == tr) {
t[v] += z;
t2[v] += z2;
return;
}
ll m = (tl + tr) >> 1ll;
if(p <= m)upd(p, z, z2, v * 2, tl, m);
else upd(p, z, z2, v * 2 + 1, m + 1, tr);
t[v] = t[v * 2] + t[v * 2 + 1];
t2[v] = t2[v * 2] + t2[v * 2 + 1];
}
pair<ll, ll> get(ll l, ll r, ll v = 1, ll tl = 0, ll tr = n + 4) {
if(l > tr || tl > r) return mp(0, 0);
if (l <= tl && tr <= r) return mp(t[v], t2[v]);
ll m = (tl + tr) >> 1ll;
pair<ll, ll> a = get(l, r, v * 2, tl, m);
pair<ll, ll> b = get(l, r, v * 2 + 1, m + 1, tr);
return mp(a.f + b.f, a.s + b.s);
}
} rt;
int main() {
speed;
cin >> n >> q;
shift = n + 1;
deque<int> s;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int j = 1; j <= n; j++) {
while(s.size() && a[s.back()] < a[j])s.pop_back();
if(s.size())p[j] = s.back();
s.push_back(j);
}
while(s.size())s.pop_back();
for(int j = n; j >= 1; j--) {
r[j] = n + 1;
while(s.size() && a[s.back()] <= a[j])s.pop_back();
if(s.size())r[j] = s.back();
s.push_back(j);
}
int cnt = 1;
for(int i = 1, t; i <= n; i++) {
// 1
st[cnt] = 1;
sd[cnt] = a[i];
si[cnt++] = i;
// 2
if(r[i] != n + 1) {
st[cnt] = 1 + (r[i] - i);
sd[cnt] = -a[i];
si[cnt++] = r[i];
}
// 3
if(p[i] != 0) {
st[cnt] = 1 + (i - p[i]);
sd[cnt] = -a[i];
si[cnt++] = i;
//4
if(r[i] != n + 1) {
st[cnt] = 1 + (r[i] - p[i]);
sd[cnt] = a[i];
si[cnt++] = r[i];
}
}
}
vector<int> sbt;
for(int i = 1; i <= n; i++)
a[i] += a[i - 1];
for(int i = 1; i <= q; i++) {
ll t, l, r;
cin >> t >> l >> r;
t++;
que[cnt] = i;
st[cnt] = t;
si[cnt++] = r;
que[cnt] = -i;
st[cnt] = t;
si[cnt++] = l - 1;
}
//kek
cnt--;
for(int i = 1; i <= cnt; i++) {
sbt.pb(i);
}
vector<int> queries;
sort(sbt.begin(), sbt.end(), comp1);
for(int i = 0; i < cnt; i++) {
int s = sbt[i];
if(que[s]) {
ll qt = st[s], qi = si[s], znak = (que[s] < 0 ? -1 : 1);
pair<ll, ll> ggg = rt.get(qt - qi + 1 + shift, n + 4);
ans[que[s] * znak] += znak * (qt + 1) * ggg.f;
ans[que[s] * znak] -= znak * ggg.s;
} else {
rt.upd(st[s] - si[s] + shift, sd[s], st[s] * sd[s]);
}
}
rt.clear();
sort(sbt.begin(), sbt.end(), comp2);
for(int i = 0; i < cnt; i++) {
int s = sbt[i];
if(que[s]) {
ll qt = st[s], qi = si[s], znak = (que[s] < 0 ? -1 : 1);
pair<ll, ll> ggg = rt.get(0, qt - qi + shift);
ans[que[s] * znak] += znak * (qi + 1) * ggg.f;
ans[que[s] * znak] -= znak * ggg.s;
} else {
rt.upd(st[s] - si[s] + shift, sd[s], si[s] * sd[s]);
}
}
for(int i = 1; i <= q; i++)
cout << ans[i] << "\n";
}
Compilation message (stderr)
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:96:17: warning: unused variable 't' [-Wunused-variable]
96 | for(int i = 1, t; i <= n; i++) {
| ^
# | 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... |