Submission #74691050


Source Code Expand

#pragma once

#include <vector>
#include <cmath>
#include <cstdint>
#include <algorithm>
#include <limits>
#include <functional>

template <typename T>
class SegTree
{
public:
    SegTree(std::uint64_t size) :
        m_baseSize{ (std::uint64_t)1 << (std::uint64_t)std::ceil(std::log2(size)) },
        m_tree(m_baseSize << 1, T{})
    {
    }

    SegTree(const std::vector<T>& elems) :
        m_baseSize{ (std::uint64_t)1 << (std::uint64_t)std::ceil(std::log2(elems.size())) },
        m_tree(m_baseSize << 1, T{})
    {
        for (std::uint64_t i = 0; i < elems.size(); i++)
        {
            m_tree[m_baseSize + i] = elems[i];
        }
        for (std::uint64_t i = m_baseSize - 1; i > 0; i--)
        {
            m_tree[i] = T::calc(
                m_tree[i << 1],
                m_tree[(i << 1) + 1]
            );
        }
    }

    T query(std::uint64_t l, std::uint64_t r) const
    {
        return this->queryRecursive(1, 0, m_baseSize - 1, l, r);
    }

    void update(std::uint64_t pos, const T& val)
    {
        this->updateRecursive(1, 0, m_baseSize - 1, pos, val);
    }

    T getElem(std::uint64_t pos) const
    {
        return m_tree[m_baseSize + pos];
    }

    template <typename U>
    friend class SegTree2d;

private:
    T queryRecursive(std::uint64_t startPos, std::uint64_t lRange, std::uint64_t rRange, std::uint64_t l, std::uint64_t r) const
    {
        if (l <= lRange && rRange <= r)
        {
            return m_tree[startPos];
        }

        if (rRange < l || r < lRange)
        {
            return T{};
        }

        std::uint64_t mid = (lRange + rRange) >> 1;

        return T::calc(
            this->queryRecursive(startPos << 1, lRange, mid, l, r),
            this->queryRecursive((startPos << 1) + 1, mid + 1, rRange, l, r)
        );
    }

    void updateRecursive(std::uint64_t startPos, std::uint64_t lRange, std::uint64_t rRange, std::uint64_t pos, const T& val)
    {
        if (lRange == rRange)
        {
            m_tree[startPos] = val;
            return;
        }

        std::uint64_t mid = (lRange + rRange) >> 1;

        if (pos <= mid)
        {
            this->updateRecursive(startPos << 1, lRange, mid, pos, val);
        }
        else
        {
            this->updateRecursive((startPos << 1) + 1, mid + 1, rRange, pos, val);
        }

        m_tree[startPos] = T::calc(
            m_tree[startPos << 1],
            m_tree[(startPos << 1) + 1]
        );
    }

    std::uint64_t m_baseSize;
    std::vector<T> m_tree;
};

struct Sum
{
    std::int64_t val = 0;

    static Sum calc(const Sum& left, const Sum& right)
    {
        return Sum{ left.val + right.val };
    }
};

#pragma GCC optimize("O3")

#include <iostream>
#include <cmath>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <numeric>
#include <bitset>
#include <cstdint>

using namespace std;

using ll = int64_t;

ll calc(const vector<int>& a, int n, ll k)
{
    if (k <= 0) {
        return (ll)n * (n + 1) / 2;
    }

    SegTree<Sum> freqOnPref(n);

    int r = 0;
    ll invs = 0;
    ll res = 0;
    
    for (int l = 0; l < n; ++l)
    {
        while (r < n && invs < k)
        {
            if (a[r] < n - 1)
            {
                invs += freqOnPref.query(a[r] + 1, n - 1).val;
            }
            freqOnPref.update(a[r], Sum{ 1 });
            ++r;
        }

        if (invs >= k)
        {
            res += n - r + 1;
        }

        if (a[l] > 0)
        {
            invs -= freqOnPref.query(0, a[l] - 1).val;
        }
        freqOnPref.update(a[l], Sum{ 0 });
    }

    return res;
}
void solve()
{
    int n;
    ll k;
    cin >> n >> k;

    vector<int> a(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
        --a[i];
    }

    cout << calc(a, n, k) - calc(a, n, k + 1) << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;

    while (t--)
    {
        solve();
    }

    return 0;
}

Submission Info

Submission Time
Task F - Interval Inversion Count
User RomanLeshchuk
Language C++23 (GCC 15.2.0)
Score 500
Code Size 4324 Byte
Status AC
Exec Time 583 ms
Memory 13868 KiB

Compile Error

./Main.cpp:1:9: warning: '#pragma once' in main file [-Wpragma-once-outside-header]
    1 | #pragma once
      |         ^~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 55
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3760 KiB
00_sample_01.txt AC 1 ms 3744 KiB
00_sample_02.txt AC 1 ms 3852 KiB
01_random_03.txt AC 315 ms 13096 KiB
01_random_04.txt AC 332 ms 13004 KiB
01_random_05.txt AC 165 ms 8504 KiB
01_random_06.txt AC 148 ms 8384 KiB
01_random_07.txt AC 243 ms 8540 KiB
01_random_08.txt AC 171 ms 8380 KiB
01_random_09.txt AC 181 ms 8580 KiB
01_random_10.txt AC 546 ms 13744 KiB
01_random_11.txt AC 551 ms 13816 KiB
01_random_12.txt AC 527 ms 13816 KiB
01_random_13.txt AC 540 ms 13828 KiB
01_random_14.txt AC 562 ms 13712 KiB
01_random_15.txt AC 578 ms 13680 KiB
01_random_16.txt AC 540 ms 13748 KiB
01_random_17.txt AC 583 ms 13824 KiB
01_random_18.txt AC 535 ms 13700 KiB
01_random_19.txt AC 534 ms 13748 KiB
01_random_20.txt AC 510 ms 13700 KiB
01_random_21.txt AC 526 ms 13840 KiB
01_random_22.txt AC 549 ms 13732 KiB
01_random_23.txt AC 552 ms 13716 KiB
01_random_24.txt AC 158 ms 13688 KiB
01_random_25.txt AC 303 ms 13716 KiB
01_random_26.txt AC 306 ms 13868 KiB
01_random_27.txt AC 301 ms 13780 KiB
01_random_28.txt AC 304 ms 13712 KiB
01_random_29.txt AC 308 ms 13772 KiB
01_random_30.txt AC 157 ms 13672 KiB
01_random_31.txt AC 274 ms 13812 KiB
01_random_32.txt AC 271 ms 13692 KiB
01_random_33.txt AC 269 ms 13668 KiB
01_random_34.txt AC 265 ms 13708 KiB
01_random_35.txt AC 274 ms 13752 KiB
01_random_36.txt AC 1 ms 3640 KiB
01_random_37.txt AC 299 ms 13752 KiB
01_random_38.txt AC 299 ms 13716 KiB
01_random_39.txt AC 307 ms 13704 KiB
01_random_40.txt AC 298 ms 13816 KiB
01_random_41.txt AC 301 ms 13816 KiB
01_random_42.txt AC 297 ms 13772 KiB
01_random_43.txt AC 300 ms 13708 KiB
01_random_44.txt AC 299 ms 13816 KiB
01_random_45.txt AC 298 ms 13688 KiB
01_random_46.txt AC 299 ms 13780 KiB
01_random_47.txt AC 300 ms 13776 KiB
01_random_48.txt AC 298 ms 13772 KiB
01_random_49.txt AC 299 ms 13684 KiB
01_random_50.txt AC 267 ms 13532 KiB
01_random_51.txt AC 265 ms 13368 KiB
01_random_52.txt AC 256 ms 13460 KiB
01_random_53.txt AC 260 ms 13496 KiB
01_random_54.txt AC 286 ms 13488 KiB