Submission #87417

# Submission time Handle Problem Language Result Execution time Memory
87417 2018-11-30T18:52:40 Z nikolapesic2802 Vudu (COCI15_vudu) C++14
140 / 140
410 ms 34336 KB
/*
    - Subtract p from every element, and calculate prefix sums.
    - We need to map every prefix sum value to a new value in range [1,n] and use fenwick tree to calculate the number of left borders that are good for every right border
*/
#include <bits/stdc++.h>

#define ll long long
#define pb push_back

using namespace std;

const int N=1e6+5;
vector<ll> pre(N);
vector<int> fen(N),mapa(N);
void update(int i){
    for(;i<N;i+=i&(-i))
        fen[i]++;
}
int get(int i){
    int sum=0;
    for(;i;i-=i&(-i))
        sum+=fen[i];
    return sum;
}
int main()
{
    int n,p;
    scanf("%i",&n);
    for(int i=1;i<=n;i++)
        scanf("%lld",&pre[i]);
    scanf("%i",&p);
    vector<pair<ll,int> > poz;
    for(int i=1;i<=n;i++)
    {
        pre[i]+=pre[i-1]-p;
        poz.pb({pre[i],i});
    }
    poz.pb({0,0});
    sort(poz.begin(),poz.end());
    for(int i=0;i<=n;i++)
        mapa[poz[i].second]=i+1;
    ll ans=0;
    update(mapa[0]);
    for(int i=1;i<=n;i++)
    {
        ans+=get(mapa[i]);
        update(mapa[i]);
    }
    printf("%lld\n",ans);
    return 0;
}

Compilation message

vudu.cpp: In function 'int main()':
vudu.cpp:28:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&n);
     ~~~~~^~~~~~~~~
vudu.cpp:30:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld",&pre[i]);
         ~~~~~^~~~~~~~~~~~~~~~
vudu.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&p);
     ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 16248 KB Output is correct
2 Correct 16 ms 16432 KB Output is correct
3 Correct 18 ms 16432 KB Output is correct
4 Correct 376 ms 34312 KB Output is correct
5 Correct 236 ms 34312 KB Output is correct
6 Correct 318 ms 34312 KB Output is correct
7 Correct 410 ms 34324 KB Output is correct
8 Correct 299 ms 34324 KB Output is correct
9 Correct 389 ms 34336 KB Output is correct
10 Correct 354 ms 34336 KB Output is correct