답안 #999063

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
999063 2024-06-15T05:58:51 Z Alfraganus Sails (IOI07_sails) C++17
100 / 100
271 ms 6120 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define str string
#define endl '\n'
#define all(a) a.begin(), a.end()
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define print(a)          \
    for (auto x : a)      \
        cout << x << ' '; \
    cout << endl;

#define printmp(a)   \
    for (auto x : a) \
        cout << x[0] << ' ' << x[1] << endl;

int N = 1e5 + 5;

struct SegTree {

    int size = 1;
    vector<int> t, ad;
    
    SegTree (){
        while(size < N)
            size <<= 1;
        t.resize(size << 1);
        ad.resize(size << 1);
    }

    void add(int l, int r, int x, int lx, int rx){
        if(l <= lx and rx <= r){
            ad[x] ++;
            return;
        }
        if(r <= lx or rx <= l)
            return;
        int mid = (lx + rx) >> 1;
        add(l, r, (x << 1) + 1, lx, mid);
        add(l, r, (x << 1) + 2, mid, rx);
        t[x]  = t[(x << 1) + 1] + ad[(x << 1) + 1] * (mid - lx) + t[(x << 1) + 2] + ad[(x << 1) + 2] * (rx - mid);
    }

    void add(int l, int r){
        add(l, r, 0, 0, size);
    }

    int get(int i, int x, int lx, int rx){
        if(lx + 1 == rx)
            return ad[x];
        int mid = (lx + rx) >> 1;
        if(i < mid)
            return ad[x] + get(i, (x << 1) + 1, lx, mid);
        return ad[x] + get(i, (x << 1) + 2, mid, rx);
    }

    int get(int x){
        return get(x, 0, 0, size);
    }

    int get(int l, int r, int x, int lx, int rx, int sum = 0){
        sum += ad[x];
        if(l <= lx and rx <= r)
            return t[x] + sum * (rx - lx);
        if(rx <= l or r <= lx)
            return 0;
        int mid = (lx + rx) >> 1;
        return get(l, r, (x << 1) + 1, lx, mid, sum) + get(l, r, (x << 1) + 2, mid, rx, sum);
    }

    int get(int l, int r){
        return get(l, r, 0, 0, size);
    }
};

void solve(){
    int n;
    cin >> n;
    vector<array<int, 2>> a(n);
    for(int i = 0; i < n; i ++)
        cin >> a[i][0] >> a[i][1];
    sort(all(a));
    SegTree s;
    int ans = 0;
    for(int i = 0; i < n; i ++){
        int val = s.get(a[i][0] - a[i][1] + 1);
        int l1 = 1, r1 = a[i][0] - a[i][1] + 1;
        while(l1 < r1){
            int mid = (l1 + r1) >> 1;
            if(s.get(mid) == val)
                r1 = mid;
            else
                l1 = mid + 1;
        }
        int l2 = a[i][0] - a[i][1] + 1, r2 = a[i][0];
        while(l2 < r2){
            int mid = (l2 + r2 + 1) >> 1;
            if(s.get(mid) == val)
                l2 = mid;
            else
                r2 = mid - 1;
        }
        ans += s.get(l2 + 1, a[i][0] + 1);
        s.add(l2 + 1, a[i][0] + 1);
        a[i][1] -= a[i][0] - l2;
        ans += s.get(l1, l1 + a[i][1]);
        s.add(l1, l1 + a[i][1]);
    }
    cout << ans;
}

signed main()
{
    fastio;
    int t = 1;
    // cin >> t;
    while(t --){
        solve();
        cout << endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 3 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4444 KB Output is correct
2 Correct 4 ms 4596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 4684 KB Output is correct
2 Correct 56 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 4952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 229 ms 5720 KB Output is correct
2 Correct 169 ms 5728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 206 ms 5976 KB Output is correct
2 Correct 85 ms 5976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 271 ms 5980 KB Output is correct
2 Correct 141 ms 6120 KB Output is correct