QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#104754#5374. 数圈HgCl20 77ms4212kbC++143.6kb2023-05-11 21:09:162023-05-11 21:09:20

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-11 21:09:20]
  • 评测
  • 测评结果:0
  • 用时:77ms
  • 内存:4212kb
  • [2023-05-11 21:09:16]
  • 提交

answer

#include <algorithm>
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>

namespace {
using std::cin;
using std::cout;
using std::int64_t;
using std::size_t;

namespace base {
template <typename T, size_t... sizes>
struct NestedArray {};

template <typename T, size_t size, size_t... sizes>
struct NestedArray<T, size, sizes...> {
  using Type = std::array<typename NestedArray<T, sizes...>::Type, size>;
};

template <typename T>
struct NestedArray<T> {
  using Type = T;
};

template <typename T, size_t... sizes>
using Array = typename NestedArray<T, sizes...>::Type;

void OptimizeIO() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
}

void OptimizeIO(const std::string &input_file, const std::string &output_file) {
  static std::ifstream input_stream(input_file);
  static std::ofstream output_stream(output_file);
  cin.rdbuf(input_stream.rdbuf());
  cout.rdbuf(output_stream.rdbuf());
  cin.tie(nullptr), cout.tie(nullptr);
}
}  // namespace base

using base::Array;

const int kMaxN = 1.0e5 + 5;
int n, tot;
int64_t ans;
Array<int, kMaxN> a, sum_a, raw, tmp, tr;

struct Node {
  int val, num;
};

Array<Node, kMaxN> b;

void Add(int x, int v) {
  while (x <= tot) tr[x] += v, x += x & -x;
}

int Sum(int x) {
  int ans = 0;
  while (x) ans += tr[x], x &= x - 1;
  return ans;
}

int Divide(int x, int y) { return x >= 0 ? x / y : (x - y + 1) / y; }

void Solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  sum_a[0] = 0;
  for (int i = 1; i <= n; i++) sum_a[i] = sum_a[i - 1] + a[i];

  if (sum_a[n] < 0) {
    cout << "-1\n";
    return;
  }

  if (sum_a[n] == 0) {
    if (std::count(a.begin() + 1, a.begin() + n + 1, 0) == n) {
      cout << "0\n";
    } else {
      cout << "-1\n";
    }

    return;
  }

  ans = 0;
  std::copy_n(sum_a.begin(), n, raw.begin());
  std::sort(raw.begin(), raw.begin() + n);
  std::copy_n(raw.begin(), n, tmp.begin());
  tot = std::unique(raw.begin(), raw.begin() + n) - raw.begin();

  for (int i = 0; i < n; i++) {
    sum_a[i] = std::lower_bound(raw.begin(), raw.begin() + tot, sum_a[i]) -
               raw.begin() + 1;
  }

  for (int i = 0; i < n; i++) {
    ans += i - Sum(sum_a[i]);
    Add(sum_a[i], 1);
  }

  int m = 0;

  for (int i = 0; i < n;) {
    int j = i;
    while (j < n && tmp[j] == tmp[i]) j++;
    b[++m] = {tmp[i], j - i}, i = j;
  }

  std::fill_n(tr.begin() + 1, tot, 0);
  tot = 0;

  for (int i = 1; i <= m; i++) {
    int x = b[i].val + 1;
    x -= Divide(x, sum_a[n]) * sum_a[n];
    raw[++tot] = x;
  }

  std::sort(raw.begin() + 1, raw.begin() + tot + 1);
  tot = std::unique(raw.begin() + 1, raw.begin() + tot + 1) - raw.begin() - 1;
  int tot = 0;
  int64_t sum = 0;

  for (int i = 1; i <= m; i++) {
    int q = Divide(b[i].val, sum_a[n]);
    int r = b[i].val - q * sum_a[n];
    ans += static_cast<int64_t>(tot) * q - sum;
    sum += static_cast<int64_t>(b[i].num) * q;
    int qwq = std::lower_bound(raw.begin() + 1, raw.begin() + ::tot + 1, r) -
              raw.begin() - 1;
    ans -= static_cast<int64_t>(b[i].num) * (tot - Sum(qwq));
    r = (r + 1) % sum_a[n];
    qwq = std::lower_bound(raw.begin() + 1, raw.begin() + ::tot + 1, r) -
          raw.begin();
    Add(qwq, b[i].num);
    tot += b[i].num;
  }

  cout << ans << "\n";
  std::fill_n(tr.begin() + 1, ::tot, 0);
}

int Main() {
  base::OptimizeIO();
  int t;
  cin >> t;
  while (t--) Solve();
  return 0;
}
}  // namespace

int main() { return Main(); }

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

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

input:

10
3
2 -9 -4
3
4 6 0
3
3 -10 0
3
7 -6 3
3
-6 7 10
3
-3 9 -2
3
6 1 -2
3
5 2 -2
3
-9 -5 7
3
-4 -5 6

output:

-1
0
-1
2
0
3
0
1
-1
-1

result:

wrong answer 4th lines differ - expected: '3', found: '2'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Wrong Answer

Test #4:

score: 0
Wrong Answer
time: 77ms
memory: 4212kb

input:

10
30000
-3879 -556 4570 1863 2815 -4010 2471 -270 2835 3071 -3331 -1251 -2243 4221 -5249 -4134 3376 1978 858 2545 -4207 386 3875 2029 1706 1119 3065 -3097 4399 4385 -3021 2473 2506 2157 3946 -886 3929 1478 2728 -4239 4091 -151 -4762 -2136 -1424 2162 -669 267 190 -1180 2640 -757 -2078 -1409 3165 216...

output:

119787573698793
44816626153753
56265847189821
58360009816531
79768776227826
47401427913086
76567437110337
110835096431954
32188027247659
59892596299421

result:

wrong answer 1st lines differ - expected: '121860198793245', found: '119787573698793'

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #5:

0%

Subtask #7:

score: 0
Skipped

Dependency #1:

0%