Submission #807559

# Submission time Handle Problem Language Result Execution time Memory
807559 2023-08-04T19:25:53 Z dong_liu Sushi (JOI16_sushi) C++17
100 / 100
1684 ms 81720 KB
/*
Statement

You are given a circular array A of length N. You are given Q queries, each
giving (l, r, a). You are to run the following code:

for (int i = l; i <= r; i++)
  if (a < A[i])
    swap(a, A[i]);

For each query, output the final value of a.

Scoring

5p: 1 <= N, Q <= 2e3
15p: For all queries, l = r = 1
80p: 1 <= N <= 4e5, 1 <= Q <= 2.5e4, 1 <= A_i <= 1e9
*/

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int N = 4e5, B = 633;

struct T {
  int *a, n;
  vector<int> u;
  priority_queue<int> v;

  void build(int *a_, int n_) {
    a = a_, n = n_;
    pull();
  }
  void pull() { v = priority_queue<int>(a, a + n); }
  int force(int l, int r, int x) {
    for (int i = l; i <= r; i++)
      if (x < a[i]) swap(x, a[i]);
    return x;
  }
  int put(int x) {
    int t = v.top();
    if (x < t) {
      v.pop();
      v.push(x);
      u.push_back(x);
      x = t;
    }
    return x;
  }
  void push() {
    if (u.empty()) return;
    static priority_queue<int, vector<int>, greater<int>> u_;
    u_ = priority_queue<int, vector<int>, greater<int>>(u.begin(), u.end());
    int t = u_.top();
    for (int i = 0; i < n; i++)
      if (t < a[i]) {
        u_.pop(), u_.push(a[i]);
        a[i] = t, t = u_.top();
      }
    u.clear();
  }
} b[(N - 1) / B + 1];

int update(int l, int r, int x) {
  if (l / B == r / B) {
    b[l / B].push();
    x = b[l / B].force(l - l / B * B, r - l / B * B, x);
    b[l / B].pull();
  } else {
    b[l / B].push();
    x = b[l / B].force(l - l / B * B, b[l / B].n - 1, x);
    b[l / B].pull();
    for (int i = l / B + 1; i < r / B; i++) x = b[i].put(x);
    b[r / B].push();
    x = b[r / B].force(0, r - r / B * B, x);
    b[r / B].pull();
  }
  return x;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, q;
  cin >> n >> q;
  static int a[N];
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i <= (n - 1) / B; i++)
    b[i].build(a + B * i, min(n, B * (i + 1)) - B * i);
  while (q--) {
    int l, r, x;
    cin >> l >> r >> x, l--, r--;
    if (l <= r)
      x = update(l, r, x);
    else {
      swap(l, r);
      x = update(r, n - 1, x);
      x = update(0, l, x);
    }
    cout << x << '\n';
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 468 KB Output is correct
2 Correct 13 ms 388 KB Output is correct
3 Correct 14 ms 476 KB Output is correct
4 Correct 13 ms 448 KB Output is correct
5 Correct 20 ms 428 KB Output is correct
6 Correct 13 ms 384 KB Output is correct
7 Correct 13 ms 420 KB Output is correct
8 Correct 13 ms 340 KB Output is correct
9 Correct 14 ms 468 KB Output is correct
10 Correct 14 ms 456 KB Output is correct
11 Correct 14 ms 468 KB Output is correct
12 Correct 16 ms 388 KB Output is correct
13 Correct 16 ms 456 KB Output is correct
14 Correct 18 ms 340 KB Output is correct
15 Correct 19 ms 468 KB Output is correct
16 Correct 6 ms 468 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 372 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1158 ms 81344 KB Output is correct
2 Correct 1123 ms 80084 KB Output is correct
3 Correct 154 ms 4640 KB Output is correct
4 Correct 1150 ms 81516 KB Output is correct
5 Correct 651 ms 80680 KB Output is correct
6 Correct 1039 ms 80556 KB Output is correct
7 Correct 985 ms 80576 KB Output is correct
8 Correct 964 ms 80524 KB Output is correct
9 Correct 140 ms 4756 KB Output is correct
10 Correct 586 ms 75072 KB Output is correct
11 Correct 140 ms 4516 KB Output is correct
12 Correct 635 ms 76112 KB Output is correct
13 Correct 1136 ms 81444 KB Output is correct
14 Correct 1096 ms 80248 KB Output is correct
15 Correct 146 ms 4684 KB Output is correct
16 Correct 1111 ms 81720 KB Output is correct
17 Correct 617 ms 80672 KB Output is correct
18 Correct 1006 ms 80652 KB Output is correct
19 Correct 1016 ms 80552 KB Output is correct
20 Correct 987 ms 80552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 468 KB Output is correct
2 Correct 13 ms 388 KB Output is correct
3 Correct 14 ms 476 KB Output is correct
4 Correct 13 ms 448 KB Output is correct
5 Correct 20 ms 428 KB Output is correct
6 Correct 13 ms 384 KB Output is correct
7 Correct 13 ms 420 KB Output is correct
8 Correct 13 ms 340 KB Output is correct
9 Correct 14 ms 468 KB Output is correct
10 Correct 14 ms 456 KB Output is correct
11 Correct 14 ms 468 KB Output is correct
12 Correct 16 ms 388 KB Output is correct
13 Correct 16 ms 456 KB Output is correct
14 Correct 18 ms 340 KB Output is correct
15 Correct 19 ms 468 KB Output is correct
16 Correct 6 ms 468 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 372 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 468 KB Output is correct
23 Correct 1158 ms 81344 KB Output is correct
24 Correct 1123 ms 80084 KB Output is correct
25 Correct 154 ms 4640 KB Output is correct
26 Correct 1150 ms 81516 KB Output is correct
27 Correct 651 ms 80680 KB Output is correct
28 Correct 1039 ms 80556 KB Output is correct
29 Correct 985 ms 80576 KB Output is correct
30 Correct 964 ms 80524 KB Output is correct
31 Correct 140 ms 4756 KB Output is correct
32 Correct 586 ms 75072 KB Output is correct
33 Correct 140 ms 4516 KB Output is correct
34 Correct 635 ms 76112 KB Output is correct
35 Correct 1136 ms 81444 KB Output is correct
36 Correct 1096 ms 80248 KB Output is correct
37 Correct 146 ms 4684 KB Output is correct
38 Correct 1111 ms 81720 KB Output is correct
39 Correct 617 ms 80672 KB Output is correct
40 Correct 1006 ms 80652 KB Output is correct
41 Correct 1016 ms 80552 KB Output is correct
42 Correct 987 ms 80552 KB Output is correct
43 Correct 578 ms 8704 KB Output is correct
44 Correct 547 ms 8852 KB Output is correct
45 Correct 289 ms 4700 KB Output is correct
46 Correct 582 ms 8760 KB Output is correct
47 Correct 541 ms 8764 KB Output is correct
48 Correct 1604 ms 10500 KB Output is correct
49 Correct 1682 ms 10272 KB Output is correct
50 Correct 1684 ms 10336 KB Output is correct
51 Correct 1654 ms 10384 KB Output is correct
52 Correct 580 ms 9648 KB Output is correct
53 Correct 578 ms 9568 KB Output is correct
54 Correct 1412 ms 12836 KB Output is correct
55 Correct 1521 ms 12740 KB Output is correct
56 Correct 1520 ms 13088 KB Output is correct
57 Correct 1552 ms 12960 KB Output is correct
58 Correct 1346 ms 13112 KB Output is correct
59 Correct 1303 ms 13276 KB Output is correct
60 Correct 589 ms 80612 KB Output is correct
61 Correct 1000 ms 80676 KB Output is correct
62 Correct 972 ms 80748 KB Output is correct
63 Correct 944 ms 80492 KB Output is correct
64 Correct 187 ms 4652 KB Output is correct
65 Correct 344 ms 42204 KB Output is correct
66 Correct 377 ms 42664 KB Output is correct
67 Correct 505 ms 48056 KB Output is correct
68 Correct 695 ms 47996 KB Output is correct
69 Correct 723 ms 47952 KB Output is correct
70 Correct 672 ms 48060 KB Output is correct