답안 #380272

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
380272 2021-03-20T19:22:04 Z vishesh312 Vudu (COCI15_vudu) C++17
140 / 140
776 ms 59196 KB
#include "bits/stdc++.h"
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()

using ll = long long;

const int mod = 1e9+7;
int mx = 1e6+5;
const int segsz = mx*4;

struct SegTree {
    vector<ll> t;
    void update(int v, int tl, int tr, int val) {
        if (tl > val or tr < val) return;
        if (tl == tr) {
            t[v]++;
        } else {
            int tm = (tl + tr) / 2;
            update(v*2, tl, tm, val);
            update(v*2+1, tm+1, tr, val);
            t[v] = t[v*2] + t[v*2+1];
        }
    }
    ll query(int v, int tl, int tr, int l, int r) {
        if (l > tr or r < tl) return 0;
        if (tl >= l and tr <= r) {
            return t[v];
        }
        int tm = (tl + tr) / 2;
        return query(v*2, tl, tm, l, r) + query(v*2+1, tm+1, tr, l, r);
    }
    SegTree() {t.resize(segsz);}
    void update(int n, int val) {update(1, 0, n, val);}
    ll query(int n, int val) {return query(1, 0, n, 0, val);}
} seg;

void solve(int tc) {
    int n;
    cin >> n;
    ll v[n+1];
    for (int i = 1; i <= n; ++i) {
        cin >> v[i];
    }
    int a[n+1];
    ll p;
    cin >> p;
    vector<array<ll, 2>> vx;
    ll sum = 0;
    vx.push_back({0,0});
    for (int i = 1; i <= n; ++i) {
        sum += (v[i] - p);
        vx.push_back({sum, i});
    }
    sort(all(vx));
    ll cur = 0;
    for (int i = 0; i < sz(vx); ++i) {
        if (i) if (vx[i][0] == vx[i-1][0]) --cur;
        a[vx[i][1]] = cur;
        ++cur;
    }
    ll ans = 0;
    seg.update(n, a[0]);
    for (int i = 1; i <= n; ++i) {
        ans += seg.query(n, a[i]);
        seg.update(n, a[i]);
    }
    cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int tc = 1;
    //cin >> tc;
    for (int i = 1; i <= tc; ++i) solve(i);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 32108 KB Output is correct
2 Correct 21 ms 31980 KB Output is correct
3 Correct 21 ms 31980 KB Output is correct
4 Correct 776 ms 58428 KB Output is correct
5 Correct 408 ms 52444 KB Output is correct
6 Correct 702 ms 55356 KB Output is correct
7 Correct 664 ms 56252 KB Output is correct
8 Correct 617 ms 54364 KB Output is correct
9 Correct 758 ms 59196 KB Output is correct
10 Correct 676 ms 55508 KB Output is correct