QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#286541#7685. Barkley IIetohari#WA 0ms18856kbC++141.5kb2023-12-17 23:44:212023-12-17 23:44:21

Judging History

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

  • [2023-12-17 23:44:21]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:18856kb
  • [2023-12-17 23:44:21]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

constexpr int MAXN = 5e5 + 10;
constexpr int MAXM = 5e5 + 10;

int n, m, a[MAXN], last[MAXM], tree[MAXN];
vector<pair<int, int> > query[MAXN];

void update(int pos, int val) {
  if (pos == 0) return;
  for (; pos <= n; pos += pos & -pos) {
    tree[pos] += val;
  }
}

int get(int pos) {
  int sum = 0;
  for (; pos > 0; pos -= pos & -pos) {
    sum += tree[pos];
  }
  return sum;
}

int get_range(int l, int r) {
  if (l > r) return 0;
  return get(r) - get(l - 1);
}

signed main() {
  cin.tie(NULL)->sync_with_stdio(false);

  int test;
  cin >> test;

  while (test--) {
    cin >> n >> m;

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

    for (int i = 1; i <= n; i++) {
      query[i - 1].emplace_back(last[a[i]] + 1, a[i]);
      last[a[i]] = i;
    }

    for (int i = 1; i <= m; i++) {
      last[a[i]] = n + 1;
    }

    for (int i = n; i >= 1; i--) {
      query[last[a[i]] - 1].emplace_back(i + 1, a[i]);
      last[a[i]] = i;
    }

    for (int i = 1; i <= m; i++) {
      last[a[i]] = 0;
    }

    int ans = -1;

    for (int r = 1; r <= n; r++) {
      update(last[a[r]], -1);
      last[a[r]] = r;
      update(last[a[r]], 1);
      for (auto [l, v] : query[r]) {
        ans = max(ans, get_range(l, r) - v);
      }
    }

    cout << ans << "\n";

    for (int i = 1; i <= max(n, m); i++) {
      last[a[i]] = 0;
      query[i].clear();
      tree[i] = 0;
    }
  }

  return (0 ^ 0);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 18856kb

input:

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

output:

1
3

result:

wrong answer 1st numbers differ - expected: '2', found: '1'