답안 #974099

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
974099 2024-05-02T19:31:10 Z Ooops_sorry 수확 (JOI20_harvest) C++14
0 / 100
132 ms 92940 KB
#include<iostream>
#include<deque>
#include<algorithm>
#include<vector>
#include<map>
#include<random>
#include<time.h>
#include<cassert>
#include<chrono>
#include<set>
#include<unordered_set>
#include<array>
 
using namespace std;
 
#define ull unsigned long long
#define pb push_back
#define ld long double
#define ll long long
#define all(a) a.begin(), a.end()
#define int long long 

mt19937_64 rnd(51);

const int N = 4e5 + 10;

vector<int> mas[N];

vector<pair<int, int>> g[N];
bool on_cycle[N];
int go[N];

ll ans[N];
ll dep[N], sz[N];
vector<pair<ll, int>> query[N];

void zhfs(int v) {
  sz[v] = mas[v].size();
  for (auto [u, c] : g[v]) {
    if (!on_cycle[u]) {
      dep[u] = dep[v] + c;
      sz[v] += sz[u];
      zhfs(u);
    }
  }
}

vector<ll> st[N];

void dfs(int v) {
  for (auto i : mas[v]) {
    st[v].pb(i + dep[v]);
  }
  for (auto [u, c] : g[v]) {
    if (!on_cycle[u]) {
      dfs(u);
      if (st[u].size() > st[v].size()) {
        swap(st[u], st[v]);
      }
    }
  }
  for (auto [u, c] : g[v]) {
    if (!on_cycle[u]) {
      for (int i : st[u]) {
        st[v].pb(i);
      }
    }
  }
  if (!on_cycle[v]) {
    for (auto [t, i] : query[v]) {
      for (auto val : st[v]) {
        if (val - dep[v] <= t) {
          ans[i]++;
        }
      }
    }
  }
}

signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif // LOCAL
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m, l, c;
  cin >> n >> m >> l >> c;
  int left = (c / l) * l;
  c %= l;
  vector<int> a(n), b(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v;
    ll t;
    cin >> v >> t;
    v--;
    query[v].pb({t, i});
  }
  int j = 0;
  while (j < b.size() && b[j] < a[0]) {
    mas[n - 1].pb(b[j] + l - a[n - 1]);
    j++;
  }
  for (int i = 0; i < n; i++) {
    while (j < b.size() && (i == n - 1 || a[i + 1] > b[j])) {
      mas[i].pb(b[j] - a[i]);
      j++;
    }
  } 
  assert(j == b.size());
  for (int i = 0; i < n; i++) {
    a.pb(a[i] + l);
  }
  j = 0;
  for (int i = n; i < 2 * n; i++) {
    while (j + 1 < 2 * n && a[i] - a[j + 1] >= c) {
      j++;
    } 
    go[i % n] = j % n;
    g[j % n].pb({i % n, a[i] - a[j] + left});
  }
  vector<bool> used(n);
  vector<deque<int>> cycles;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      int j = i;
      map<int, int> here;
      deque<int> arr;
      while (!used[j]) {
        used[j] = 1;
        here[j] = 1;
        arr.pb(j);
        j = go[j];
      }
      if (here[j]) {
        while (arr.front() != j) {
          arr.pop_front();
        }
        cycles.pb(arr);
        for (int v : arr) {
          on_cycle[v] = 1;
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (on_cycle[i]) {
      zhfs(i);
      dfs(i);
    }
  }
  for (auto cycle : cycles) {
    int sz = cycle.size();
    vector<ll> pr(sz + 1);
    for (int i = 0; i < sz; i++) {
      int u = cycle[i], v = cycle[(i + 1) % sz];
      if (a[u] >= a[v]) {
        pr[i + 1] = pr[i] + a[u] - a[v] + left;
      } else {
        pr[i + 1] = pr[i] + a[u] + l - a[v] + left;
      }
    }
    for (int i = 0; i < sz; i++) {
      int v = cycle[i];
      for (auto [t, ind] : query[v]) {
        for (int j = 0; j < sz; j++) {
          for (auto val : st[cycle[j]]) {
            if (val + pr[i] - pr[j] <= t) {
              ans[ind] += t / pr.back() - (val + pr[i] - pr[j]) / pr.back();
              if ((val + pr[i] - pr[j]) % pr.back() <= t % pr.back()) {
                ans[ind]++;
              }
            }
          }
        } 
        for (int j = i + 1; j < sz; j++) {
          for (auto val : st[cycle[j]]) {
            ans[ind] += t / pr.back() - (val + pr[i] - pr[j]) / pr.back() - 1;
            if ((val + pr[i] - pr[j]) % pr.back() <= t % pr.back()) {
              ans[ind]++;
            }
          }
        }
      }
    }
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
  return 0; 
}

Compilation message

harvest.cpp: In function 'void zhfs(long long int)':
harvest.cpp:39:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   39 |   for (auto [u, c] : g[v]) {
      |             ^
harvest.cpp: In function 'void dfs(long long int)':
harvest.cpp:54:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   54 |   for (auto [u, c] : g[v]) {
      |             ^
harvest.cpp:62:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   62 |   for (auto [u, c] : g[v]) {
      |             ^
harvest.cpp:70:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   70 |     for (auto [t, i] : query[v]) {
      |               ^
harvest.cpp: In function 'int main()':
harvest.cpp:107:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |   while (j < b.size() && b[j] < a[0]) {
      |          ~~^~~~~~~~~~
harvest.cpp:112:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |     while (j < b.size() && (i == n - 1 || a[i + 1] > b[j])) {
      |            ~~^~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
                 from harvest.cpp:8:
harvest.cpp:117:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |   assert(j == b.size());
      |          ~~^~~~~~~~~~~
harvest.cpp:172:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  172 |       for (auto [t, ind] : query[v]) {
      |                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 42844 KB Output is correct
2 Correct 17 ms 44980 KB Output is correct
3 Correct 132 ms 45328 KB Output is correct
4 Incorrect 16 ms 45144 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 81 ms 92940 KB Execution killed with signal 8
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 42844 KB Output is correct
2 Correct 17 ms 44980 KB Output is correct
3 Correct 132 ms 45328 KB Output is correct
4 Incorrect 16 ms 45144 KB Output isn't correct
5 Halted 0 ms 0 KB -