QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#383022#7685. Barkley IILeonard#RE 0ms6564kbC++202.0kb2024-04-08 20:58:292024-04-08 20:58:29

Judging History

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

  • [2024-04-08 20:58:29]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:6564kb
  • [2024-04-08 20:58:29]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int N = 5e5 + 10;
struct node {
	int l, r, v;
#define ls u << 1
#define rs u << 1 | 1
} tr[N << 2];
void pushup(int u) {
	tr[u].v = tr[ls].v + tr[rs].v;
}
void build(int u, int l, int r) {
	tr[u] = {l, r};
	if(l == r) {
		return ;
	}
	int mid = l + r >> 1;
	build(ls, l, mid), build(rs, mid + 1, r);
	pushup(u);
}
int query(int u, int l, int r) {
	if(tr[u].l >= l && tr[u].r <= r) return tr[u].v;
	int mid = tr[u].l + tr[u].r >> 1;
	int v = 0;
	if(l <= mid) v = v + query(ls, l, r);
	if(r > mid) v = v + query(rs, l, r);
	return v;
}
void modify(int u, int x, int v) {
	if(tr[u].l == x && tr[u].r == x) tr[u].v = v;
	else {
		int mid = tr[u].l + tr[u].r >> 1;
		if(mid >= x) modify(ls, x, v);
		else modify(rs, x, v);
		pushup(u);
	}
}
int n, m, a[N];
void solve() {
	cin >> n >> m;
	vector<vector<int>> g(m + 1);
	for(int i = 1; i <= m; i++) {
		g[i].push_back(0);
	}
	vector<set<int>> s(m + 1);
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		g[a[i]].push_back(i);
		s[a[i]].insert(i);
	}
	
	build(1, 1, n);
	for(int i = 1; i <= m; i++) {
		if(s[i].empty()) continue;
		auto it = *s[i].begin();
		modify(1, it, 1);
		
		s[i].erase(it);
	}
	for(int i = 1; i <= m; i++) {
		g[i].push_back(n + 1);
	}
	vector<vector<array<int, 2>>> q(n + 2);
	for(int i = 1; i <= m; i++) {
		for(int j = 0; j + 1 < g[i].size(); j++) {
			q[g[i][j] + 1].push_back({g[i][j + 1] - 1, i});
		}
	}
	sort(q.begin(), q.end());
	int ans = 0;
	for(int i = 1; i <= n; i++) {
		for(int j = 0; j < q[i].size(); j++) {
			int l = i, r = q[i][j][0], y = q[i][j][1];
			if(l > r) continue;
			int cnt = query(1, l, r);
			ans = max(cnt - y, ans);
			if(!s[a[i]].empty()) {
				auto it = *s[a[i]].begin();
				modify(1, it, 1);
				//cout << it << '\n';
				s[i].erase(it);
			}
		}
		
	}
	cout << ans << '\n';
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t;
	t = 1;
	cin >> t;
	while(t--) {
		solve();
	}
	
	return 0;
}

詳細信息

Test #1:

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

input:

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

output:

2
3

result:

ok 2 number(s): "2 3"

Test #2:

score: -100
Runtime Error

input:

50000
10 19
12 6 1 12 11 15 4 1 13 18
10 8
8 7 6 7 6 2 2 3 4 8
10 6
3 2 6 6 5 2 3 4 5 6
10 11
6 3 7 9 2 1 2 10 10 4
10 6
6 1 2 6 1 1 3 4 2 1
10 9
8 5 3 9 1 7 5 5 1 1
10 5
1 4 3 2 5 4 5 3 5 2
10 14
3 8 12 10 4 2 3 13 7 3
10 14
5 5 12 2 8 1 13 9 8 5
10 7
5 5 6 6 1 5 3 7 3 4
10 7
5 1 4 6 1 6 4 3 7 5
10...

output:


result: