답안 #94545

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
94545 2019-01-20T09:29:43 Z theknife2001 Vudu (COCI15_vudu) C++17
140 / 140
334 ms 45356 KB
#include <bits/stdc++.h>
#define ll long long
#define mid (l+r)/2
#define ii pair < ll , int >
#define se second
#define fi first


using namespace std;
const int N=1e6+55;
vector < ii > vec;
int tree[N];
ll sum[N];
ll a[N];
int n;

bool cmp (ii A, ii B) {
  if (A.first != B.first) return A.first < B.first;
  return A.second > B.second;
}

int query(int x)
{
    if(x<=0)
        return 0;
    return query(x-(x&(-x)))+tree[x];
}

void update(int x)
{
    if(x>n)
        return ;
    tree[x]++;
    update(x+(x&(-x)));
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    int p;
    cin>>p;
    for(int i=n-1;i>=0;i--)
    {
        sum[i]=a[i]-p;
        if(i!=n-1)
            sum[i]+=sum[i+1];
        a[i]=sum[i];
    }
    for(int i=0;i<n;i++)
    {
        vec.push_back({a[i],i+1});
    }
    sort(vec.begin(),vec.end(),cmp);
    int cnt=1;
    ll ans=0;
    for(int i=0;i<n;i++)
    {
//        cout<<vec[i].fi<<' '<<vec[i].se<<endl;
        if(vec[i].fi>=0)
            ans++;
        ans+=(i)-query(vec[i].se);
//        cout<<ans<<endl;
        update(vec[i].se);
    }
    cout<<ans<<endl;
    return 0;
}

Compilation message

vudu.cpp: In function 'int main()':
vudu.cpp:57:9: warning: unused variable 'cnt' [-Wunused-variable]
     int cnt=1;
         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 764 KB Output is correct
2 Correct 3 ms 632 KB Output is correct
3 Correct 3 ms 628 KB Output is correct
4 Correct 334 ms 44012 KB Output is correct
5 Correct 178 ms 30800 KB Output is correct
6 Correct 280 ms 38848 KB Output is correct
7 Correct 285 ms 40460 KB Output is correct
8 Correct 266 ms 36432 KB Output is correct
9 Correct 322 ms 45356 KB Output is correct
10 Correct 285 ms 39484 KB Output is correct