답안 #1067068

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1067068 2024-08-20T10:35:41 Z juicy 수확 (JOI20_harvest) C++17
100 / 100
449 ms 73436 KB
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

using namespace __gnu_pbds;

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

#define int long long

template<class T>
using Tree = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 2e5 + 5;

int n, m, l, c, q;
int a[N], b[N], tin[N], tout[N], root[N], pr[N];
bool in[N], spec[N];
int dep[N], res[N];
vector<int> g[N];
Tree<int> st;
vector<array<int, 2>> updates[N];
vector<array<int, 3>> queries[N];

void add(int x) {
  st.insert(x);
}

int qry(int i) {
  return st.order_of_key(i + 1);
}

int qry(int l, int r) {
  return qry(r) - qry(l - 1);
}

int add(int a, int b, int c) {
  a = (a + b) % c;
  if (a >= c) {
    a -= c;
  }
  if (a < 0) {
    a += c;
  }
  return a;
} 

int dis(int s, int t) {
  return c + add(a[t], -add(a[s], c, l), l);
}

int order;

void dfs(int u) {
  tin[u] = ++order;
  for (int v : g[u]) {
    if (!root[v]) {
      root[v] = root[u];
      dep[v] = dep[u] + dis(u, v);
      dfs(v);
    }
  }
  tout[u] = order;
}

void solve(vector<array<int, 3>> &que, vector<array<int, 2>> &ups, int len) {
  sort(que.begin(), que.end());
  sort(ups.begin(), ups.end());
  int j = 0;
  for (auto &[x, u, id] : que) {
    while (j < ups.size() && ups[j][0] <= x) {
      add(tin[ups[j][1]]);
      ++j;
    }
    res[id] = qry(tin[u], tout[u]);
    if (spec[id]) {
      x -= len;
    }
  }
  st.clear();
  sort(que.begin(), que.end());
  int k = 0;
  j = 0;
  for (auto [x, u, id] : que) {
    while (j < ups.size() && ups[j][0] <= x) {
      add(ups[j][0] % len);
      k += ups[j][0] / len;
      ++j;
    }
    if (spec[id]) {
      res[id] += (int) st.size() * (x / len) - k + qry(x % len);
    }
  }
  st.clear();
}

int32_t main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n >> m >> l >> c;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }  
  for (int i = 1; i <= m; ++i) {
    cin >> b[i];
  }
  for (int i = 1; i <= n; ++i) {
    pr[i] = add(upper_bound(a + 1, a + n + 1, add(a[i], -c, l)) - a - 1, -1, n) + 1;
    g[pr[i]].push_back(i);
  }
  for (int i = 1; i <= n; ++i) {
    if (!root[i]) {
      int j = i;
      for (; !in[j]; j = pr[j]) {
        in[j] = 1;
      }
      root[j] = j;
      for (int k = i; k != j; k = pr[k]) {
        in[k] = 0;
      }
      dfs(j);
    }
  }
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    int r, x; cin >> r >> x;
    spec[i] = in[r];
    queries[root[r]].push_back({x + dep[r], r, i});
  }
  for (int i = 1; i <= m; ++i) {
    int p = add(upper_bound(a + 1, a + n + 1, b[i]) - a - 1, -1, n) + 1;
    updates[root[p]].push_back({dep[p] + add(b[i], -a[p], l), p});
  }
  for (int i = 1; i <= n; ++i) {
    if (root[i] == i) {
      int l = dep[pr[i]] + dis(pr[i], i);
      solve(queries[i], updates[i], l);
    }
  }
  for (int i = 1; i <= q; ++i) {
    cout << res[i] << "\n";
  }
  return 0;
}

Compilation message

harvest.cpp: In function 'void solve(std::vector<std::array<long long int, 3> >&, std::vector<std::array<long long int, 2> >&, long long int)':
harvest.cpp:78:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |     while (j < ups.size() && ups[j][0] <= x) {
      |            ~~^~~~~~~~~~~~
harvest.cpp:92:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |     while (j < ups.size() && ups[j][0] <= x) {
      |            ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14684 KB Output is correct
2 Correct 8 ms 14924 KB Output is correct
3 Correct 9 ms 15196 KB Output is correct
4 Correct 10 ms 15192 KB Output is correct
5 Correct 13 ms 15704 KB Output is correct
6 Correct 10 ms 15464 KB Output is correct
7 Correct 9 ms 15452 KB Output is correct
8 Correct 8 ms 15196 KB Output is correct
9 Correct 9 ms 15196 KB Output is correct
10 Correct 9 ms 15196 KB Output is correct
11 Correct 8 ms 15196 KB Output is correct
12 Correct 8 ms 15268 KB Output is correct
13 Correct 9 ms 15448 KB Output is correct
14 Correct 8 ms 15356 KB Output is correct
15 Correct 8 ms 15380 KB Output is correct
16 Correct 9 ms 15196 KB Output is correct
17 Correct 9 ms 15344 KB Output is correct
18 Correct 9 ms 15192 KB Output is correct
19 Correct 8 ms 15184 KB Output is correct
20 Correct 10 ms 15196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 28804 KB Output is correct
2 Correct 149 ms 42164 KB Output is correct
3 Correct 122 ms 44408 KB Output is correct
4 Correct 149 ms 45488 KB Output is correct
5 Correct 153 ms 54324 KB Output is correct
6 Correct 136 ms 54448 KB Output is correct
7 Correct 135 ms 41016 KB Output is correct
8 Correct 150 ms 40632 KB Output is correct
9 Correct 166 ms 55984 KB Output is correct
10 Correct 140 ms 55984 KB Output is correct
11 Correct 181 ms 55028 KB Output is correct
12 Correct 202 ms 54960 KB Output is correct
13 Correct 187 ms 55216 KB Output is correct
14 Correct 155 ms 54968 KB Output is correct
15 Correct 166 ms 51144 KB Output is correct
16 Correct 144 ms 49584 KB Output is correct
17 Correct 127 ms 49136 KB Output is correct
18 Correct 125 ms 31856 KB Output is correct
19 Correct 95 ms 31804 KB Output is correct
20 Correct 119 ms 41656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14684 KB Output is correct
2 Correct 8 ms 14924 KB Output is correct
3 Correct 9 ms 15196 KB Output is correct
4 Correct 10 ms 15192 KB Output is correct
5 Correct 13 ms 15704 KB Output is correct
6 Correct 10 ms 15464 KB Output is correct
7 Correct 9 ms 15452 KB Output is correct
8 Correct 8 ms 15196 KB Output is correct
9 Correct 9 ms 15196 KB Output is correct
10 Correct 9 ms 15196 KB Output is correct
11 Correct 8 ms 15196 KB Output is correct
12 Correct 8 ms 15268 KB Output is correct
13 Correct 9 ms 15448 KB Output is correct
14 Correct 8 ms 15356 KB Output is correct
15 Correct 8 ms 15380 KB Output is correct
16 Correct 9 ms 15196 KB Output is correct
17 Correct 9 ms 15344 KB Output is correct
18 Correct 9 ms 15192 KB Output is correct
19 Correct 8 ms 15184 KB Output is correct
20 Correct 10 ms 15196 KB Output is correct
21 Correct 104 ms 28804 KB Output is correct
22 Correct 149 ms 42164 KB Output is correct
23 Correct 122 ms 44408 KB Output is correct
24 Correct 149 ms 45488 KB Output is correct
25 Correct 153 ms 54324 KB Output is correct
26 Correct 136 ms 54448 KB Output is correct
27 Correct 135 ms 41016 KB Output is correct
28 Correct 150 ms 40632 KB Output is correct
29 Correct 166 ms 55984 KB Output is correct
30 Correct 140 ms 55984 KB Output is correct
31 Correct 181 ms 55028 KB Output is correct
32 Correct 202 ms 54960 KB Output is correct
33 Correct 187 ms 55216 KB Output is correct
34 Correct 155 ms 54968 KB Output is correct
35 Correct 166 ms 51144 KB Output is correct
36 Correct 144 ms 49584 KB Output is correct
37 Correct 127 ms 49136 KB Output is correct
38 Correct 125 ms 31856 KB Output is correct
39 Correct 95 ms 31804 KB Output is correct
40 Correct 119 ms 41656 KB Output is correct
41 Correct 284 ms 59084 KB Output is correct
42 Correct 146 ms 49764 KB Output is correct
43 Correct 121 ms 41860 KB Output is correct
44 Correct 270 ms 60840 KB Output is correct
45 Correct 349 ms 70068 KB Output is correct
46 Correct 358 ms 70832 KB Output is correct
47 Correct 356 ms 72448 KB Output is correct
48 Correct 313 ms 71856 KB Output is correct
49 Correct 332 ms 71940 KB Output is correct
50 Correct 366 ms 56504 KB Output is correct
51 Correct 357 ms 55716 KB Output is correct
52 Correct 394 ms 71596 KB Output is correct
53 Correct 449 ms 73436 KB Output is correct
54 Correct 384 ms 71860 KB Output is correct
55 Correct 343 ms 70836 KB Output is correct
56 Correct 251 ms 65464 KB Output is correct
57 Correct 243 ms 66152 KB Output is correct
58 Correct 254 ms 67760 KB Output is correct
59 Correct 236 ms 66716 KB Output is correct
60 Correct 232 ms 66744 KB Output is correct
61 Correct 228 ms 66916 KB Output is correct
62 Correct 370 ms 62964 KB Output is correct
63 Correct 318 ms 47924 KB Output is correct
64 Correct 301 ms 48308 KB Output is correct
65 Correct 321 ms 48744 KB Output is correct
66 Correct 288 ms 48944 KB Output is correct
67 Correct 295 ms 49100 KB Output is correct
68 Correct 285 ms 48444 KB Output is correct
69 Correct 254 ms 60596 KB Output is correct
70 Correct 239 ms 57264 KB Output is correct
71 Correct 254 ms 58296 KB Output is correct
72 Correct 248 ms 58548 KB Output is correct