Submission #905390


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define mem(a) memset(a,0,sizeof(a))
#define pd(a) printf("%.10f\n",a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin>>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout<<'\n'
template<C T>void pr(T a){cout<<a;ln;}
template<C T,C T2>void pr(T a,T2 b){cout<<a<<' '<<b;ln;}
template<C T,C T2,C T3>void pr(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}
template<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;
class RMQ{
public:
  int n,dat[555555];
  void init(int _n){
    n=1;
    while(n<_n)n*=2;
    fill(dat,dat+2*n-1,MAX);
  }
  void update(int k,int a){
    k+=n-1;dat[k]=a;
    while(k>0){
      k=(k-1)/2;
      dat[k]=min(dat[k*2+1],dat[k*2+2]);
    }
  }
  int query(int a,int b){return query(a,b,0,0,n);}
  int query(int a,int b,int k,int l,int r){
    if(r<=a||b<=l) return MAX;
    if(a<=l&&r<=b) return dat[k];
    int vl=query(a,b,k*2+1,l,(l+r)/2);
    int vr=query(a,b,k*2+2,(l+r)/2,r);
    return min(vl,vr);
  }
};

void Main() {
  ll n;
  R n;
  ll a[n];
  RMQ t;
  t.init(n+1);
  rep(i,n) {
    R a[i];
    t.update(i+1,a[i]);
  }
  ll ans=0;
  rep(i,n) {
    ll l=0,r=i+1;
    while(l+1<r) {
      int m=(l+r)/2;
      if(t.query(m,i+2)<a[i]) l=m;
      else r=m;
    }
    ll x=i-l+1;
    l=i,r=n;
    while(l+1<r) {
      int m=(l+r)/2;
      if(t.query(i+1,m+2)<a[i]) r=m;
      else l=m;
    }
    ll y=l-i+1;
    ans+=x*y*a[i];
  }
  pr(ans);
}

int main() {
  ios::sync_with_stdio(0);cin.tie(0);
  Main();return 0;
}

Submission Info

Submission Time
Task B - Minimum Sum
User kzyKT
Language C++14 (GCC 5.4.1)
Score 400
Code Size 2091 Byte
Status AC
Exec Time 1042 ms
Memory 3840 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 13
Set Name Test Cases
Sample example0, example1, example2
All corner0, corner1, corner2, corner3, example0, example1, example2, maxrand0, maxrand1, maxrand2, rand0, rand1, rand2
Case Name Status Exec Time Memory
corner0 AC 1036 ms 3840 KB
corner1 AC 944 ms 3840 KB
corner2 AC 3 ms 256 KB
corner3 AC 1042 ms 3840 KB
example0 AC 3 ms 256 KB
example1 AC 3 ms 256 KB
example2 AC 3 ms 256 KB
maxrand0 AC 985 ms 3840 KB
maxrand1 AC 985 ms 3840 KB
maxrand2 AC 984 ms 3840 KB
rand0 AC 3 ms 256 KB
rand1 AC 2 ms 256 KB
rand2 AC 3 ms 256 KB