Submission #74673670


Source Code Expand

#include<bits/extc++.h>

#pragma GCC optimize("O3","unroll-loops")
#pragma GCC target("avx,avx2")

#include<iostream>
#include<queue>
#include<vector>
#include<cmath>
#include<map>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

#define coutc "\033[48;5;196m\033[38;5;15m"
#define endc "\033[0m"
#define M(_1, _2, _3, _4, NAME, ...) NAME
#define rep(...) \
  M(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rep4(_, x, n, s) \
  for (int _ = x; (s < 0) ? _ > n : _ < n; _ += s)
#define rep3(_, x, n) rep4(_, x, n, (x < n ? 1 : -1))
#define rep2(_, n) rep3(_, 0, n)
#define rep1(n) rep2(i, n)

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/gcd(a,b)

#define mp make_pair
// #define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define endl "\n"

// #define cin fin
// #define cout fout

// ifstream fin("word.in");
// ofstream fout("word.out");

const int inf = INT_MAX;
const int MOD = 1000000007;
double PI = 4*atan(1);

#ifdef DEBUG
string to_string(char c) { return string({c}); }
// 7
template<class... Ts>
ostream& operator<<(ostream& o, tuple<Ts...> t) {
  string s = "(";
  apply([&](auto&&... r) {
    ((s += to_string(r) + ", "), ...); }, t);
  return o << s.substr(0, len(s) - 2) + ")";
}
// 3
ostream& operator<<(ostream &o, pair<auto, auto> p) {
  return o << "(" << p.fi << ", " << p.se << ")";
}
// 7
template<class C, class T = typename C::value_type,
typename std::enable_if<!std::is_same<C, std::string>
::value>::type* = nullptr>
ostream& operator<<(ostream &o, C c) {
  for (auto e : c) o << setw(7) << right << e;
  return o << endc << endl << coutc;
}
// 7
void debug(const auto &e, const auto &... r) {
  cout << coutc << e;
  ((cout << " " << r), ..., (cout << endc << endl));
}
#else
#define debug(...)
#endif

map<string,int> mon;
map<int,string> inp;

struct bit {
    int n;
    vi t;
    bit(int n = 0) : n(n), t(n + 1) {}
    void add(int i, int v) {
        while (i <= n) t[i] += v, i += i & -i;
    }
    int sum(int i) {
        int r = 0;
        while (i > 0) r += t[i], i -= i & -i;
        return r;
    }
};

ll calc(const vi &a, ll k) {
    if (k < 0) return 0;
    int n = (int)a.size() - 1;
    bit fw(n);
    ll ans = 0, inv = 0;
    int l = 1, r = 1, sz = 0;
    while (l <= n) {
        while (r <= n) {
            int x = a[r];
            int gt = sz - fw.sum(x);
            if (inv + gt > k) break;
            inv += gt;
            fw.add(x, 1);
            sz++;
            r++;
        }
        ans += r - l;
        if (l < r) {
            int x = a[l];
            fw.add(x, -1);
            sz--;
            inv -= fw.sum(x - 1);
        } else r = l + 1;
        l++;
    }
    return ans;
}

void _main(int tc) {
    int n;
    ll k;
    cin >> n >> k;
    vi p(n + 1);
    int i = 1;
    while (i <= n) cin >> p[i], i++;
    cout << calc(p, k) - calc(p, k - 1) << endl;
}
// 5
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  _main(0), exit(0);
  int tc; cin >> tc; rep(i, tc) _main(i + 1);
}

Submission Info

Submission Time
Task F - Interval Inversion Count
User zaahir
Language C++23 (GCC 15.2.0)
Score 500
Code Size 3591 Byte
Status AC
Exec Time 89 ms
Memory 7372 KiB

Compile Error

./Main.cpp: In function 'void _main(int)':
./Main.cpp:134:16: warning: unused parameter 'tc' [-Wunused-parameter]
  134 | void _main(int tc) {
      |            ~~~~^~

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 3620 KiB
00_sample_01.txt AC 1 ms 3692 KiB
00_sample_02.txt AC 1 ms 3512 KiB
01_random_03.txt AC 48 ms 5844 KiB
01_random_04.txt AC 48 ms 5928 KiB
01_random_05.txt AC 30 ms 4772 KiB
01_random_06.txt AC 26 ms 4792 KiB
01_random_07.txt AC 38 ms 5344 KiB
01_random_08.txt AC 29 ms 4748 KiB
01_random_09.txt AC 33 ms 4760 KiB
01_random_10.txt AC 86 ms 7136 KiB
01_random_11.txt AC 82 ms 7176 KiB
01_random_12.txt AC 87 ms 7308 KiB
01_random_13.txt AC 78 ms 7372 KiB
01_random_14.txt AC 76 ms 7316 KiB
01_random_15.txt AC 85 ms 7356 KiB
01_random_16.txt AC 88 ms 7288 KiB
01_random_17.txt AC 82 ms 7176 KiB
01_random_18.txt AC 75 ms 7192 KiB
01_random_19.txt AC 89 ms 7316 KiB
01_random_20.txt AC 71 ms 7308 KiB
01_random_21.txt AC 74 ms 7196 KiB
01_random_22.txt AC 75 ms 7368 KiB
01_random_23.txt AC 89 ms 7316 KiB
01_random_24.txt AC 30 ms 7164 KiB
01_random_25.txt AC 46 ms 7236 KiB
01_random_26.txt AC 48 ms 7316 KiB
01_random_27.txt AC 43 ms 7192 KiB
01_random_28.txt AC 46 ms 7288 KiB
01_random_29.txt AC 47 ms 7204 KiB
01_random_30.txt AC 30 ms 7116 KiB
01_random_31.txt AC 53 ms 7116 KiB
01_random_32.txt AC 53 ms 7116 KiB
01_random_33.txt AC 53 ms 7100 KiB
01_random_34.txt AC 54 ms 7116 KiB
01_random_35.txt AC 54 ms 7208 KiB
01_random_36.txt AC 1 ms 3664 KiB
01_random_37.txt AC 44 ms 7200 KiB
01_random_38.txt AC 45 ms 7244 KiB
01_random_39.txt AC 43 ms 7204 KiB
01_random_40.txt AC 46 ms 7176 KiB
01_random_41.txt AC 44 ms 7204 KiB
01_random_42.txt AC 43 ms 7372 KiB
01_random_43.txt AC 43 ms 7228 KiB
01_random_44.txt AC 44 ms 7360 KiB
01_random_45.txt AC 46 ms 7232 KiB
01_random_46.txt AC 46 ms 7228 KiB
01_random_47.txt AC 47 ms 7364 KiB
01_random_48.txt AC 47 ms 7364 KiB
01_random_49.txt AC 47 ms 7148 KiB
01_random_50.txt AC 39 ms 6588 KiB
01_random_51.txt AC 35 ms 6604 KiB
01_random_52.txt AC 37 ms 6568 KiB
01_random_53.txt AC 36 ms 6696 KiB
01_random_54.txt AC 41 ms 6892 KiB