Submission #74671054


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct BIT {
	vector<int> tree;
	int n;
	BIT(int n = 0) {
		init(n);
	}
	void init(int n_) {
		n = n_;
		tree.assign(n + 2, 0);
	}
	void add(int i, int v) {
		for (; i <= n; i += i & -i)
			tree[i] += v;
	}
	int query(int i) {
		int res = 0;
		for (; i > 0; i -= i & -i)
			res += tree[i];
		return res;
	}
	int queryRange(int l, int r) {
		if (l > r)
			return 0;
		return query(r) - query(l - 1);
	}
};

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

	int N;
	ll K;
	cin >> N >> K;

	vector<int> P(N);
	for (int i = 0; i < N; i++)
		cin >> P[i];

	// 坐标压缩
	vector<int> vals = P;
	sort(vals.begin(), vals.end());
	vals.erase(unique(vals.begin(), vals.end()), vals.end());

	auto getId = [&](int x) {
		return lower_bound(vals.begin(), vals.end(), x) - vals.begin() + 1;
	};

	int M = vals.size();
	for (int i = 0; i < N; i++)
		P[i] = getId(P[i]);

	BIT bit(M);
	ll ans = 0;
	int r = -1;
	ll curInv = 0;

	for (int l = 0; l < N; l++) {
		if (r < l - 1) {
			r = l - 1;
			curInv = 0;

		}
		if (l > 0 && r >= l - 1) {
			int cnt = bit.query(P[l - 1] - 1);
			curInv -= cnt;
			bit.add(P[l - 1], -1);
		}

		while (r < N - 1 && curInv < K) {
			r++;
			int cnt = bit.queryRange(P[r] + 1, M);
			curInv += cnt;
			bit.add(P[r], 1);
		}


		if (curInv == K) {

			ll cntR = 1;


			while (r < N - 1) {
				int nextCnt = bit.queryRange(P[r + 1] + 1, M);
				if (nextCnt == 0) {
					r++;
					bit.add(P[r], 1);

					cntR++;
				} else {
					break;
				}
			}

			ans += cntR;
		}

	}



	bit = BIT(M);
	ans = 0;
	r = -1;
	curInv = 0;

	for (int l = 0; l < N; l++) {

		if (l > 0) {
			int cnt = bit.query(P[l - 1] - 1);
			curInv -= cnt;
			bit.add(P[l - 1], -1);
		}


		if (r < l - 1) {
			r = l - 1;
		}


		while (r < N - 1 && curInv < K) {
			r++;
			int cnt = bit.queryRange(P[r] + 1, M);
			curInv += cnt;
			bit.add(P[r], 1);
		}
		if (curInv == K) {

			ll cntR = 1;
			while (r < N - 1) {
				int nextCnt = bit.queryRange(P[r + 1] + 1, M);
				if (nextCnt == 0) {
					r++;
					bit.add(P[r], 1);
					cntR++;
				} else {
					break;
				}
			}
			ans += cntR;
		}
	}

	cout << ans << endl;
}

Submission Info

Submission Time
Task F - Interval Inversion Count
User Eason0709
Language C++23 (GCC 15.2.0)
Score 0
Code Size 2345 Byte
Status WA
Exec Time 179 ms
Memory 11276 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 29
WA × 26
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 3544 KiB
00_sample_01.txt AC 1 ms 3532 KiB
00_sample_02.txt AC 1 ms 3564 KiB
01_random_03.txt AC 89 ms 8024 KiB
01_random_04.txt AC 94 ms 8308 KiB
01_random_05.txt AC 51 ms 6080 KiB
01_random_06.txt AC 47 ms 5952 KiB
01_random_07.txt AC 72 ms 7244 KiB
01_random_08.txt AC 53 ms 6072 KiB
01_random_09.txt AC 56 ms 6352 KiB
01_random_10.txt AC 156 ms 10832 KiB
01_random_11.txt AC 155 ms 11144 KiB
01_random_12.txt AC 155 ms 11192 KiB
01_random_13.txt AC 153 ms 11220 KiB
01_random_14.txt AC 151 ms 11144 KiB
01_random_15.txt AC 156 ms 11108 KiB
01_random_16.txt AC 157 ms 11084 KiB
01_random_17.txt AC 153 ms 11216 KiB
01_random_18.txt AC 151 ms 11216 KiB
01_random_19.txt AC 156 ms 11220 KiB
01_random_20.txt AC 150 ms 11088 KiB
01_random_21.txt AC 151 ms 11196 KiB
01_random_22.txt AC 152 ms 11200 KiB
01_random_23.txt AC 155 ms 11212 KiB
01_random_24.txt WA 63 ms 11212 KiB
01_random_25.txt AC 67 ms 11276 KiB
01_random_26.txt AC 69 ms 11200 KiB
01_random_27.txt AC 63 ms 11216 KiB
01_random_28.txt AC 67 ms 11212 KiB
01_random_29.txt AC 69 ms 11220 KiB
01_random_30.txt WA 63 ms 11064 KiB
01_random_31.txt WA 168 ms 11108 KiB
01_random_32.txt WA 179 ms 11200 KiB
01_random_33.txt WA 167 ms 11216 KiB
01_random_34.txt WA 168 ms 11076 KiB
01_random_35.txt WA 168 ms 11204 KiB
01_random_36.txt WA 1 ms 3604 KiB
01_random_37.txt WA 66 ms 11276 KiB
01_random_38.txt WA 67 ms 11156 KiB
01_random_39.txt WA 63 ms 11124 KiB
01_random_40.txt WA 69 ms 11264 KiB
01_random_41.txt WA 67 ms 11192 KiB
01_random_42.txt WA 63 ms 11216 KiB
01_random_43.txt WA 63 ms 11212 KiB
01_random_44.txt WA 65 ms 11196 KiB
01_random_45.txt WA 71 ms 11088 KiB
01_random_46.txt WA 70 ms 11192 KiB
01_random_47.txt WA 70 ms 11276 KiB
01_random_48.txt WA 70 ms 11276 KiB
01_random_49.txt WA 70 ms 11144 KiB
01_random_50.txt WA 60 ms 9804 KiB
01_random_51.txt WA 54 ms 9868 KiB
01_random_52.txt WA 58 ms 9536 KiB
01_random_53.txt WA 56 ms 9812 KiB
01_random_54.txt WA 65 ms 10248 KiB