답안 #336123

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
336123 2020-12-14T18:55:59 Z smax Sails (IOI07_sails) C++17
100 / 100
209 ms 8684 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__);
#else
#define DEBUG(...) 6;
#endif

template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << s << " = " << x << "\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {cerr << s.substr(0, s.find(',')) << " = " << x << " | "; debug(s.substr(s.find(',') + 2), args...);}

struct Node {
    int ans = 0, lazy = 0, l, r;

    void leaf(int val) {
        ans += val;
    }

    void pull(Node &a, Node &b) {
        ans = min(a.ans, b.ans);
    }

    void push(int val) {
        lazy += val;
    }

    void apply() {
        ans += lazy;
        lazy = 0;
    }
};

struct SegmentTree {
    int n;
    vector<int> a;
    vector<Node> st;

    SegmentTree(int _n) : n(_n), st(4*n) {
        build(1, 0, n-1);
    }

    SegmentTree(const vector<int> &_a) : n(_a.size()), a(_a), st(4*n) {
        build(1, 0, n-1);
    }

    void build(int p, int l, int r) {
        st[p].l = l;
        st[p].r = r;
        if (l == r) {
            return;
        }

        int m = (l + r) / 2;
        build(2*p, l, m);
        build(2*p+1, m+1, r);
        st[p].pull(st[2*p], st[2*p+1]);
    }

    void push(int p) {
        if (st[p].lazy) {
            if (st[p].l != st[p].r) {
                st[2*p].push(st[p].lazy);
                st[2*p+1].push(st[p].lazy);
            }
            st[p].apply();
        }
    }

    Node query(int p, int i, int j) {
        push(p);

        if (st[p].l == i && st[p].r == j)
            return st[p];

        int m = (st[p].l + st[p].r) / 2;
        if (j <= m)
            return query(2*p, i, j);
        else if (i > m)
            return query(2*p+1, i, j);
        Node ret, ls = query(2*p, i, m), rs = query(2*p+1, m+1, j);
        ret.pull(ls, rs);
        return ret;
    }

    int query(int i, int j) {
        return query(1, i, j).ans;
    }

    void update(int p, int i, int j, int val) {
        if (st[p].l == i && st[p].r == j) {
            st[p].push(val);
            push(p);
            return;
        }

        push(p);

        int m = (st[p].l + st[p].r) / 2;
        if (j <= m) {
            update(2*p, i, j, val);
            push(2*p+1);
        } else if (i > m) {
            push(2*p);
            update(2*p+1, i, j, val);
        } else {
            update(2*p, i, m, val);
            update(2*p+1, m+1, j, val);
        }
        st[p].pull(st[2*p], st[2*p+1]);
    }

    void update(int i, int j, int val) {
        update(1, i, j, val);
    }

    int leftMost(int p, int x) {
        if (st[p].l == st[p].r) {
            if (st[p].ans > x)
                return st[p].l + 1;
            return st[p].l;
        }
        push(2*p);
        push(2*p+1);
        if (st[2*p].ans <= x)
            return leftMost(2*p, x);
        return leftMost(2*p+1, x);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n;
    vector<pair<int, int>> v(n);
    for (int i=0; i<n; i++)
        cin >> v[i].first >> v[i].second;

    sort(v.begin(), v.end());
    SegmentTree st(100000);
    for (auto [h, k] : v) {
        int val = st.query(h - k, h - k);
        if (k < h) {
            int nxt = st.query(h - k - 1, h - k - 1);
            if (nxt == val) {
                int first = st.leftMost(1, val - 1), l = st.leftMost(1, val), len = max(h - first, 0);
                if (len > 0)
                    st.update(first, h - 1, 1);
                st.update(l, l + k - len - 1, 1);
            } else {
                st.update(h - k, h - 1, 1);
            }
        } else {
            st.update(h - k, h - 1, 1);
        }
    }

    long long ret = 0;
    for (int i=0; i<100000; i++) {
        long long cur = st.query(i, i);
        ret += cur * (cur - 1) / 2;
    }
    cout << ret << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 6636 KB Output is correct
2 Correct 14 ms 6636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 6636 KB Output is correct
2 Correct 14 ms 6636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 6636 KB Output is correct
2 Correct 14 ms 6636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 6636 KB Output is correct
2 Correct 15 ms 6636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 6636 KB Output is correct
2 Correct 16 ms 6636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 6764 KB Output is correct
2 Correct 59 ms 7148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 7148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 7532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 189 ms 8044 KB Output is correct
2 Correct 138 ms 8172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 180 ms 8300 KB Output is correct
2 Correct 89 ms 8080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 209 ms 8684 KB Output is correct
2 Correct 133 ms 8500 KB Output is correct