trafik

sparse

Feb 28th, 2022
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <set>
  5. #include <deque>
  6. #include <queue>
  7. #include <algorithm>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <iomanip>
  12. #include <fstream>
  13. #define all(v) v.begin(), v.end()
  14. #define ll long long
  15. #define lb lower_bound
  16. #define ub upper_bound
  17. #define len(v) (int)v.size()
  18. using namespace std;
  19. const ll inf = 1e18;
  20. const ll MAXN = 1e5 + 10;
  21. const int logn = 20;
  22. template<class T>
  23. istream& operator>>(istream& in, vector<T>& a) {
  24.     for (auto& i : a)
  25.         in >> i;
  26.     return in;
  27. }
  28. template<class T>
  29. ostream& operator<<(ostream& out, vector<T>& a) {
  30.     for (auto& i : a)
  31.         out << i << " ";
  32.     return out;
  33. }
  34.  
  35. ll n, m, a1, u, v;
  36. ll a[MAXN];
  37. vector<ll> log_2;
  38. ll dp[MAXN][logn];
  39.  
  40. void initRMQ() {
  41.     int i, j;
  42.     for (i = 1; i <= n; i++) dp[i][0] = i;
  43.     for (j = 1; (1 << j) <= n; j++) {
  44.         for (i = 1; i + (1 << j) - 1 <= n; i++) {
  45.             if (a[dp[i][j - 1]] < a[dp[i + (1 << (j - 1))][j - 1]])
  46.                 dp[i][j] = dp[i][j - 1];
  47.             else dp[i][j] = dp[i + (1 << (j - 1))][j - 1];
  48.         }
  49.     }
  50. }
  51.  
  52. ll get(ll l, ll r) {
  53.     ll k = (ll)(log(1.0 * r - l + 1) / log(2.0));
  54.     ll res = dp[l][k];
  55.     if (a[dp[r - (1 << k) + 1][k]] < a[res])
  56.         res = dp[r - (1 << k) + 1][k];
  57.     return res;
  58. }
  59.  
  60. void solve() {
  61.     ifstream fin("sparse.in");
  62.     ofstream fout("sparse.out");
  63.     fin >> n >> m >> a1 >> u >> v;
  64.  
  65.     a[1] = a1;
  66.     for (int i = 2; i <= n; i++) {
  67.         a[i] = (23 * a[i - 1] + 21563) % 16714589;
  68.     }
  69.     initRMQ();
  70.     ll ans;
  71.     for (int i = 1; i <= m; ++i) {
  72.         ll u_ = u;
  73.         ll v_ = v;
  74.         if (u_ > v_) swap(u_, v_);
  75.         ans = a[get(u_, v_)];
  76.         if (i == m) break;
  77.         u = ((17 * u + 751 + ans + 2 * i) % n) + 1;
  78.         v = ((13 * v + 593 + ans + 5 * i) % n) + 1;
  79.     }
  80.     fout << u << " " << v << " " << ans;
  81. }
  82.  
  83. signed main() {
  84.     ios::sync_with_stdio(false);
  85.     cin.tie(nullptr);
  86.     cout.tie(nullptr);
  87.  
  88.     solve();
  89. };
Advertisement
Add Comment
Please, Sign In to add comment