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<array>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
#include<unordered_map>
#include<cstring>
#include<iomanip>
#include<bitset>
#include<tuple>
#define ll long long
#define LL __int128_t
#define DB double
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define INF (ll)(1e9)
#define MOD (ll)(1e9+7)
#define F first
#define S second
#define endl "\n"
#define AC ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
template<class T> using PQ=priority_queue<T,vector<T>,greater<T> >;
void file(){
freopen("/Users/liaoyunyang/Desktop/meta_in.txt","r",stdin);
freopen("/Users/liaoyunyang/Desktop/meta_out.txt","w",stdout);
}
vector<pair<pair<int,int>,ll> >op[200005],query[200005];
ll seg[2][800005],tag[2][800005];
void push(int t,int id,int l,int r){
if(!tag[t][id]) return;
int mid=(l+r)>>1;
seg[t][id<<1]+=tag[t][id]*(mid-l+1);
seg[t][(id<<1)|1]+=tag[t][id]*(r-mid);
tag[t][id<<1]+=tag[t][id];
tag[t][(id<<1)|1]+=tag[t][id];
tag[t][id]=0;
}
void add(int t,int id,int l,int r,int L,int R,ll v){
if(l>R||r<L) return;
if(L<=l&&r<=R){
seg[t][id]+=v*(r-l+1);
tag[t][id]+=v;
return;
}
int mid=(l+r)>>1;
push(t,id,l,r);
add(t,id<<1,l,mid,L,R,v);
add(t,(id<<1)|1,mid+1,r,L,R,v);
seg[t][id]=seg[t][id<<1]+seg[t][(id<<1)|1];
}
ll ask(int t,int id,int l,int r,int L,int R){
if(l>R||r<L) return 0;
if(L<=l&&r<=R){
//if(t==0) cout<<" l="<<l<<" r="<<r<<" val="<<seg[t][id]<<endl;
return seg[t][id];
}
push(t,id,l,r);
int mid=(l+r)>>1;
return ask(t,id<<1,l,mid,L,R)+ask(t,(id<<1)|1,mid+1,r,L,R);
}
int n,q;
void add_tri(int l,int r,ll v){
//cout<<"l="<<l<<" r="<<r<<" v="<<v<<endl;
add(0,1,1,n,l,n,v);
if(r+1<=n) add(1,1,1,n,r+1,n,-v);
op[(r-l+1)].pb({{l,r},v});
}
int s[200005],l[200005],r[200005];
ll ans[200005];
signed main(){
AC;
cin>>n>>q;
FOR(i,1,n+1) cin>>s[i];
stack<pair<int,int> > st;
FOR(i,1,n+1){
while(st.size()&&st.top().F<s[i]){
st.pop();
}
if(!st.size()) l[i]=0;
else l[i]=st.top().S;
st.push({s[i],i});
}
while(st.size()) st.pop();
REP(i,n,1){
while(st.size()&&st.top().F<=s[i]){
st.pop();
}
if(!st.size()) r[i]=n+1;
else r[i]=st.top().S;
st.push({s[i],i});
}
FOR(i,1,n+1){
//cout<<"l["<<i<<"]="<<l[i]<<" r["<<i<<"]="<<r[i]<<endl;
if(!l[i]){
add(1,1,1,n,i,r[i]-1,s[i]);
if(i+1<=r[i]-1) add_tri(i+1,r[i]-1,-s[i]);
}
else{
add_tri(l[i]+1,r[i]-1,s[i]);
if(l[i]+1<=i-1) add_tri(l[i]+1,i-1,-s[i]);
if(i+1<=r[i]-1) add_tri(i+1,r[i]-1,-s[i]);
}
}
FOR(i,0,q){
int t,l,r; cin>>t>>l>>r;
query[t].pb({{l,r},i});
}
FOR(i,1,n+1){
//cout<<"i="<<i<<endl;
for(auto [a,b]:op[i]){
//cout<<"b="<<b<<endl;
add(0,1,1,n,a.F,n,-b);
if(a.S+1<=n) add(1,1,1,n,a.S+1,n,b);
}
for(auto [a,b]:query[i]){
//cout<<"a="<<a.F<<" "<<a.S<<endl;
ans[b]=ask(0,1,1,n,a.F-i,a.S-i)+ask(1,1,1,n,a.F,a.S);
//cout<<"b="<<b<<endl;
//cout<<ask(0,1,1,n,a.F-i,a.S-i)<<endl;
//cout<<ask(1,1,1,n,a.F,a.S)<<endl;
}
}
FOR(i,0,q) cout<<ans[i]<<endl;
}
/*
*/
Compilation message (stderr)
ho_t5.cpp: In function 'void file()':
ho_t5.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen("/Users/liaoyunyang/Desktop/meta_in.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t5.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen("/Users/liaoyunyang/Desktop/meta_out.txt","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... |