Submission #631367

# Submission time Handle Problem Language Result Execution time Memory
631367 2022-08-18T03:49:56 Z Spade1 Sails (IOI07_sails) C++14
90 / 100
100 ms 1380 KB
#include<bits/stdc++.h>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
using namespace std;

const int N = 1e5 + 10;
pii s[N];
int fw[N];

int get(int i) {
    int ret = 0;
    for (; i > 0; i -= i&-i) {
        ret += fw[i];
    }
    return ret;
}

void add(int i, int val) {
    for (; i < N; i += i&-i) {
        fw[i] += val;
    }
}

int findl(int x) {
    int l = 1, r = x;
    int target = get(x);
    while (l < r) {
        int mid = (l+r)/2;
        if (get(mid) == target) r = mid;
        else l = mid + 1;
    }
    return l;
}

int findr(int x, int fr) {
    int l = x, r = fr;
    int target = get(x);
    while (l < r) {
        int mid = ceil(1.0*(l+r)/2);
        if (get(mid) == target) l = mid;
        else r = mid - 1;
    }
    return l;
}

void solve() {
    int n; cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> s[i].st >> s[i].nd;
    }
    sort(s+1, s+n+1);
    for (int i = 1; i <= n; ++i) {
        int l = s[i].st - s[i].nd + 1, r = s[i].st;
        int lb = findl(l);
        int rb = findr(l, r);
        if (l == lb) {
            add(l, 1);
            add(r+1, -1);
        }
        else {
            add(rb+1, 1);
            add(r+1, -1);
            add(lb, 1);
            add(lb + (s[i].nd - (r - rb)), -1);
        }
    }

    ll sum = 0;
    for (int i = 1; i < N; ++i) {
        int cur = get(i);
        sum += cur*(cur-1)/2;
    }
    cout << sum << '\n';
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 336 KB Output is correct
2 Correct 3 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 468 KB Output is correct
2 Correct 23 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 1280 KB Output is correct
2 Correct 73 ms 1284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 80 ms 1288 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 100 ms 1380 KB Output is correct
2 Correct 70 ms 1184 KB Output is correct