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>
using namespace std;
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=600007;
ll BIT[N];
void ins(int v,ll x)
{
for(;v>0;v-=(v&-v)) BIT[v]+=x;
}
ll que(int v)
{
ll res=0;
for(;v<N;v+=(v&-v)) res+=BIT[v];
return res;
}
int a[N];
int L[N],R[N];
struct ev
{
int t,b,l,r,x;
};
bool cmp(ev l,ev r)
{
if(l.t==r.t) return l.b<r.b;
return l.t<r.t;
}
ll ans[N];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++) cin>>a[i];
vector<pair<int,int>>S;
S.pb({inf,n+1});
for(int i=n;i>0;i--)
{
while(S.back().st<=a[i]) S.pop_back();
R[i]=S.back().nd-1;
S.pb({a[i],i});
}
S.clear();
S.pb({inf,-n});
for(int i=1;i<=n;i++)
{
while(S.back().st<i) S.pop_back();
L[i]=S.back().nd;
S.pb({R[i],i});
}
vector<ev>E1,E2;
for(int i=1;i<=n;i++)
{
int t=i-L[i]-1,d=R[i]-i+1;
if(d<=t)
{
for(int j=i;j<=R[i];j++)
{
E1.pb({j,0,j-i,j-i+t,a[i]});
}
}
else
{
for(int j=0;j<=t;j++)
{
E2.pb({i-j,0,j,j+d-1,a[i]});
}
}
}
for(int i=1;i<=q;i++)
{
int t,l,r;
cin>>t>>l>>r;
E1.pb({l,-i,t,0,0});
E1.pb({r,i,t,0,0});
E2.pb({l-t,-i,t,0,0});
E2.pb({r-t,i,t,0,0});
}
sort(all(E1),cmp);
for(auto x:E1)
{
if(x.b<0) ans[-x.b]-=que(x.l);
if(x.b>0) ans[x.b]+=que(x.l);
if(x.b==0)
{
ins(x.r,x.x);
ins(x.l-1,-x.x);
}
}
memset(BIT,0,sizeof BIT);
sort(all(E2),cmp);
for(auto x:E2)
{
if(x.b<0) ans[-x.b]-=que(x.l);
if(x.b>0) ans[x.b]+=que(x.l);
if(x.b==0)
{
ins(x.r,x.x);
ins(x.l-1,-x.x);
}
}
for(int i=1;i<=q;i++) cout<<ans[i]<<endl;
return 0;
}
# | 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... |