# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
205381 | TadijaSebez | Fire (JOI20_ho_t5) | C++11 | 348 ms | 21240 KiB |
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 pb push_back
#define ll long long
const int N=200050;
struct BIT{
ll sum[N];
void init(){for(int i=0;i<N;i++)sum[i]=0;}
BIT(){init();}
void Set(int i,ll f){for(i++;i<N;i+=i&-i)sum[i]+=f;}
ll Get(int i){ll ans=0;for(i++;i;i-=i&-i)ans+=sum[i];return ans;}
}SUM,MUL,STK;
int a[N];
vector<pair<int,int>> Qs[N];
int stk[N],top;
ll ans[N];
void AddD(int t,ll f){
//printf("AddD %i %lld\n",t,f);
SUM.Set(t,-t*f);
MUL.Set(t,f);
}
ll Get(int t){
//printf("t:%i mul:%lld sum:%lld\n",t,MUL.Get(t),SUM.Get(t));
return (t+1)*MUL.Get(t)+SUM.Get(t);
}
int main(){
int n,q;
scanf("%i %i",&n,&q);
for(int i=1;i<=n;i++)scanf("%i",&a[i]);
for(int i=1,t,l,r;i<=q;i++)scanf("%i %i %i",&t,&l,&r),Qs[r].pb({i,t}),Qs[l-1].pb({-i,t});
for(int i=1;i<=n;i++){
while(top && a[stk[top]]<=a[i]){
AddD(i-stk[top],-a[stk[top]]);
if(top>1)AddD(i-stk[top-1],a[stk[top]]);
if(top>1)STK.Set(top,-(ll)(stk[top]-stk[top-1])*a[stk[top]]);
top--;
}
AddD(0,a[i]);
if(top>0)AddD(i-stk[top],-a[i]);
stk[++top]=i;
if(top>1)STK.Set(top,(ll)(stk[top]-stk[top-1])*a[stk[top]]);
for(auto Q:Qs[i]){
int t=Q.second;
ll now=Get(t);
//printf("Get: %lld ",now);
int hi=top,lo=1,mid,pos=top;
while(hi>=lo){
mid=hi+lo>>1;
if(i-stk[mid]<=t)pos=mid,hi=mid-1;
else lo=mid+1;
}
now-=STK.Get(top)-STK.Get(pos);
//printf("%lld ",now);
now-=a[stk[pos]]*(t-(i-stk[pos]));
int id=abs(Q.first);
//printf("i:%i t:%i now:%lld\n",i,t,now);
if(Q.first<0)ans[id]-=now;
else ans[id]+=now;
}
}
for(int i=1;i<=q;i++)printf("%lld\n",ans[i]);
return 0;
}
Compilation message (stderr)
# | 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... |