Submission #474009

# Submission time Handle Problem Language Result Execution time Memory
474009 2021-09-16T15:28:22 Z ljuba Sails (IOI07_sails) C++17
100 / 100
95 ms 2500 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
 
typedef vector<int> vi;
typedef vector<ll> vll;
 
typedef vector<vi> vvi;
typedef vector<vll> vvll;
 
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
typedef vector<pii> vpii;
typedef vector<pll> vpll;
 
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
 
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define fi first
#define se second
 
template<class T> bool ckmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<class T> bool ckmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
 
namespace debug {
    void __print(int x) {cerr << x;}
    void __print(long long x) {cerr << x;}
    void __print(double x) {cerr << x;}
    void __print(long double x) {cerr << x;}
    void __print(char x) {cerr << '\'' << x << '\'';}
    void __print(const string &x) {cerr << '\"' << x << '\"';}
    void __print(const char *x) {cerr << '\"' << x << '\"';}
    void __print(bool x) {cerr << (x ? "true" : "false");}
 
    template<typename T, typename V>
    void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
    template<typename T>
    void __print(const T &x) {int f = 0; cerr << '{'; for(auto z : x) cerr << (f++ ? "," : ""), __print(z); cerr << "}";}
    void _print() {cerr << "]\n";}
    template <typename T, typename... V>
    void _print(T t, V... v) {__print(t); if(sizeof...(v)) cerr << ", "; _print(v...);}
 
#ifdef ljuba
#define dbg(x...) cerr << "\e[91m" << "LINE(" << __LINE__ << ") -> " << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
}
 
using namespace debug;
 
const char nl = '\n';
 
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
 
/*
погледај сампле тестове, пре него што имплементираш (можда имаш погрешну идеју) 
уместо да разматраш неке глупе случајеве на папиру, размишљај заправо о задатку
*/

const int mxN = 1e5 + 12;

struct ft {
    int n;
    int a[mxN+1];
    void update(int i, int x) {
        for(; i <= mxN; i += i&-i) {
            a[i] += x;
        }
    }

    int query(int i) {
        int r = 0;
        for(; i; i -= i&-i) {
            r += a[i];
        }
        return r;
    }

    void dodaj(int x) {
        if(n == x)
            return;
        for(int i = n+1; i <= x; ++i) {
            int vrednost = query(i);
            update(i, -vrednost);
        }
        n = x;
    }
}ft;

template<class T, class U>
T firstTrue(T lo, T hi, U f) {
    T ans = -1;
    while(lo <= hi) {
        T mid = lo + (hi - lo) / 2;
        if(f(mid)) {
            ans = mid;
            hi = mid - 1;
        } else {
            lo = mid + 1;
        }
    }
    return ans;
}

template<class T, class U>
T lastTrue(T lo, T hi, U f) {
    T ans = -1;
    while(lo <= hi) {
        T mid = lo + (hi - lo) / 2;
        if(f(mid)) {
            ans = mid;
            lo = mid + 1;
        } else {
            hi = mid - 1;
        }
    }
    return ans;
}


void solve() {
    int n;
    cin >> n;
    vpii v(n);
    for(auto &z : v)
        cin >> z.fi >> z.se;

    sort(all(v));

    for(int iter = 0; iter < n; ++iter) {
        ft.dodaj(v[iter].fi);
        int k = v[iter].se;
        int pocetak = ft.n-k+1;

        if(ft.query(pocetak-1) >= ft.query(pocetak)+1) {
            ft.update(pocetak, 1);
            ft.update(ft.n+1, -1);
            continue;
        }

        int temp = ft.query(pocetak);
        int poslednjiVrednost = lastTrue(pocetak, ft.n, [&](int i) {
            return ft.query(i) == temp;
        });
        int ima = poslednjiVrednost - pocetak + 1; //koliko ima tih vrednost koje moramo izvan da uvecamo

        int mesto = lastTrue(1, ft.n, [&](int i) {
            return ft.query(i) > temp;
        });
        mesto = max(mesto, 0);
        ++mesto;

        ft.update(mesto, 1);
        ft.update(mesto+ima, -1);

        ft.update(poslednjiVrednost+1, 1);
        ft.update(ft.n+1, -1);
    }

    ll ans = 0;

    for(int i = 1; i <= ft.n; ++i) {
        auto calc = [](ll x) {
            return x * (x-1) / 2;
        };
        ans += calc(ft.query(i));
    }

    cout << ans << nl;
}
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int testCases = 1;
    //cin >> testCases;
    while(testCases--)
        solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 5 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 588 KB Output is correct
2 Correct 22 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 1432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 2156 KB Output is correct
2 Correct 70 ms 2096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 2356 KB Output is correct
2 Correct 56 ms 1988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 2500 KB Output is correct
2 Correct 65 ms 2248 KB Output is correct