답안 #702400

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
702400 2023-02-24T00:21:41 Z thimote75 Sails (IOI07_sails) C++14
50 / 100
458 ms 8500 KB
#include <bits/stdc++.h>

using namespace std;

#define num long long

struct SGD {
    num sum  = 0;
    num size = 1;

    num __l_value = 0;
};

struct SegTree {
    vector<SGD> tree;

    int size, start, height;

    SegTree (int ps) {
        size   = ps;
        height = ceil(log2(size));
        start  = 1 << height;

        tree.resize(start << 1);

        for (int i = start - 1; i >= 1; i --)
            tree[i].size = 2 * tree[i * 2].size;   
    }

    num _value (int index) {
        index += start;
        _update(index);

        return tree[index].sum;
    }
    void _update (int index) {
        if (index == 0) return ;
        _update (index >> 1);

        if (index < start) {
            int n0 = index << 1;
            int n1 = n0 + 1;

            tree[n0].__l_value += tree[index].__l_value;
            tree[n1].__l_value += tree[index].__l_value;
        }

        tree[index].sum += tree[index].size * tree[index].__l_value;
        tree[index].__l_value = 0;
    }

    void _modify (int index, int delta) {
        _update(index);
        tree[index].__l_value += delta;
    }
    void modify (int left, int right, int delta) {
        left  += start;
        right += start;

        while (left < right) {
            if ((left  & 1) == 1) _modify(left,  delta);
            if ((right & 1) == 0) _modify(right, delta);
        
            left  = (left  + 1) >> 1;
            right = (right - 1) >> 1;
        }

        if (left == right) _modify(left, delta);
    }

    num compute () {
        num result = 0;

        for (int index = 0; index < size; index ++) {
            num sum = _value(index);

            result += sum * (sum - 1);
        }

        return result >> 1;
    }

    int lower_bound (num value) { // S[i] < x
        int a = -1;
        int b = size;

        while (b - a > 1) {
            int c = (a + b) >> 1;
            if (_value(c) < value) b = c;
            else a = c;
        }

        return b;
    }
    int upper_bound (num value) { // S[i] <= x <=> S[i] < x + 1
        return lower_bound(value + 1);
    }

    void update_1 (int height, int count) {
        // add 1 to the { count } minimals in range [0, height[

        int start = height - count;
        int end   = height - 1;

        if (start != 0 && _value(start) == _value(start - 1)) {
            int right_start = lower_bound(_value(start));
            if (right_start == size) {
                modify(0, count - 1, 1);
                return ;
            }

            int count_left  = right_start - start;

            int left_start = upper_bound(_value(start));
            int left_end   = left_start + count_left - 1;

            start = right_start;
            modify(left_start, left_end, 1);
        }

        modify(start, end, 1);

        //for (int x = 0; x < size; x ++)
        //    cout << _value(x) << " ";
        //cout << endl;
    }
};

struct Sail {
    int height;
    int count;

    Sail (int ph, int pc) : height(ph), count(pc) {}

    bool operator< (const Sail &other) {
        if (height == other.height) return count < other.count;

        return height < other.height;
    }
};

int main () {
    int maxSailHeight = 0, nbSails = 0;
    cin >> nbSails;

    vector<Sail> sailArray;
    for (int idSail = 0; idSail < nbSails; idSail ++) {
        int h, c;
        cin >> h >> c;

        sailArray.push_back(Sail(h, c));
        maxSailHeight = max(maxSailHeight, h);
    }

    SegTree tree(maxSailHeight);
    sort(sailArray.begin(), sailArray.end());

    for (Sail &sail : sailArray)
        tree.update_1(sail.height, sail.count);

    cout << tree.compute();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 596 KB Output is correct
2 Incorrect 13 ms 6452 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 2004 KB Output is correct
2 Correct 77 ms 1136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 2528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 180 ms 4416 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 365 ms 8040 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 367 ms 8320 KB Output is correct
2 Incorrect 179 ms 7988 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 458 ms 8500 KB Output isn't correct
2 Halted 0 ms 0 KB -