답안 #1027711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1027711 2024-07-19T09:25:01 Z 우민규(#10951) Sushi (JOI16_sushi) C++17
100 / 100
3908 ms 16044 KB
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'

const int bucket = 623;

int n, q;

struct Segment {
  vector<int> arr;
  priority_queue<int> in_arr;
  vector<int> queued;
  int push(int x) {
    queued.push_back(x);
    in_arr.push(x);
    auto out = in_arr.top();
    in_arr.pop();
    if (queued.size() > 3 * bucket) {
      // rebuild this segment
      apply();
      build();
    }
    return out;
  }
  void apply() {
    // rebuild arr, clear queued, don't modify in_arr

    priority_queue<int, vector<int>, greater<int>> in_queue;
    for (auto q : queued) in_queue.push(q);
    for (auto& a : arr) {
      in_queue.push(a);
      a = in_queue.top();
      in_queue.pop();
    }
    queued.clear();
  }
  void build() {
    while (!in_arr.empty()) in_arr.pop();
    for (auto a : arr) in_arr.push(a);
  }
};

void solve() {
  cin >> n >> q;
  vector<Segment> segments((n + bucket - 1) / bucket);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    segments[i / bucket].arr.push_back(x);
  }
  for (auto& s : segments) s.build();
  auto query_naive = [&](int l, int r, int b, int p) {
    segments[b].apply();
    for (int i = l; i < r; ++i) {
      if (p < segments[b].arr[i]) {
        swap(p, segments[b].arr[i]);
      }
    }
    segments[b].build();
    return p;
  };
  auto query = [&](int s, int t, int p) {
    int sb = s / bucket, tb = (t - 1) / bucket;
    if (sb == tb) {
      return query_naive(s - sb * bucket, t - sb * bucket, sb, p);
    }
    p = query_naive(s - sb * bucket, bucket, sb, p);
    for (int i = sb + 1; i < tb; ++i) {
      p = segments[i].push(p);
    }
    return query_naive(0, t - tb * bucket, tb, p);
  };
  for (int i = 0; i < q; ++i) {
    if (i == 3) {
      int x = 3;
    }
    int s, t, p;
    cin >> s >> t >> p;
    s -= 1;
    // s -> t-1
    if (s >= t) {
      // s -> N-1, 0 -> t
      p = query(s, n, p);
      s = 0;
    }
    cout << query(s, t, p) << "\n";
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int t = 1;

  while (t--) {
    solve();
  }
  return 0;
}

Compilation message

sushi.cpp: In function 'void solve()':
sushi.cpp:75:11: warning: unused variable 'x' [-Wunused-variable]
   75 |       int x = 3;
      |           ^
# 결과 실행 시간 메모리 Grader output
1 Correct 134 ms 468 KB Output is correct
2 Correct 135 ms 476 KB Output is correct
3 Correct 135 ms 468 KB Output is correct
4 Correct 139 ms 472 KB Output is correct
5 Correct 134 ms 344 KB Output is correct
6 Correct 134 ms 348 KB Output is correct
7 Correct 41 ms 344 KB Output is correct
8 Correct 40 ms 472 KB Output is correct
9 Correct 143 ms 720 KB Output is correct
10 Correct 132 ms 348 KB Output is correct
11 Correct 147 ms 344 KB Output is correct
12 Correct 128 ms 344 KB Output is correct
13 Correct 143 ms 344 KB Output is correct
14 Correct 170 ms 476 KB Output is correct
15 Correct 168 ms 472 KB Output is correct
16 Correct 61 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1880 ms 11344 KB Output is correct
2 Correct 1962 ms 14164 KB Output is correct
3 Correct 884 ms 11896 KB Output is correct
4 Correct 1949 ms 15356 KB Output is correct
5 Correct 1192 ms 15468 KB Output is correct
6 Correct 1847 ms 15268 KB Output is correct
7 Correct 1872 ms 15480 KB Output is correct
8 Correct 1878 ms 15484 KB Output is correct
9 Correct 733 ms 11860 KB Output is correct
10 Correct 1135 ms 14136 KB Output is correct
11 Correct 682 ms 12140 KB Output is correct
12 Correct 1027 ms 14272 KB Output is correct
13 Correct 1758 ms 15248 KB Output is correct
14 Correct 1866 ms 13840 KB Output is correct
15 Correct 830 ms 12020 KB Output is correct
16 Correct 1772 ms 15404 KB Output is correct
17 Correct 1204 ms 15444 KB Output is correct
18 Correct 1774 ms 15568 KB Output is correct
19 Correct 1779 ms 15472 KB Output is correct
20 Correct 1758 ms 15844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 134 ms 468 KB Output is correct
2 Correct 135 ms 476 KB Output is correct
3 Correct 135 ms 468 KB Output is correct
4 Correct 139 ms 472 KB Output is correct
5 Correct 134 ms 344 KB Output is correct
6 Correct 134 ms 348 KB Output is correct
7 Correct 41 ms 344 KB Output is correct
8 Correct 40 ms 472 KB Output is correct
9 Correct 143 ms 720 KB Output is correct
10 Correct 132 ms 348 KB Output is correct
11 Correct 147 ms 344 KB Output is correct
12 Correct 128 ms 344 KB Output is correct
13 Correct 143 ms 344 KB Output is correct
14 Correct 170 ms 476 KB Output is correct
15 Correct 168 ms 472 KB Output is correct
16 Correct 61 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1880 ms 11344 KB Output is correct
24 Correct 1962 ms 14164 KB Output is correct
25 Correct 884 ms 11896 KB Output is correct
26 Correct 1949 ms 15356 KB Output is correct
27 Correct 1192 ms 15468 KB Output is correct
28 Correct 1847 ms 15268 KB Output is correct
29 Correct 1872 ms 15480 KB Output is correct
30 Correct 1878 ms 15484 KB Output is correct
31 Correct 733 ms 11860 KB Output is correct
32 Correct 1135 ms 14136 KB Output is correct
33 Correct 682 ms 12140 KB Output is correct
34 Correct 1027 ms 14272 KB Output is correct
35 Correct 1758 ms 15248 KB Output is correct
36 Correct 1866 ms 13840 KB Output is correct
37 Correct 830 ms 12020 KB Output is correct
38 Correct 1772 ms 15404 KB Output is correct
39 Correct 1204 ms 15444 KB Output is correct
40 Correct 1774 ms 15568 KB Output is correct
41 Correct 1779 ms 15472 KB Output is correct
42 Correct 1758 ms 15844 KB Output is correct
43 Correct 3188 ms 13416 KB Output is correct
44 Correct 3338 ms 13292 KB Output is correct
45 Correct 1657 ms 9808 KB Output is correct
46 Correct 3236 ms 13292 KB Output is correct
47 Correct 3126 ms 13504 KB Output is correct
48 Correct 3029 ms 13476 KB Output is correct
49 Correct 3454 ms 13396 KB Output is correct
50 Correct 3554 ms 13288 KB Output is correct
51 Correct 3908 ms 13412 KB Output is correct
52 Correct 3904 ms 15708 KB Output is correct
53 Correct 3587 ms 15832 KB Output is correct
54 Correct 3365 ms 16004 KB Output is correct
55 Correct 3790 ms 16044 KB Output is correct
56 Correct 3658 ms 15904 KB Output is correct
57 Correct 3793 ms 15888 KB Output is correct
58 Correct 2316 ms 14284 KB Output is correct
59 Correct 1864 ms 14404 KB Output is correct
60 Correct 1794 ms 15408 KB Output is correct
61 Correct 2203 ms 15488 KB Output is correct
62 Correct 2083 ms 15440 KB Output is correct
63 Correct 2119 ms 15440 KB Output is correct
64 Correct 917 ms 9904 KB Output is correct
65 Correct 1280 ms 14008 KB Output is correct
66 Correct 1311 ms 13872 KB Output is correct
67 Correct 2562 ms 15448 KB Output is correct
68 Correct 3224 ms 15404 KB Output is correct
69 Correct 2941 ms 15624 KB Output is correct
70 Correct 2837 ms 15588 KB Output is correct