Submission #593474

# Submission time Handle Problem Language Result Execution time Memory
593474 2022-07-11T08:52:12 Z thezomb1e Sails (IOI07_sails) C++17
100 / 100
803 ms 6356 KB
//thatsramen

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define eb emplace_back
#define pb push_back
#define ft first
#define sd second
#define pi pair<int, int>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(...) dbg_out(__VA_ARGS__)

using ll = long long;
using ld = long double;
using namespace std;
using namespace __gnu_pbds;

//Constants
const ll INF = 5 * 1e18;
const int IINF = 2 * 1e9;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ld PI = 3.14159265359;

//Templates
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) {return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '['; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << ']';}
void dbg_out() {cerr << endl;}
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ' '; dbg_out(T...); }
template<typename T> void mins(T& x, T y) {x = min(x, y);}
template<typename T> void maxs(T& x, T y) {x = max(x, y);}
template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

//order_of_key(k): number of elements strictly less than k
//find_by_order(k): k-th element in the set

void setPrec() {cout << fixed << setprecision(15);}
void unsyncIO() {cin.tie(0)->sync_with_stdio(0);}
void setIn(string s) {freopen(s.c_str(), "r", stdin);}
void setOut(string s) {freopen(s.c_str(), "w", stdout);}
void setIO(string s = "") {
    unsyncIO(); setPrec();
    if(s.size()) setIn(s + ".in"), setOut(s + ".out");
}

// #define TEST_CASES

struct SegmentTree {

    struct Node {
        ll val, modi;
    };

    vector<Node> tre;
    const ll NO_OPERATION = 0;
    const ll NEUTRAL = 0;
    int sz;

    SegmentTree(vector<int> &a) {
        int n = (int) a.size();
        sz = 1;
        while (sz < n) sz *= 2;
        tre.assign(2 * sz, {NEUTRAL, NO_OPERATION});
        build(a, 1, 0, sz);
    }

    SegmentTree(int n) {
        sz = 1;
        while (sz < n) sz *= 2;
        tre.assign(2 * sz, {NEUTRAL, NO_OPERATION});
    }

    ll op_modify(ll x, ll y) {
        return x + y;
    }

    ll op_modify2(ll x, ll y, int lx, int rx) {
        return x + (rx - lx) * y;
    }

    ll op_calc(ll x, ll y) {
        return x + y;
    }

    void propagate(int x, int lx, int rx) {
        if (rx - lx == 1 || tre[x].modi == NO_OPERATION) return;
        int m = (lx + rx) / 2;
        tre[2 * x].modi     = op_modify(tre[2 * x].modi, tre[x].modi);
        tre[2 * x].val      = op_modify2(tre[2 * x].val, tre[x].modi, lx, m);
        tre[2 * x + 1].modi = op_modify(tre[2 * x + 1].modi, tre[x].modi);
        tre[2 * x + 1].val  = op_modify2(tre[2 * x + 1].val, tre[x].modi, m, rx);
        tre[x].modi = NO_OPERATION;
    }

    void build(vector<int> &a, int x, int lx, int rx) {
        if (rx - lx == 1) {
            if (lx < a.size()) {
                tre[x].val = a[lx];
            }
            return;
        }
        int m = (lx + rx) / 2;
        build(a, 2 * x, lx, m);
        build(a, 2 * x + 1, m, rx);
        tre[x].val = op_calc(tre[2 * x].val, tre[2 * x + 1].val);
    }

    void set(int l, int r, int v, int x, int lx, int rx) {
        propagate(x, lx, rx);
        if (rx <= l || lx >= r) {
            return;
        }
        if (l <= lx && r >= rx) {
            if (tre[x].modi == NO_OPERATION) {
                tre[x].modi = v;
            } else {
                tre[x].modi = op_modify(tre[x].modi, v);
            }
            tre[x].val = op_modify2(tre[x].val, v, lx, rx);
            return;
        }
        int m = (lx + rx) / 2;
        set(l, r, v, 2 * x, lx, m);
        set(l, r, v, 2 * x + 1, m, rx);
        tre[x].val = op_calc(tre[2 * x].val, tre[2 * x + 1].val);
    }

    void set(int l, int r, int v) {
        set(l, r, v, 1, 0, sz);
    }

    ll get(int l, int r, int x, int lx, int rx) {
        propagate(x, lx, rx);
        if (rx <= l || lx >= r) {
            return NEUTRAL;
        }
        if (l <= lx && r >= rx) {
            return tre[x].val;
        }
        int m = (lx + rx) / 2;
        ll s1 = get(l, r, 2 * x, lx, m);
        ll s2 = get(l, r, 2 * x + 1, m, rx);
        return op_calc(s1, s2);
    }

    ll get(int l, int r) {
        return get(l, r, 1, 0, sz);
    }
};

void solve() {
    int n;
    cin >> n;
    vector<pi> al(n);
    for (int i = 0; i < n; i++) {
        cin >> al[i].ft >> al[i].sd;
    }
    sort(all(al));
    SegmentTree st(100100);
    for (int i = 0; i < n; i++) {
        int h = al[i].ft, k = al[i].sd;
        int val = st.get(h - k + 1, h - k + 2);
        int left, right;
        {
            int lo = 1, hi = h;
            while (lo <= hi) {
                int mi = (lo + hi) / 2;
                if (st.get(mi, mi + 1) >= val) {
                    right = mi;
                    lo = mi + 1;
                } else {
                    hi = mi - 1;
                }
            }
        }
        {
            int lo = 1, hi = h;
            while (lo <= hi) {
                int mi = (lo + hi) / 2;
                if (st.get(mi, mi + 1) <= val) {
                    left = mi;
                    hi = mi - 1;
                } else {
                    lo = mi + 1;
                }
            }
        }
        int take = right - h + k;
        st.set(left, left + take, 1);
        st.set(right + 1, h + 1, 1);
    }
    ll ans = 0;
    for (int i = 1; i <= 100000; i++) {
        int val = st.get(i, i + 1);
        ans += 1ll * val * (val - 1) / 2;
    }
    cout << ans;
}

int main() {
    setIO();

    int tt = 1;
    #ifdef TEST_CASES
        cin >> tt;
    #endif

    while (tt--)
        solve();

    return 0;
}

Compilation message

sails.cpp: In member function 'void SegmentTree::build(std::vector<int>&, int, int, int)':
sails.cpp:102:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |             if (lx < a.size()) {
      |                 ~~~^~~~~~~~~~
sails.cpp: In function 'void setIn(std::string)':
sails.cpp:44:30: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 | void setIn(string s) {freopen(s.c_str(), "r", stdin);}
      |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
sails.cpp: In function 'void setOut(std::string)':
sails.cpp:45:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 | void setOut(string s) {freopen(s.c_str(), "w", stdout);}
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sails.cpp: In function 'void solve()':
sails.cpp:195:15: warning: 'right' may be used uninitialized in this function [-Wmaybe-uninitialized]
  195 |         st.set(right + 1, h + 1, 1);
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~
sails.cpp:194:15: warning: 'left' may be used uninitialized in this function [-Wmaybe-uninitialized]
  194 |         st.set(left, left + take, 1);
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 4460 KB Output is correct
2 Correct 15 ms 4436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 4436 KB Output is correct
2 Correct 14 ms 4424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 4436 KB Output is correct
2 Correct 16 ms 4416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 4420 KB Output is correct
2 Correct 20 ms 4436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 4436 KB Output is correct
2 Correct 28 ms 4432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 4556 KB Output is correct
2 Correct 171 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 167 ms 5008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 412 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 698 ms 5892 KB Output is correct
2 Correct 621 ms 5928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 678 ms 6164 KB Output is correct
2 Correct 523 ms 5840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 803 ms 6356 KB Output is correct
2 Correct 561 ms 6256 KB Output is correct