Submission #776199

# Submission time Handle Problem Language Result Execution time Memory
776199 2023-07-07T11:54:11 Z Ahmed57 Sails (IOI07_sails) C++17
100 / 100
105 ms 2708 KB
#include <bits/stdc++.h>

using namespace std;
//BIT Fenwick tree
long long bit[100005]={0};
void add(int e,int v){
    while(e<=100002){
        bit[e]+=v;
        e+=e&-e;
    }
}
long long sum(int e){
    long long res = 0;
    while(e>=1){
        res+=bit[e];
        e -= e&-e;
    }
    return res;
}
//end BIT
int main(){
    int n;
    cin>>n;
    vector<pair<int,int>> v;
    for(int i =0;i<n;i++){
        int a,b;cin>>a>>b;
        v.push_back({a,b});
    }
    sort(v.begin(),v.end());
    for(int i = 0;i<n;i++){
        int h = v[i].first;
        int k = v[i].second;
        long long diff = h-k+1 , cur = sum(diff);
        int ans = -1 , l = diff , r = h;
        while(l<=r){
            int mid = (l+r)/2;
            if(sum(mid)>=cur){
                ans = mid;
                l = mid+1;
            }else r = mid-1;
        }
        int ans2 = -1 , l2 = 1 , r2 = diff;
        while(l2<=r2){
            int mid = (l2+r2)/2;
            if(sum(mid)<=cur){
                ans2 = mid;
                r2 = mid-1;
            }else l2 = mid+1;
        }
        add(ans+1,1);
        add(h+1,-1);
        add(ans2,1);
        add(ans2+k-(h-ans),-1);
    }
    long long all = 0;
    for(int i = 1;i<=100000;i++){
        long long sz = sum(i);
        all+=sz*(sz-1)/2;
    }
    cout<<all<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 2 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 340 KB Output is correct
# 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 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 324 KB Output is correct
2 Correct 2 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 712 KB Output is correct
2 Correct 24 ms 948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 1088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 1444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 2044 KB Output is correct
2 Correct 70 ms 2708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 2180 KB Output is correct
2 Correct 57 ms 2540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 2192 KB Output is correct
2 Correct 71 ms 2480 KB Output is correct