/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 392.0 KiB
#4 Accepted 1ms 384.0 KiB
#5 Accepted 1ms 468.0 KiB
#6 Accepted 1ms 532.0 KiB
#7 Accepted 1ms 444.0 KiB
#8 Accepted 1ms 444.0 KiB
#9 Accepted 1ms 532.0 KiB
#10 Accepted 1ms 324.0 KiB
#11 Accepted 1ms 532.0 KiB
#12 Accepted 1ms 392.0 KiB
#13 Accepted 1ms 532.0 KiB
#14 Accepted 1ms 320.0 KiB
#15 Accepted 1ms 324.0 KiB
#16 Accepted 1ms 348.0 KiB
#17 Accepted 1ms 564.0 KiB
#18 Accepted 1ms 368.0 KiB
#19 Accepted 1ms 396.0 KiB
#20 Accepted 1ms 532.0 KiB
#21 Accepted 1ms 532.0 KiB
#22 Accepted 1ms 532.0 KiB
#23 Accepted 1ms 532.0 KiB
#24 Accepted 1ms 324.0 KiB
#25 Accepted 1ms 532.0 KiB
#26 Accepted 1ms 320.0 KiB
#27 Accepted 1ms 328.0 KiB
#28 Accepted 1ms 532.0 KiB
#29 Accepted 1ms 532.0 KiB
#30 Accepted 1ms 532.0 KiB
#31 Accepted 1ms 520.0 KiB
#32 Accepted 1ms 352.0 KiB
#33 Accepted 1ms 536.0 KiB
#34 Accepted 1ms 328.0 KiB
#35 Accepted 1ms 396.0 KiB
#36 Accepted 1ms 532.0 KiB
#37 Accepted 1ms 344.0 KiB
#38 Accepted 1ms 532.0 KiB
#39 Accepted 1ms 320.0 KiB
#40 Accepted 1ms 336.0 KiB
#41 Accepted 1ms 532.0 KiB
#42 Accepted 1ms 532.0 KiB
#43 Accepted 1ms 532.0 KiB
#44 Accepted 1ms 432.0 KiB
#45 Accepted 1ms 392.0 KiB
#46 Accepted 1ms 536.0 KiB
#47 Accepted 1ms 564.0 KiB
#48 Accepted 1ms 356.0 KiB
#49 Accepted 1ms 532.0 KiB
#50 Accepted 1ms 536.0 KiB
#51 Accepted 1ms 532.0 KiB
#52 Accepted 1ms 532.0 KiB
#53 Accepted 1ms 576.0 KiB
#54 Accepted 1ms 532.0 KiB
#55 Accepted 1ms 560.0 KiB
#56 Accepted 1ms 508.0 KiB
#57 Accepted 1ms 564.0 KiB
#58 Accepted 1ms 580.0 KiB
#59 Accepted 1ms 580.0 KiB
#60 Accepted 1ms 532.0 KiB
#61 Accepted 2ms 532.0 KiB
#62 Accepted 2ms 580.0 KiB
#63 Accepted 2ms 532.0 KiB
#64 Accepted 5ms 536.0 KiB
#65 Accepted 7ms 532.0 KiB
#66 Accepted 6ms 580.0 KiB
#67 Accepted 6ms 532.0 KiB
#68 Accepted 6ms 532.0 KiB
#69 Accepted 6ms 532.0 KiB
#70 Accepted 6ms 440.0 KiB
#71 Accepted 9ms 532.0 KiB
#72 Accepted 9ms 600.0 KiB
#73 Accepted 8ms 604.0 KiB
#74 Accepted 8ms 488.0 KiB
#75 Accepted 8ms 532.0 KiB
#76 Accepted 8ms 600.0 KiB
#77 Accepted 8ms 532.0 KiB
#78 Accepted 8ms 532.0 KiB
#79 Accepted 8ms 684.0 KiB
#80 Accepted 8ms 580.0 KiB
#81 Accepted 11ms 744.0 KiB
#82 Accepted 9ms 688.0 KiB
#83 Accepted 9ms 532.0 KiB
#84 Accepted 9ms 560.0 KiB
#85 Accepted 5ms 532.0 KiB
#86 Accepted 4ms 532.0 KiB
#87 Accepted 6ms 532.0 KiB
#88 Accepted 7ms 532.0 KiB
#89 Accepted 7ms 600.0 KiB
#90 Accepted 7ms 532.0 KiB
#91 Accepted 43ms 2.02 MiB
#92 Accepted 19ms 1.836 MiB
#93 Accepted 16ms 2.043 MiB
#94 Accepted 23ms 2.02 MiB
#95 Accepted 23ms 2.02 MiB
#96 Accepted 15ms 2.02 MiB
#97 Accepted 19ms 1.938 MiB
#98 Accepted 18ms 1.934 MiB
#99 Accepted 20ms 2.02 MiB
#100 Accepted 19ms 1.938 MiB

Code

using i64 = long long;
using i128 = __int128;
using u32 = unsigned;
using u64 = unsigned long long;
using f32 = double;
using f64 = long double;

#define uset unordered_set
#define umap unordered_map
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<i64>
#define vvll vector<vll>
#define pii pair<int, int>
#define pll pair<i64, i64>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvpii vector<vpii>
#define vvpll vector<vpll>
#define vz vector<Z>
#define vvz vector<vz>
#define pb push_back
#define pq priority_queue
#define ALL(x) (x).begin(), (x).end()
#define rep(i, x, y) for (int (i) = (x); (i) < (y); (i)++)
#define repr(i, x, y) for (int (i) = (x); (i) > (y); (i)--)
#define YES "YES\n"
#define NO "NO\n"
#define SZ(x) (static_cast<int>(x.size()))


#include <bits/stdc++.h>

using namespace std;

mt19937_64 rng((unsigned) chrono::high_resolution_clock::now().time_since_epoch().count());

namespace atcoder {

    namespace internal {
    
    // @param m `1 <= m`
    // @return x mod m
    constexpr long long safe_mod(long long x, long long m) {
        x %= m;
        if (x < 0) x += m;
        return x;
    }
    
    // Fast modular multiplication by barrett reduction
    // Reference: https://en.wikipedia.org/wiki/Barrett_reduction
    // NOTE: reconsider after Ice Lake
    struct barrett {
        unsigned int _m;
        unsigned long long im;
    
        // @param m `1 <= m`
        explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}
    
        // @return m
        unsigned int umod() const { return _m; }
    
        // @param a `0 <= a < m`
        // @param b `0 <= b < m`
        // @return `a * b % m`
        unsigned int mul(unsigned int a, unsigned int b) const {
            // [1] m = 1
            // a = b = im = 0, so okay
    
            // [2] m >= 2
            // im = ceil(2^64 / m)
            // -> im * m = 2^64 + r (0 <= r < m)
            // let z = a*b = c*m + d (0 <= c, d < m)
            // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
            // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
            // ((ab * im) >> 64) == c or c + 1
            unsigned long long z = a;
            z *= b;
    #ifdef _MSC_VER
            unsigned long long x;
            _umul128(z, im, &x);
    #else
            unsigned long long x =
                (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
    #endif
            unsigned long long y = x * _m;
            return (unsigned int)(z - y + (z < y ? _m : 0));
        }
    };
    
    // @param n `0 <= n`
    // @param m `1 <= m`
    // @return `(x ** n) % m`
    constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
        if (m == 1) return 0;
        unsigned int _m = (unsigned int)(m);
        unsigned long long r = 1;
        unsigned long long y = safe_mod(x, m);
        while (n) {
            if (n & 1) r = (r * y) % _m;
            y = (y * y) % _m;
            n >>= 1;
        }
        return r;
    }
    
    // Reference:
    // M. Forisek and J. Jancina,
    // Fast Primality Testing for Integers That Fit into a Machine Word
    // @param n `0 <= n`
    constexpr bool is_prime_constexpr(int n) {
        if (n <= 1) return false;
        if (n == 2 || n == 7 || n == 61) return true;
        if (n % 2 == 0) return false;
        long long d = n - 1;
        while (d % 2 == 0) d /= 2;
        constexpr long long bases[3] = {2, 7, 61};
        for (long long a : bases) {
            long long t = d;
            long long y = pow_mod_constexpr(a, t, n);
            while (t != n - 1 && y != 1 && y != n - 1) {
                y = y * y % n;
                t <<= 1;
            }
            if (y != n - 1 && t % 2 == 0) {
                return false;
            }
        }
        return true;
    }
    template <int n> constexpr bool is_prime = is_prime_constexpr(n);
    
    // @param b `1 <= b`
    // @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
    constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
        a = safe_mod(a, b);
        if (a == 0) return {b, 0};
    
        // Contracts:
        // [1] s - m0 * a = 0 (mod b)
        // [2] t - m1 * a = 0 (mod b)
        // [3] s * |m1| + t * |m0| <= b
        long long s = b, t = a;
        long long m0 = 0, m1 = 1;
    
        while (t) {
            long long u = s / t;
            s -= t * u;
            m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b
    
            // [3]:
            // (s - t * u) * |m1| + t * |m0 - m1 * u|
            // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
            // = s * |m1| + t * |m0| <= b
    
            auto tmp = s;
            s = t;
            t = tmp;
            tmp = m0;
            m0 = m1;
            m1 = tmp;
        }
        // by [3]: |m0| <= b/g
        // by g != b: |m0| < b/g
        if (m0 < 0) m0 += b / s;
        return {s, m0};
    }
    
    // Compile time primitive root
    // @param m must be prime
    // @return primitive root (and minimum in now)
    constexpr int primitive_root_constexpr(int m) {
        if (m == 2) return 1;
        if (m == 167772161) return 3;
        if (m == 469762049) return 3;
        if (m == 754974721) return 11;
        if (m == 998244353) return 3;
        int divs[20] = {};
        divs[0] = 2;
        int cnt = 1;
        int x = (m - 1) / 2;
        while (x % 2 == 0) x /= 2;
        for (int i = 3; (long long)(i)*i <= x; i += 2) {
            if (x % i == 0) {
                divs[cnt++] = i;
                while (x % i == 0) {
                    x /= i;
                }
            }
        }
        if (x > 1) {
            divs[cnt++] = x;
        }
        for (int g = 2;; g++) {
            bool ok = true;
            for (int i = 0; i < cnt; i++) {
                if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                    ok = false;
                    break;
                }
            }
            if (ok) return g;
        }
    }
    template <int m> constexpr int primitive_root = primitive_root_constexpr(m);
    
    // @param n `n < 2^32`
    // @param m `1 <= m < 2^32`
    // @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
    unsigned long long floor_sum_unsigned(unsigned long long n,
                                          unsigned long long m,
                                          unsigned long long a,
                                          unsigned long long b) {
        unsigned long long ans = 0;
        while (true) {
            if (a >= m) {
                ans += n * (n - 1) / 2 * (a / m);
                a %= m;
            }
            if (b >= m) {
                ans += n * (b / m);
                b %= m;
            }
    
            unsigned long long y_max = a * n + b;
            if (y_max < m) break;
            // y_max < m * (n + 1)
            // floor(y_max / m) <= n
            n = (unsigned long long)(y_max / m);
            b = (unsigned long long)(y_max % m);
            std::swap(m, a);
        }
        return ans;
    }
    
    }  // namespace internal
    
    }  // namespace atcoder
    
    
    #if __cplusplus >= 202002L
    #include <bit>
    #endif
    
    namespace atcoder {
    
    namespace internal {
    
    #if __cplusplus >= 202002L
    
    using std::bit_ceil;
    
    #else
    
    // @return same with std::bit::bit_ceil
    unsigned int bit_ceil(unsigned int n) {
        unsigned int x = 1;
        while (x < (unsigned int)(n)) x *= 2;
        return x;
    }
    
    #endif
    
    // @param n `1 <= n`
    // @return same with std::bit::countr_zero
    int countr_zero(unsigned int n) {
    #ifdef _MSC_VER
        unsigned long index;
        _BitScanForward(&index, n);
        return index;
    #else
        return __builtin_ctz(n);
    #endif
    }
    
    // @param n `1 <= n`
    // @return same with std::bit::countr_zero
    constexpr int countr_zero_constexpr(unsigned int n) {
        int x = 0;
        while (!(n & (1 << x))) x++;
        return x;
    }
    
    }  // namespace internal
    
    }  // namespace atcoder
    
    namespace atcoder {
    namespace internal {
    
    template <class E> struct csr {
        std::vector<int> start;
        std::vector<E> elist;
        explicit csr(int n, const std::vector<std::pair<int, E>>& edges)
            : start(n + 1), elist(edges.size()) {
            for (auto e : edges) {
                start[e.first + 1]++;
            }
            for (int i = 1; i <= n; i++) {
                start[i] += start[i - 1];
            }
            auto counter = start;
            for (auto e : edges) {
                elist[counter[e.first]++] = e.second;
            }
        }
    };
    
    }  // namespace internal
    
    }  // namespace atcoder
    
    namespace atcoder {
    
    namespace internal {
    
    template <class T> struct simple_queue {
        std::vector<T> payload;
        int pos = 0;
        void reserve(int n) { payload.reserve(n); }
        int size() const { return int(payload.size()) - pos; }
        bool empty() const { return pos == int(payload.size()); }
        void push(const T& t) { payload.push_back(t); }
        T& front() { return payload[pos]; }
        void clear() {
            payload.clear();
            pos = 0;
        }
        void pop() { pos++; }
    };
    
    }  // namespace internal
    
    }  // namespace atcoder
    
    namespace atcoder {
    
    namespace internal {
    
    #ifndef _MSC_VER
    template <class T>
    using is_signed_int128 =
        typename std::conditional<std::is_same<T, __int128_t>::value ||
                                      std::is_same<T, __int128>::value,
                                  std::true_type,
                                  std::false_type>::type;
    
    template <class T>
    using is_unsigned_int128 =
        typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                      std::is_same<T, unsigned __int128>::value,
                                  std::true_type,
                                  std::false_type>::type;
    
    template <class T>
    using make_unsigned_int128 =
        typename std::conditional<std::is_same<T, __int128_t>::value,
                                  __uint128_t,
                                  unsigned __int128>;
    
    template <class T>
    using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                      is_signed_int128<T>::value ||
                                                      is_unsigned_int128<T>::value,
                                                  std::true_type,
                                                  std::false_type>::type;
    
    template <class T>
    using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                     std::is_signed<T>::value) ||
                                                        is_signed_int128<T>::value,
                                                    std::true_type,
                                                    std::false_type>::type;
    
    template <class T>
    using is_unsigned_int =
        typename std::conditional<(is_integral<T>::value &&
                                   std::is_unsigned<T>::value) ||
                                      is_unsigned_int128<T>::value,
                                  std::true_type,
                                  std::false_type>::type;
    
    template <class T>
    using to_unsigned = typename std::conditional<
        is_signed_int128<T>::value,
        make_unsigned_int128<T>,
        typename std::conditional<std::is_signed<T>::value,
                                  std::make_unsigned<T>,
                                  std::common_type<T>>::type>::type;
    
    #else
    
    template <class T> using is_integral = typename std::is_integral<T>;
    
    template <class T>
    using is_signed_int =
        typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                                  std::true_type,
                                  std::false_type>::type;
    
    template <class T>
    using is_unsigned_int =
        typename std::conditional<is_integral<T>::value &&
                                      std::is_unsigned<T>::value,
                                  std::true_type,
                                  std::false_type>::type;
    
    template <class T>
    using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                                  std::make_unsigned<T>,
                                                  std::common_type<T>>::type;
    
    #endif
    
    template <class T>
    using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
    
    template <class T>
    using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
    
    template <class T> using to_unsigned_t = typename to_unsigned<T>::type;
    
    }  // namespace internal
    
    }  // namespace atcoder

namespace atcoder {

// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
template <class T> struct fenwick_tree {
    using U = internal::to_unsigned_t<T>;

    public:
    fenwick_tree() : _n(0) {}
    explicit fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

    private:
    int _n;
    std::vector<U> data;

    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

}  // namespace atcoder

void solve() {
    int n;
    cin >> n;
    vi a(n);
    rep(i, 0, n) cin >> a[i];
    vi ans(n);
    atcoder::fenwick_tree<int> ftr(n), ftl(n);
    rep(i, 0, n) ftr.add(a[i], 1);
    rep(i, 0, n) {
        ftr.add(a[i], -1);
        if (ftr.sum(a[i], n) >= a[i] || ftl.sum(0, a[i] + 1) >= a[i]) ans[i] = 1;
        ftl.add(a[i], 1);
    }
    cout << accumulate(ALL(ans), 0) << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    while (t--) solve();
}

Information

Submit By
Type
Submission
Problem
P1184 The Curious Kid and the Number Game
Contest
Brain Booster #9
Language
C++17 (G++ 13.2.0)
Submit At
2025-04-06 15:40:57
Judged At
2025-04-06 15:40:57
Judged By
Score
100
Total Time
43ms
Peak Memory
2.043 MiB