답안 #542472

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
542472 2022-03-26T18:44:25 Z Olympia Vudu (COCI15_vudu) C++17
42 / 140
802 ms 65536 KB
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cmath>
#include <map>
#include <random>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <queue>
#include <limits.h>
using namespace std;
template<class T>
class SegmentTree {
public:

    SegmentTree (int N) {
        N = (1 << ((int)floor(log2(N - 1)) + 1));
        this->N = N;
        val.assign(2 * N, ID);
    }

    void update (int x, T y) {
        x += N - 1;
        val[x] += y;
        while (x != 0) {
            x = (x - 1)/2;
            val[x] = merge(val[2 * x + 1], val[2 * x + 2]);
        }
    }

    T query (int ind, const int l, const int r, int tl, int tr) {
        if (tl >= l && tr <= r) {
            return val[ind];
        }
        if (tr < l || tl > r) {
            return ID;
        }
        return merge(query(2 * ind + 1, l, r, tl, (tl + tr)/2), query(2 * ind + 2, l, r, (tl + tr)/2 + 1, tr));
    }

    T query (int l, int r) {
        return query(0, l, r, 0, N - 1);
    }
private:
    vector<T> val;
    T ID = 0;
    T merge (T x, T y) {
        return x + y;
    }
    int N;
};
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N; cin >> N;
    vector<int64_t> pref = {0}; for (int i = 0; i < N; i++) {int64_t x; cin >> x; pref.push_back(pref.back() + x);}
    set<int> s;
    int64_t P; cin >> P;
    for (int i = 0; i <= N; i++) {
        s.insert(pref[i] - P * i);
    }
    SegmentTree<int64_t> st(N + 1);
    map<int,int> myMap; int cntr = 0;
    for (int i: s) {
        myMap[i] = cntr++;
    }
    int64_t c = 0;
    for (int r = 0; r < N; r++) {
        st.update(myMap[pref[r] - P * r], 1);
        c += st.query(0, myMap[pref[r + 1] - P * (r + 1)]);
    }
    cout << c;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1108 KB Output is correct
2 Correct 4 ms 980 KB Output is correct
3 Correct 4 ms 852 KB Output is correct
4 Runtime error 769 ms 65536 KB Execution killed with signal 9
5 Runtime error 510 ms 65536 KB Execution killed with signal 9
6 Runtime error 717 ms 65536 KB Execution killed with signal 9
7 Runtime error 704 ms 65536 KB Execution killed with signal 9
8 Runtime error 587 ms 65536 KB Execution killed with signal 9
9 Runtime error 802 ms 65536 KB Execution killed with signal 9
10 Runtime error 666 ms 65536 KB Execution killed with signal 9