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 <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, ll>
#define fi first
#define se second
using namespace std;
const int N = 2e5+5;
const int M = 250;
const int mod = 1e9+7;
const ll base = 75;
const ll inf = 1e16;
ll n, t, lab[N], c[N], b[N], a[N];
ll ans, tong, d[N], l[N], r[N], fe[N];
ll m, k;
vector<pii> adj[N], radj[N];
vector<int> vi, ask[N];
string s, res;
struct node
{
int l, r, t;
}q[N];
void add(int id, ll x)
{
assert(id > 0);
for(; id <= n; id += id & -id)fe[id] += x;
}
ll get(int id)
{
ll total = 0;
if(id <= 0)return total;
for(; id; id -= id & -id)total += fe[id];
return total;
}
void sol()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
while(!vi.empty() && a[i] >= a[vi.back()])vi.pop_back();
l[i] = vi.empty() ? 0 : vi.back();
vi.pb(i);
}
vi.clear();
for(int i = n; i > 0; i --)
{
while(!vi.empty() && a[i] > a[vi.back()])vi.pop_back();
r[i] = vi.empty() ? n+1 : vi.back();
vi.pb(i);
}
for(int i = 1; i <= n; i ++)
{
//cout << l[i] <<" "<<r[i] << '\n';
if(!l[i] || i-l[i] > r[i]-i)
{
for(int j = i; j < r[i]; j ++)
{
adj[j-i].pb({j, a[i]});
if(l[i])adj[j-l[i]].pb({j, -a[i]});
}
}
else
{
for(int j = i; j > l[i]; j --)
{
radj[i-j].pb({j, a[i]});
if(r[i] <= n)radj[r[i]-j].pb({j, -a[i]});
}
}
}
for(int i = 1; i <= m; i ++)
{
cin >> q[i].t >> q[i].l >> q[i].r;
ask[q[i].t].pb(i);
}
for(int i = 0; i <= n; i ++)
{
for(pii j: adj[i])add(j.fi, j.se);
for(int j: ask[i])d[j] += get(q[j].r)-get(q[j].l-1);
}
fill_n(fe, n+1, 0);
for(int i = 0; i <= n; i ++)
{
for(pii j: radj[i])add(j.fi, j.se);
for(int j: ask[i])d[j] += get(q[j].r-i) - get(q[j].l-i-1);
}
for(int i = 1; i <= m; i ++)cout << d[i] << '\n';
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
//cin >> test;
while(test -- > 0)sol();
return 0;
}
Compilation message (stderr)
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ho_t5.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | 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... |