Submission #74662921


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define se second
#define fi first
#define pb push_back
#define pf push_front
#define ld long double
#define INF 1e18
#define lcm(x, y) x / __gcd(x, y) * y
#define Pair pair<ll, ll>
#define pii pair<int, int>
#define Pq priority_queue<ll>
#define Pqr priority_queue<ll, vector<ll>, greater<ll >>
#define clockst clock_t tStart = clock()
#define clockfin printf("Time taken: %.2fs\n",(double)(clock() - tStart)/CLOCKS_PER_SEC)
#define all(x) x.begin(), x.end()
#define sz(x)((ll)(x).size())
#define eb emplace_back
#define mp make_pair
#define mems(a, x) memset((a),(x), sizeof(a))
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,popcnt,lzcnt,bmi,bmi2,fma")
typedef unsigned long long ull;
void init() { freopen("cf.INP", "r", stdin); freopen("cf.OUT", "w", stdout); }
ll xc[5] = { -1, 1, 0, 0 }, yc[5] = { 0, 0, 1, -1 };
ll xc8[8] = { -1, 1, 0, 0, -1, 1, -1, 1 };
ll yc8[8] = { 0, 0, 1, -1, -1, 1, 1, -1 };
const long double pi = acosl(-1.0L);
template<class T1, class T2> bool cmax(T1 &x, const T2 &y) { if(x < y) { x = y; return 1; } return 0; }

inline ll read()
{
    ll x = 0;
    char ch = getchar();;
    bool str = 1;
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') str = 0; ch = getchar();;
    }
    while(ch >= '0' && ch <= '9')
    {
        x =((x << 3) +(x << 1)) + ch - '0';
        ch = getchar();;
    }
    return str ? x : x;
}

struct fenwick {
    ll n;
    vector<ll> bit;
    fenwick(ll x) : n(x), bit(x + 2, 0) {}
    void add(ll i, ll delta)
    {
        for (; i <= n; i += i & -i) bit[i] += delta;
    }
    ll sum(ll i)
    {
        ll s = 0;
        for (; i > 0; i -= i & -i) s += bit[i];
        return s;
    }
    ll query(ll i)
    {
        return sum(i);
    }
};

void solve()
{
    ll n, k;
    cin >> n >> k;
    vector<ll> p(n + 1);
    for (ll i = 1; i <= n; i++) cin >> p[i];
    fenwick blow(n), bhigh(n);
    ll rl = 0, rh = 0, invl = 0, invh = 0, ans = 0;
    for (ll l = 1; l <= n; l++)
    {
        while (rl < n && invl < k)
        {
            rl++;
            ll val = p[rl];
            ll total = rl - l;
            ll it = blow.sum(val - 1);
            invl += total - it;
            blow.add(val, 1);
        }
        while (rh < n && invh <= k)
        {
            rh++;
            ll val = p[rh];
            ll total = rh - l;
            ll it = bhigh.sum(val - 1);
            invh += total - it;
            bhigh.add(val, 1);
        }
        if (rh == n && invh <= k) rh = n + 1;
        if (invl == k)
        {
            ll left = max(l, rl);
            ans += rh - left;
        }
        if (l <= rl)
        {
            ll val = p[l];
            ll it = blow.sum(val - 1);
            invl -= it;
            blow.add(val, -1);
        }
        if (l <= rh)
        {
            ll val = p[l];
            ll it = bhigh.sum(val - 1);
            invh -= it;
            bhigh.add(val, -1);
        }
    }
    cout << ans << '\n';
}

bool multest = 0;

int main()
{
    fast;
    ll t = 1;
    if(multest) cin >> t;
    while(t--) solve();
    return 0;
}

Submission Info

Submission Time
Task F - Interval Inversion Count
User khanhdang2109
Language C++23 (Clang 21.1.0)
Score 500
Code Size 3343 Byte
Status AC
Exec Time 234 ms
Memory 14700 KiB

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 3 ms 2964 KiB
00_sample_01.txt AC 1 ms 2908 KiB
00_sample_02.txt AC 1 ms 2972 KiB
01_random_03.txt AC 129 ms 10104 KiB
01_random_04.txt AC 133 ms 10492 KiB
01_random_05.txt AC 67 ms 7064 KiB
01_random_06.txt AC 62 ms 6840 KiB
01_random_07.txt AC 101 ms 8888 KiB
01_random_08.txt AC 69 ms 7248 KiB
01_random_09.txt AC 75 ms 7448 KiB
01_random_10.txt AC 224 ms 14284 KiB
01_random_11.txt AC 228 ms 14540 KiB
01_random_12.txt AC 230 ms 14636 KiB
01_random_13.txt AC 227 ms 14688 KiB
01_random_14.txt AC 230 ms 14684 KiB
01_random_15.txt AC 234 ms 14616 KiB
01_random_16.txt AC 233 ms 14540 KiB
01_random_17.txt AC 229 ms 14540 KiB
01_random_18.txt AC 225 ms 14592 KiB
01_random_19.txt AC 233 ms 14540 KiB
01_random_20.txt AC 223 ms 14528 KiB
01_random_21.txt AC 225 ms 14576 KiB
01_random_22.txt AC 224 ms 14612 KiB
01_random_23.txt AC 232 ms 14616 KiB
01_random_24.txt AC 152 ms 14684 KiB
01_random_25.txt AC 173 ms 14684 KiB
01_random_26.txt AC 174 ms 14688 KiB
01_random_27.txt AC 169 ms 14528 KiB
01_random_28.txt AC 173 ms 14540 KiB
01_random_29.txt AC 175 ms 14684 KiB
01_random_30.txt AC 152 ms 14576 KiB
01_random_31.txt AC 175 ms 14592 KiB
01_random_32.txt AC 175 ms 14588 KiB
01_random_33.txt AC 174 ms 14672 KiB
01_random_34.txt AC 173 ms 14700 KiB
01_random_35.txt AC 173 ms 14700 KiB
01_random_36.txt AC 1 ms 2908 KiB
01_random_37.txt AC 168 ms 14612 KiB
01_random_38.txt AC 173 ms 14540 KiB
01_random_39.txt AC 167 ms 14688 KiB
01_random_40.txt AC 168 ms 14540 KiB
01_random_41.txt AC 168 ms 14540 KiB
01_random_42.txt AC 167 ms 14672 KiB
01_random_43.txt AC 169 ms 14584 KiB
01_random_44.txt AC 167 ms 14688 KiB
01_random_45.txt AC 167 ms 14584 KiB
01_random_46.txt AC 168 ms 14612 KiB
01_random_47.txt AC 170 ms 14412 KiB
01_random_48.txt AC 168 ms 14612 KiB
01_random_49.txt AC 167 ms 14636 KiB
01_random_50.txt AC 138 ms 12540 KiB
01_random_51.txt AC 139 ms 12544 KiB
01_random_52.txt AC 133 ms 12240 KiB
01_random_53.txt AC 139 ms 12536 KiB
01_random_54.txt AC 152 ms 13308 KiB