This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#define task "A"
using namespace std;
using ll = long long;
using ld = long double;
const int N = 2e5 + 5;
struct FenwickTree
{
ll a[N];
int n;
FenwickTree()
{
memset(a, 0, sizeof a);
}
void Clear()
{
memset(a, 0, sizeof a);
}
void Update(int p, ll v)
{
for (; p <= n; p += p & -p)
a[p] += v;
}
ll Get(int p)
{
ll ans(0);
for (; p > 0; p -= p & -p)
ans += a[p];
return ans;
}
ll Get(int l, int r)
{
return Get(r) - Get(l - 1);
}
} f;
int n, pre[N], suf[N], q;
ll a[N], ans[N];
vector<pair<pair<int, int>, int>> que[N];
vector<pair<int, ll>> upd[N], upp[N];
void Read()
{
cin >> n >> q;
f.n = n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int i = 1; i <= q; ++i)
{
int t, l, r;
cin >> t >> l >> r;
que[t].push_back({{l, r}, i});
}
}
void Solve()
{
vector<int> s;
s.reserve(n);
for (int i = 1; i <= n; ++i)
{
while (s.size() && a[s.back()] < a[i])
s.pop_back();
pre[i] = s.empty() ? 0 : s.back();
s.push_back(i);
}
s.clear();
for (int i = n; i; --i)
{
while (s.size() && a[s.back()] <= a[i])
s.pop_back();
suf[i] = s.empty() ? n + 1 : s.back();
s.push_back(i);
}
for (int i = 1; i <= n; ++i)
{
if (pre[i] == 0 || suf[i] - i < i - pre[i])
for (int j = i; j < suf[i]; ++j)
{
upd[j - i].push_back({j, a[i]});
if (pre[i])
upd[j - pre[i]].push_back({j, -a[i]});
}
else
for (int j = pre[i] + 1; j <= i; ++j)
{
upp[i - j].push_back({j, a[i]});
upp[suf[i] - j].push_back({j, -a[i]});
}
}
for (int i = 0; i <= n; ++i)
{
//cout << i << ": ";
for (auto j : upd[i])
f.Update(j.first, j.second);
//for (int i = 1; i <= n; ++i)
// cout << f.Get(i, i) << " ";
//cout << "\n";
for (auto j : que[i])
ans[j.second] += f.Get(j.first.first, j.first.second);
}
f.Clear();
for (int i = 0; i <= n; ++i)
{
for (auto j : upp[i])
f.Update(j.first, j.second);
for (auto j : que[i])
ans[j.second] += f.Get(j.first.first - i, j.first.second - i);
}
for (int i = 1; i <= q; ++i)
cout << ans[i] << "\n";
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (fopen(task ".INP", "r"))
{
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
Read();
Solve();
}
Compilation message (stderr)
ho_t5.cpp: In function 'int32_t main()':
ho_t5.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
124 | freopen(task ".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t5.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
125 | freopen(task ".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |