QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#279178#7904. Rainbow SubarrayWoodRE 0ms5672kbC++232.9kb2023-12-08 13:22:122023-12-08 13:22:12

Judging History

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

  • [2024-06-09 00:00:26]
  • hack成功,自动添加数据
  • (/hack/652)
  • [2023-12-08 13:22:12]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:5672kb
  • [2023-12-08 13:22:12]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

#ifdef LOCAL
#include "algo/debug.h"
#else 
#define debug(...) 42
#endif

struct Node {
  Node *l = nullptr;
  Node *r = nullptr;
  int cnt = 0;
  i64 sum = 0;
  Node() : l{}, r{}, cnt{}, sum{} {}
};

const int N = 5e5;
int L[N + 1], R[N + 1], cnt[N + 1], idx;
i64 sum[N + 1];
inline int newNode() {
  return ++idx;
}

int add(int &t, int l, int r, int x, int v) {
  int nt = newNode();
  L[nt] = L[t];
  R[nt] = R[t];
  cnt[nt] = cnt[t] + 1;
  sum[nt] = sum[t] + v;
  if (r - l > 1) {
    int m = (l + r) / 2;
    if (m > x) {
      L[nt] = add(L[nt], l, m, x, v);
    } else {
      R[nt] = add(R[nt], m, r, x, v);
    }
  }
  return nt;
}

int kth(int &t1, int &t2, int l, int r, int k) {
  if (r - l == 1) {
    return l;
  }
  int ct = cnt[L[t2]] - cnt[L[t1]];
  int m = (l + r) / 2;
  if (ct >= k) {
    return kth(L[t1], L[t2], l, m, k);
  } else {
    return kth(R[t1], R[t2], m, r, k - ct);
  }
}

pair<int, i64> operator+(const pair<int, i64> &lhs, const pair<int, i64> &rhs) {
  return make_pair(lhs.first + rhs.first, lhs.second + rhs.second);
}

pair<int, i64> query(int &t1, int &t2, int l, int r, int x, int y) {
  if (l >= x && r <= y) {
    return make_pair(cnt[t2] - cnt[t1], sum[t2] - sum[t1]);
  }
  if (l >= y || r <= x) {
    return make_pair(0, 0);
  }
  int m = (l + r) / 2;
  return query(L[t1], L[t2], l, m, x, y) + query(R[t1], R[t2], m, r, x, y);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    i64 k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      a[i] -= i;
    }
    auto v = a;
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    const int m = (int) v.size();
    vector<int> b(n);
    for (int i = 0; i < n; i++) {
      b[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
    }
    idx = 0;
    vector<int> node(n + 1);
    for (int i = 0; i < n; i++) {
      node[i + 1] = add(node[i], 0, m, b[i], a[i]);
    }
    auto calc = [&](int x) {
      i64 ans = 1e18;
      const int s = (x + 1) / 2;
      for (int i = 0; i + x <= n; i++) {
        int p = kth(node[i], node[i + x], 0, m, s);
        auto [c1, s1] = query(node[i], node[i + x], 0, m, 0, p);
        auto [c2, s2] = query(node[i], node[i + x], 0, m, p, m);
        i64 res = 1LL * v[p] * c1 - s1 + s2 - 1LL * v[p] * c2;
        ans = min(ans, res);
      }
      return ans;
    };
    int low = 1, high = n;
    int ans = 1;
    while (low <= high) {
      int mid = (low + high) / 2;
      if (calc(mid) <= k) {
        ans = mid;
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    cout << ans << '\n';
    for (int i = 1; i <= idx; i++) {
      L[i] = R[i] = cnt[i] = sum[i] = 0;
    }
  }
  return 0; 
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
7 5
7 2 5 5 4 11 7
6 0
100 3 4 5 99 100
5 6
1 1 1 1 1
5 50
100 200 300 400 500
1 100
3

output:

4
3
5
1
1

result:

ok 5 lines

Test #2:

score: -100
Runtime Error

input:

11102
2 167959139
336470888 134074578
5 642802746
273386884 79721198 396628655 3722503 471207868
6 202647942
268792718 46761498 443917727 16843338 125908043 191952768
2 717268783
150414369 193319712
6 519096230
356168102 262263554 174936674 407246545 274667941 279198849
9 527268921
421436316 3613460...

output:

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

result: