Submission #514934

# Submission time Handle Problem Language Result Execution time Memory
514934 2022-01-18T20:31:34 Z kevin Sails (IOI07_sails) C++17
100 / 100
119 ms 2404 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define ca(v) for(auto i:v) cout<<i<<" ";
#define nl cout<<"\n"

const int MAXN = 5e5 + 5;

struct BIT {
    vector<int> f;
    int n;
    void init(int N) { f = vector<int>(N+1, 0); n = N;}
    int lsb(int x) { return x & -x; }
    void update(int p, int v){ 
        while(p <= n){ f[p] += v; p += lsb(p); } 
    }
    int query(int p) { 
        int sum = 0; 
        while (p > 0) { sum += f[p]; p -= lsb(p); } 
        return sum; 
    }
};

int findup(BIT &bit, int x, int r){
    int l = 1;
    int a = r;
    while(l <= r){
        int m = (l + r) / 2;
        if(bit.query(m) >= x) { a = m; l = m+1; }
        else r = m-1;
    }
    return a;
}
int finddn(BIT &bit, int x, int r){
    int l = 1;
    int a = 1;
    while(l <= r){
        int m = (l + r) / 2;
        if(bit.query(m) <= x) { a = m; r = m-1; }
        else l = m+1;
    }
    return a;
}
int main()
{
    // ios_base::sync_with_stdio(0); cin.tie(0);
    if (fopen("input.in", "r")) freopen("input.in", "r", stdin);
    int n; cin>>n;
    vector<pair<int, int>> ar;
    for(int i=0; i<n; i++){
        int h, k; cin>>h>>k;
        ar.push_back({h, k});
    }
    BIT bit; bit.init(100005);
    sort(ar.begin(), ar.end());
    for(int i=0; i<n; i++){
        // for(int i=1; i<6; i++) cout<<bit.query(i)<<" ";
        // nl;
        int x = bit.query(ar[i].f - ar[i].s + 1);
        int l = finddn(bit, x, ar[i].f);
        int r = findup(bit, x, ar[i].f);
        // cout<<ar[i].f<<" "<<ar[i].s<<" "<<l<<" "<<r<<" "<<x<<"\n";
        bit.update(r+1, 1);
        bit.update(ar[i].f+1, -1);
        bit.update(l, 1);
        bit.update(l + ar[i].s - (ar[i].f-r), -1);
    }
    ll tot = 0;
    for(int i=1; i<=100000; i++){
        int x = bit.query(i);
        tot += (ll)x * (x-1) / 2;
    }
    cout<<tot;
}

Compilation message

sails.cpp: In function 'int main()':
sails.cpp:51:40: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |     if (fopen("input.in", "r")) freopen("input.in", "r", stdin);
      |                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 632 KB Output is correct
2 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 588 KB Output is correct
2 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 688 KB Output is correct
2 Correct 3 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 844 KB Output is correct
2 Correct 28 ms 1232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 1172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 1368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 1564 KB Output is correct
2 Correct 84 ms 2340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 1596 KB Output is correct
2 Correct 89 ms 2108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 1712 KB Output is correct
2 Correct 87 ms 2404 KB Output is correct