Submission #146708

# Submission time Handle Problem Language Result Execution time Memory
146708 2019-08-25T12:45:01 Z dolphingarlic Sails (IOI07_sails) C++14
100 / 100
224 ms 3960 KB
#include <bits/stdc++.h>
#pragma GCC Optimize("O3")
#define FOR(i, x, y) for (ll i = x; i < y; i++)
typedef long long ll;
using namespace std;

ll MAXH;
ll bit1[100001], bit2[100001];
pair<ll, ll> a[100001];

void update(ll l, ll r, ll val) {
    for (ll i = l; i <= MAXH; i += (i & (-i))) bit1[i] += val;
    for (ll i = r + 1; i <= MAXH; i += (i & (-i))) bit1[i] -= val;

    for (ll i = l; i <= MAXH; i += (i & (-i))) bit2[i] += val * (l - 1);
    for (ll i = r + 1; i <= MAXH; i += (i & (-i)))  bit2[i] -= val * r;
}
ll query(ll l, ll r) {
    ll ans = 0;
    for (ll i = r; i; i -= (i & (-i))) ans += bit1[i] * r;
    for (ll i = l - 1; i; i -= (i & (-i))) ans -= bit1[i] * (l - 1);

    for (ll i = r; i; i -= (i & (-i))) ans -= bit2[i];
    for (ll i = l - 1; i; i -= (i & (-i))) ans += bit2[i];
    return ans;
}

int main() {
    iostream::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin >> n;
    FOR(i, 0, n) cin >> a[i].first >> a[i].second;
    sort(a, a + n);
    MAXH = a[n - 1].first;

    ll ans = 0;
    FOR(i, 0, n) {
        ll ub = query(a[i].first - a[i].second + 1, a[i].first - a[i].second + 1);

        ll l = a[i].first - a[i].second + 1, r = a[i].first + 1;
        while (l != r) {
            ll mid = (l + r) / 2;
            if (query(mid, mid) < ub) r = mid;
            else l = mid + 1;
        }

        ll pos = l;
        ans += query(pos, a[i].first);
        update(pos, a[i].first, 1);

        l = 1, r = a[i].first;
        while (l != r) {
            ll mid = (l + r) / 2;
            if (query(mid, mid) > ub) l = mid + 1;
            else r = mid;
        }

        ans += query(l, l + a[i].second - a[i].first + pos - 2);
        update(l, l + a[i].second - a[i].first + pos - 2, 1);
    }

    cout << ans;
    return 0;
}

Compilation message

sails.cpp:2:0: warning: ignoring #pragma GCC Optimize [-Wunknown-pragmas]
 #pragma GCC Optimize("O3")
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 6 ms 1912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 892 KB Output is correct
2 Correct 40 ms 1192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 1272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 1900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 180 ms 3200 KB Output is correct
2 Correct 156 ms 3960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 170 ms 3312 KB Output is correct
2 Correct 105 ms 3844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 224 ms 3484 KB Output is correct
2 Correct 136 ms 3456 KB Output is correct