제출 #75376

#제출 시각아이디문제언어결과실행 시간메모리
75376damien_gVudu (COCI15_vudu)C++14
42 / 140
1081 ms66560 KiB
#include <cstdio>
#include <set>
#include <map>

using namespace std;

typedef long long ll;

const ll INF = 1LL << 60;

struct Node
{
    int nb;
    Node *left, *right;
    
    Node() {};
    Node(int _nb, Node* _left, Node* _right): nb(_nb), left(_left), right(_right) {};
};

Node* root;

void add(Node *act, int value, int l = 0, int r = (1 << 20))
{
    if(r != l)
    {
        if(value <= (l+r)/2)
        {
            if(!act->left)
                act->left = new Node(0, NULL, NULL);
            add(act->left, value, l, (l+r)/2);
        }
        else
        {
            if(!act->right)
                act->right = new Node(0, NULL, NULL);
            add(act->right, value, (l+r)/2+1, r);
        }
    }
    act->nb++;
}

ll rsq(Node* node, int l, int r, int nl = 0, int nr = (1 << 20))
{
    if((r < nl) || (nr < l))
        return 0;
    
    if((l <= nl) && (nr <= r))
        return node->nb;
    
    ll a = (node->left)?rsq(node->left, l, r, nl, (nl+nr)/2):0;
    ll b = (node->right)?rsq(node->right, l, r, (nl+nr)/2+1, nr):0;
    return a+b;
}

int N;
ll a[1 << 20];
ll P;

int main()
{
    scanf("%d", &N);
    for(int i = 0; i < N; i++)
        scanf("%lld", &a[i]);
    scanf("%lld", &P);
    
    for(int i = 0; i < N; i++)
        a[i] -= P;
    
    root = new Node(0, NULL, NULL);
    
    ll ans = 0, S = 0;
    set<ll> sums;
    sums.insert(0);
    for(int i = 0; i < N; i++)
    {
        S += a[i];
        sums.insert(S);
    }
    
    int k = 0;
    map<ll, int> hash;
    for(ll u : sums)
        hash[u] = k++;
    
    sums.clear();
    add(root, hash[0]);
    
    S = 0;
    for(int i = 0; i < N; i++)
    {
        S += a[i];
        ans += rsq(root, 0, hash[S]);
        add(root, hash[S]);
    }
    
    printf("%lld\n", ans);
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

vudu.cpp: In function 'int main()':
vudu.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
vudu.cpp:63:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld", &a[i]);
         ~~~~~^~~~~~~~~~~~~~~
vudu.cpp:64:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld", &P);
     ~~~~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...