Submission #216978

# Submission time Handle Problem Language Result Execution time Memory
216978 2020-03-28T15:30:44 Z IOrtroiii Harvest (JOI20_harvest) C++14
5 / 100
1796 ms 52980 KB
/*
5 3 20 6
0 4 8 12 16
2 11 14
9
4 1932
2 93787
1 89
5 98124798
1 2684
1 137598
3 2
3 8375
4 237
*/
#include <bits/stdc++.h>

#include <ext/pb_ds/tree_policy.hpp>

#include <ext/pb_ds/assoc_container.hpp>

using namespace std;

using namespace __gnu_pbds;

using ll = int64_t;
using ull = uint64_t;
template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int main() {
   ios_base::sync_with_stdio(false);
   int N, M; ll L, C;
   cin >> N >> M >> L >> C;
   vector<ll> A(N);
   for (int i = 0; i < N; ++i) cin >> A[i], A[i] = L - 1 - A[i]; // for convenience
   reverse(A.begin(), A.end());
   vector<int> nxt(N);
   vector<ll> dist(N);
   {
      for (int v = 0; v < N; ++v) {
         ll newA = (A[v] + C) % L;
         int z = lower_bound(A.begin(), A.end(), newA) - A.begin();
         if (z == N) {
            nxt[v] = 0;
            dist[v] = C + L + A[0] - newA;
         } else {
            nxt[v] = z;
            dist[v] = C + A[z] - newA;
         }
      }
   }

   vector<vector<ll>> adds(N);
   while (M--) {
      ll B;
      cin >> B;
      B = L - 1 - B;
      int z = lower_bound(A.begin(), A.end(), B) - A.begin();
      if (z == N) {
         adds[0].emplace_back(L + A[0] - B);
      } else {
         adds[z].emplace_back(A[z] - B);
      }
   }

   int Q;
   cin >> Q;
   vector<vector<pair<ll, int>>> qs(N);
   vector<ll> ans(Q);
   for (int i = 0; i < Q; ++i) {
      int v; ll T;
      cin >> v >> T;
      v = N - v;
      qs[v].emplace_back(T, i);
   }
   vector<bool> isRoot(N);
   vector<int> visited(N, 0);
   for (int i = 0; i < N; ++i) {
      int v = i;
      while (visited[v] == 0) {
         visited[v] = 1;
         v = nxt[v];
      }
      if (visited[v] == 1) isRoot[v] = true;
      v = i;
      while (visited[v] == 1) {
         visited[v] = 2;
         v = nxt[v];
      }
   }
   vector<vector<int>> adj(N);
   for (int v = 0; v < N; ++v) {
      if (!isRoot[v]) {
         adj[nxt[v]].emplace_back(v);
      }
   }
   vector<ll> distToRoot(N);
   vector<ordered_set<pair<ll, ull>>> vals(N);
   for (int i = 0; i < N; ++i) {
      if (!isRoot[i]) continue;
      function<void(int)> dfs1 = [&](int v) {
         for (int u : adj[v]) {
            distToRoot[u] = distToRoot[v] + dist[u];
            dfs1(u);
         }
      };
      dfs1(i);
      function<void(int)> dfs2 = [&](int v) {
         for (auto z : adds[v]) {
            vals[v].insert({distToRoot[v] + z, rng()});
         }
         for (int u : adj[v]) {
            dfs2(u);
            /*
            if (int(vals[v].size()) < int(vals[u].size())) {
               vals[v].swap(vals[u]);
            }
            */
            for (auto z : vals[u]) {
               vals[v].insert(z);
            }
            vals[u] = {};
         }
         for (auto q : qs[v]) {
            ans[q.second] += vals[v].order_of_key({q.first + distToRoot[v], -1});
         }
      };
      dfs2(i);
      vector<pair<ll, int>> cur_qs;
      ll cycleLen = distToRoot[nxt[i]] + dist[i];
      int v = i;
      while (true) {
         for (auto q : qs[v]) {
            cur_qs.emplace_back(q.first - (cycleLen - distToRoot[v]), q.second); // must pass through removed edge
         }
         v = nxt[v];
         if (v == i) break;
      }
      for (auto z : vals[i]) cur_qs.emplace_back(z.first, -1);
      sort(cur_qs.begin(), cur_qs.end());
      ordered_set<pair<ll, ull>> st;
      ll subt = 0;
      for (auto q : cur_qs) {
         if (q.second == -1) {
            st.insert({q.first, rng()});
            subt += (q.first / cycleLen);
         } else {
            ans[q.second] += (ll(q.first / cycleLen) * ll(st.size()) + st.order_of_key({q.first % cycleLen, -1}) - subt);
         }
      }
   }
   for (int i = 0; i < Q; ++i) cout << ans[i] << "\n";
   return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 640 KB Output is correct
2 Correct 9 ms 1280 KB Output is correct
3 Correct 10 ms 1536 KB Output is correct
4 Correct 49 ms 1532 KB Output is correct
5 Correct 808 ms 2424 KB Output is correct
6 Correct 829 ms 2300 KB Output is correct
7 Correct 823 ms 2348 KB Output is correct
8 Correct 10 ms 1536 KB Output is correct
9 Correct 10 ms 1536 KB Output is correct
10 Correct 10 ms 1536 KB Output is correct
11 Correct 10 ms 1536 KB Output is correct
12 Correct 843 ms 2424 KB Output is correct
13 Correct 817 ms 2552 KB Output is correct
14 Correct 395 ms 2036 KB Output is correct
15 Correct 386 ms 2040 KB Output is correct
16 Correct 398 ms 2044 KB Output is correct
17 Correct 401 ms 2040 KB Output is correct
18 Correct 418 ms 2040 KB Output is correct
19 Correct 405 ms 1912 KB Output is correct
20 Correct 413 ms 1912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 11396 KB Output is correct
2 Incorrect 1796 ms 52980 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 640 KB Output is correct
2 Correct 9 ms 1280 KB Output is correct
3 Correct 10 ms 1536 KB Output is correct
4 Correct 49 ms 1532 KB Output is correct
5 Correct 808 ms 2424 KB Output is correct
6 Correct 829 ms 2300 KB Output is correct
7 Correct 823 ms 2348 KB Output is correct
8 Correct 10 ms 1536 KB Output is correct
9 Correct 10 ms 1536 KB Output is correct
10 Correct 10 ms 1536 KB Output is correct
11 Correct 10 ms 1536 KB Output is correct
12 Correct 843 ms 2424 KB Output is correct
13 Correct 817 ms 2552 KB Output is correct
14 Correct 395 ms 2036 KB Output is correct
15 Correct 386 ms 2040 KB Output is correct
16 Correct 398 ms 2044 KB Output is correct
17 Correct 401 ms 2040 KB Output is correct
18 Correct 418 ms 2040 KB Output is correct
19 Correct 405 ms 1912 KB Output is correct
20 Correct 413 ms 1912 KB Output is correct
21 Correct 156 ms 11396 KB Output is correct
22 Incorrect 1796 ms 52980 KB Output isn't correct
23 Halted 0 ms 0 KB -