QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#291196#7685. Barkley IIOAleksaRE 0ms0kbC++141.5kb2023-12-26 05:29:082023-12-26 05:29:08

Judging History

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

  • [2023-12-26 05:29:08]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-12-26 05:29:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
const int N = 5e5 + 69;
int n, m, a[N], fenw[N];
vector<int> pos[N];
map<int, int> lst, sv;
signed main() {
	ios_base::sync_with_stdio(false);
  cout.tie(nullptr); 
  cin.tie(nullptr);
  //freopen("newbarn.in", "r", stdin);
  //freopen(newbarn.out", "w", stdout);
  int tt = 1;
  cin >> tt;
  while (tt--) {
  	cin >> n >> m;
  	sv.clear();
  	lst.clear();
  	for (int i = 1;i <= n;i++) {
  		cin >> a[i];
  		sv[a[i]] = 1;
  	}
  	for (auto u : sv)
  		pos[u.f].push_back(0);
  	for (int i = 1;i <= n;i++)
  		pos[a[i]].push_back(i);
  	for (auto u : sv)
  		pos[u.f].push_back(n + 1);
  	vector<pair<int, int>> q[n + 1];
  	for (auto i : sv) {
  		for (int j = 1;j < (int)pos[i.f].size();j++) {
  			q[pos[i.f][j] - 1].push_back({pos[i.f][j - 1] + 1, i.f});
  		}
  	}
  	auto add = [&](int v, int val) {
  		for (int i = v;i < N;i += (i & -i))
  			fenw[i] += val;
  	};
  	auto get = [&](int v) {
  		int res = 0;
  		for (int i = v;i >= 1;i -= (i & -i))
  			res += fenw[i];
  		return res;
  	};
  	int ans = -1;
  	for (int i = 1;i <= n;i++) {
  		if (lst[a[i]] > 0)
  			add(lst[a[i]], -1);
  		add(i, 1);
  		for (auto u : q[i]) {
  			int l = u.f, mx = u.s;
  			ans = max(ans, get(i) - get(l - 1) - mx);
  		}
  		lst[a[i]] = i;
  	}
  	for (auto i : lst)
  		add(i.f, -1);
  	cout << ans << '\n';
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

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

output:


result: