답안 #165508

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
165508 2019-11-27T11:46:21 Z egekabas Vudu (COCI15_vudu) C++14
140 / 140
686 ms 31840 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long   ll;
typedef unsigned long long   ull;
typedef long double ld;
typedef pair<ll, ll>    pll;
typedef pair<ull, ull>    pull;
typedef pair<ll, ll>  pii;
typedef pair<ld, ld>  pld;
ll n, p;
ll a[1000009];
ll bit[2000009];
vector<ll> v;
void upd(ll idx){
    while(idx > 0){
        bit[idx]++;
        idx -= idx&(-idx);
    }
}
ll get(ll idx){
    ll ret = 0;
    while(idx <= v.size()+2){
        ret += bit[idx];
        idx += idx&(-idx);
    }
    return ret;
}
int main() {
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    scanf("%lld", &n);
    for(ll i = 0; i < n; ++i){
        scanf("%lld", &a[i]);
    }
    scanf("%lld", &p);
    for(ll i = 0; i < n; ++i){
        a[i] -= p;
    }
    
    ll add = 0;
    for(ll i = 0; i < n; ++i){
        v.pb(-a[i]+add);    
        add -= a[i];
        v.pb(a[i]+add);    
    }
    sort(v.begin(), v.end());
    v.resize(unique(v.begin(), v.end())-v.begin());
    ll ans = 0;
    add = 0;
    for(ll i = 0; i < n; ++i){
        if(a[i] >= 0){
            ++ans;
        }
        ans += get(1+lower_bound(v.begin(), v.end(), -a[i]+add)-v.begin());
        add -= a[i];
        upd(1+lower_bound(v.begin(), v.end(), a[i]+add)-v.begin());
    }
    printf("%lld", ans);
}

Compilation message

vudu.cpp: In function 'll get(ll)':
vudu.cpp:26:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(idx <= v.size()+2){
           ~~~~^~~~~~~~~~~~~
vudu.cpp: In function 'int main()':
vudu.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld", &n);
     ~~~~~^~~~~~~~~~~~
vudu.cpp:39:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld", &a[i]);
         ~~~~~^~~~~~~~~~~~~~~
vudu.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld", &p);
     ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 632 KB Output is correct
2 Correct 5 ms 632 KB Output is correct
3 Correct 5 ms 632 KB Output is correct
4 Correct 686 ms 30672 KB Output is correct
5 Correct 381 ms 21152 KB Output is correct
6 Correct 606 ms 27324 KB Output is correct
7 Correct 605 ms 28476 KB Output is correct
8 Correct 513 ms 24636 KB Output is correct
9 Correct 670 ms 31840 KB Output is correct
10 Correct 574 ms 27708 KB Output is correct