QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#718098#9584. 顾影自怜PHarrRE 90ms112552kbC++201.4kb2024-11-06 19:41:442024-11-06 19:41:45

Judging History

你现在查看的是最新测评结果

  • [2024-11-06 19:41:45]
  • 评测
  • 测评结果:RE
  • 用时:90ms
  • 内存:112552kb
  • [2024-11-06 19:41:44]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;


using i32 = int32_t;
using i64 = long long;
using ui32 = unsigned int;

#define int i64

using vi = vector<int>;

void solve() {
	int n, k;
	cin >> n >> k;

	vi a(n + 1);
	for(int i = 1; i <= n; i ++)
		cin >> a[i];

	vector vis(n + 1, vi(1, 0));

	for(int i = 1;i <= n; i ++)
		vis[a[i]].push_back(i);

	for(int i = 1; i <= n; i ++)
		vis[i].push_back(n + 1);

	vi lst(n + 1), nxt(n + 1, -1), ed(n + 1, -1);
	for(int i = 1; i <= n; i ++) {
		if(vis[i].size() - 2 < k) continue;
		for(int l = 1, r = k; r < vis[i].size(); l ++, r ++) {
			lst[vis[i][l]] = vis[i][l - 1];
			nxt[vis[i][l]] = vis[i][r];
		}
	}

	vi L(n + 1);
	stack<int> stk;
	for(int i = 1; i <= n; i ++) {
		while(not stk.empty() and a[stk.top()] <= a[i])
			stk.pop();
		if(stk.empty()) L[i] = 0;
		else L[i] = stk.top();
		stk.push(i); 
	}

	vi R(n + 1);
	stk = stack<int>();
	for(int i = n; i >= 1; i --) {
		while(not stk.empty() and a[stk.top()] <= a[i])
			stk.pop();
		if(stk.empty()) R[i] = n + 1;
		else R[i] = stk.top();
		stk.push(i);
	}
	int res = 0;
	for(int i = 1; i <= n; i ++){
		if(nxt[i] == -1) continue;
		int l = i - max(lst[i], L[i]);
		int r = R[i] - nxt[i];
		res += l * r;
	}
	cout << res << "\n";
	return ;
}

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

	int T;
	cin >> T;
	while(T --)
		solve();

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3484kb

input:

2
5 2
1 3 3 2 2
4 3
1 4 2 1

output:

7
0

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 90ms
memory: 112552kb

input:

1
1000000 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

500000500000

result:

ok single line: '500000500000'

Test #3:

score: -100
Runtime Error

input:

158921
1 1
1
2 1
1 1
2 2
1 1
2 1
1 2
2 2
1 2
2 1
2 1
2 2
2 1
2 1
2 2
2 2
2 2
3 1
1 1 1
3 2
1 1 1
3 3
1 1 1
3 1
1 1 2
3 2
1 1 2
3 3
1 1 2
3 1
1 1 3
3 2
1 1 3
3 3
1 1 3
3 1
1 2 1
3 2
1 2 1
3 3
1 2 1
3 1
1 2 2
3 2
1 2 2
3 3
1 2 2
3 1
1 2 3
3 2
1 2 3
3 3
1 2 3
3 1
1 3 1
3 2
1 3 1
3 3
1 3 1
3 1
1 3 2
3 2...

output:


result: