QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#267234#7685. Barkley IIwxhtzdyWA 124ms5564kbC++201.9kb2023-11-27 02:32:482023-11-27 02:32:49

Judging History

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

  • [2023-11-27 02:32:49]
  • 评测
  • 测评结果:WA
  • 用时:124ms
  • 内存:5564kb
  • [2023-11-27 02:32:48]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 500005;

int n, lst[N], fenw[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int m;
    cin >> n >> m;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      --a[i];
    }
    vector<int> xs(1, 1);
    for (int i = 0; i < n; i++) {
      xs.push_back(a[i]);
      xs.push_back(a[i] + 1);
    }
    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    int k = (int) xs.size();
    for (int i = 0; i < n; i++) {
      a[i] = (int) (lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin());
    }
    vector<vector<int>> pos(k);
    for (int v = 0; v < k; v++) {
      pos[v].push_back(-1);
    }
    for (int i = 0; i < n; i++) {
      pos[a[i]].push_back(i);
    }
    for (int v = 0; v < k; v++) {
      lst[v] = -1;
    }
    vector<vector<pair<int, int>>> qs(n);
    for (int v = 0; v < k; v++) {
      pos[v].push_back(n);
      int sz = (int) pos[v].size();
      for (int j = 0; j + 1 < sz; j++) {
        int L = pos[v][j] + 1;
        int R = pos[v][j + 1] - 1;
        if (L <= R) {
          qs[R].emplace_back(L, -(xs[v] + 1));
        }
      }
    }
    vector<int> fenw(n + 1);
    auto Modify = [&](int p, int x) {
      for (p++; p <= n; p += p & -p) { 
        fenw[p] += x;
      }
    }; 
    auto Query = [&](int p) {
      int res = 0;
      for (p++; p; p -= p & -p) {
        res += fenw[p];
      }
      return res;
    };
    int ans = -1;
    for (int i = 0; i < n; i++) {
      if (lst[a[i]] != -1) {
        Modify(lst[a[i]], -1);
      }
      Modify(i, +1);
      lst[a[i]] = i;
      for (auto& p : qs[i]) {
        ans = max(ans, Query(i) - Query(p.first - 1) + p.second);
      }
    }    
    cout << ans << '\n';
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Wrong Answer
time: 124ms
memory: 5564kb

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:

6
1
2
4
2
4
3
3
4
4
4
5
2
3
5
3
4
5
7
6
3
3
5
2
4
5
7
2
3
4
6
2
2
3
2
5
3
3
3
3
2
5
2
1
3
5
3
3
3
8
4
5
5
4
4
2
5
4
5
5
4
3
6
3
5
3
1
7
7
6
5
5
4
3
3
4
3
6
3
4
3
3
4
4
3
8
4
5
4
5
3
4
2
2
4
4
4
6
4
3
4
2
4
5
4
7
3
1
6
5
3
5
4
4
5
4
6
5
6
7
4
4
5
6
4
3
3
4
3
5
3
2
3
2
5
3
2
1
6
5
2
5
5
5
4
3
4
6
6
5
...

result:

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