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 optimization "O2"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 2e5 + 16;
const ll Log2 = 19;
const ll inf = 1e9 + 7;
typedef pair<ll,ll> LL;
///Road to EF/M
ll s[N],n,Q,T,l,r,ans[N],pre[N],suf[N];
struct Query{
ll l,r,id;
};
vector<Query> q[N];
vector<LL> f[N],g[N];
ll bit[N];
void upd(ll p,ll val){
for (ll i = p;i < N;i += i & -i) bit[i] += val;
}
ll Get(ll p){
ll res = 0;
for (ll i = p;i > 0;i -= i & -i) res += bit[i];
return res;
}
stack<ll> st;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define task "test"
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
//freopen(task".out", "w", stdout);
}
cin>>n>>Q;
for (ll i = 1;i <= n;i++) cin>>s[i];
for (ll i = 1;i <= Q;i++){
cin>>T>>l>>r;
q[T].push_back({l,r,i});
}
for (ll i = 1;i <= n;i++){
while(!st.empty() && s[st.top()] <= s[i]) st.pop();
if (!st.empty()) pre[i] = st.top();
st.push(i);
}
while(!st.empty()) st.pop();
for (ll i = n;i > 0;i--){
while(!st.empty() && s[st.top()] < s[i]) st.pop();
if (!st.empty()) suf[i] = st.top();
else suf[i] = n + 1;
st.push(i);
}
for (ll i = 1;i <= n;i++){
if (!pre[i] || i - pre[i] > suf[i] - i){
for (ll j = i;j < suf[i];j++){
f[j - i].push_back({j,s[i]});
if (pre[i]) f[j - pre[i]].push_back({j,-s[i]});
}
}
else{
for (ll j = i;j > pre[i];j--){
g[i - j].push_back({j,s[i]});
g[suf[i] - j].push_back({j,-s[i]});
}
}
}
for (ll i = 0;i <= n;i++){
for (auto j : f[i]) upd(j.fs,j.sc);
for (auto j : q[i]) ans[j.id] += Get(j.r) - Get(j.l - 1);
}
memset(bit,0,sizeof(bit));
for (ll i = 0;i <= n;i++){
for (auto j : g[i]) upd(j.fs,j.sc);
for (auto j : q[i]) ans[j.id] += Get(j.r) - Get(j.l - 1);
}
for (ll i = 1;i <= Q;i++) cout<<ans[i]<<"\n";
}
Compilation message (stderr)
ho_t5.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
1 | #pragma GCC optimization "O2"
|
ho_t5.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization "unroll-loop"
|
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |