Submission #457920

# Submission time Handle Problem Language Result Execution time Memory
457920 2021-08-07T14:33:55 Z Sarah_Mokhtar Vudu (COCI15_vudu) C++14
140 / 140
852 ms 57804 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define read freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define LESSGO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const ll N=1e6+10,M=505,OO=1e16,mod=1e9+7;
ll n,k,a[N],ans,sum;
ll tree[N<<2],val[N],idx;
vector<pair<ll,ll>>prefixes;
void compress(){
    sort(prefixes.begin(),prefixes.end());
    ll prev=-OO;
    for(auto i:prefixes){
        if(i.first!=prev) ++idx;
        val[i.second]=idx;
        prev=i.first;
    }
}
void update(int node,int st,int en,int idx,int val){
    if(st==en){
        tree[node]+=val;
        return;
    }
    int mid=(st+en)>>1;
    if(idx<=mid) update(2*node,st,mid,idx,val);
    else update(2*node+1,mid+1,en,idx,val);
    tree[node]=tree[2*node]+tree[2*node+1];
}
int get(int node,int st,int en,int l,int r){
    if(en<l||st>r) return 0;
    if(st>=l&&en<=r) return tree[node];
    int mid=(st+en)>>1;
    return get(2*node,st,mid,l,r)+get(2*node+1,mid+1,en,l,r);
}
int main(){
    LESSGO;
    cin>>n;
    for(int i=0;i<n;++i){
        cin>>a[i];
    }
    cin>>k;
    for(int i=0;i<n;++i){
        a[i]-=k;
        sum+=a[i];
        prefixes.push_back({sum,i});
    }
    compress();
    sum=0;
    for(int i=0;i<n;++i){
        sum+=a[i];
        ans+=get(1,0,n,1,val[i]);
        update(1,0,n,val[i],1);
        if(sum>=0) ++ans;
        
    }
    cout<<ans<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 844 KB Output is correct
2 Correct 4 ms 716 KB Output is correct
3 Correct 3 ms 716 KB Output is correct
4 Correct 852 ms 56328 KB Output is correct
5 Correct 440 ms 39236 KB Output is correct
6 Correct 740 ms 51880 KB Output is correct
7 Correct 716 ms 53296 KB Output is correct
8 Correct 647 ms 48472 KB Output is correct
9 Correct 821 ms 57804 KB Output is correct
10 Correct 713 ms 52344 KB Output is correct