ivnikkk

Untitled

Sep 27th, 2022
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "bits/stdc++.h"
  3. using namespace std;
  4. #define all(a) a.begin(), a.end()
  5. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  6. typedef long double ld;
  7. #define int long long
  8. const int N = 70000 + 1, sqrt_c = 320, mod = 1e13 + 7;
  9.  
  10. signed main() {
  11. #ifdef _DEBUG
  12.     freopen("input.txt", "r", stdin);
  13.     freopen("output.txt", "w", stdout);
  14. #endif
  15.     ios_base::sync_with_stdio(false);
  16.     cin.tie(nullptr);
  17.     cout.tie(nullptr);
  18.     int n;
  19.     cin >> n;
  20.     vector<int> p(n), q(n), q_mod(n), cur(n);
  21.     for (int i = 0; i < n; i++) {
  22.         string s;
  23.         cin >> s;
  24.         int log_pw = 1;
  25.         for (int j = (int)s.size() - 1; j >= 0; j--) {
  26.             q[i] = (q[i] + (int)(s[j] - '0') * log_pw) % mod;
  27.             log_pw = (log_pw * 10) % mod;
  28.         }
  29.     }
  30.     unordered_map<int, int> cnt[sqrt_c];
  31.     int sum = 1;
  32.     q_mod[0] = 1;
  33.     for (int i = 1; i < n; i++) {
  34.         q_mod[i] = (q[i] + mod - q[i - 1]) % mod;
  35.     }
  36.     for (int i = 0; i < n; i++) {
  37.         cur[i] = (sum - q_mod[i] + mod) % mod;
  38.         sum = (sum + q_mod[i]) % mod;
  39.     }
  40.     for (int i = 0; i < n; i++) {
  41.         cnt[i / sqrt_c][cur[i]]++;
  42.     }
  43.     vector<int> b(sqrt_c);
  44.     auto find = [&]() {
  45.         for (int it = (n - 1) / sqrt_c; it >= 0; it--) {
  46.             if (cnt[it].find(b[it]) != cnt[it].end()) {
  47.                 return it;
  48.             }
  49.         }
  50.     };
  51.     for (int i = n; i >= 1; i--) {
  52.         int pos = find();
  53.         int f = min((pos + 1ll) * sqrt_c - 1ll, n - 1ll);
  54.         while (p[f] != 0 || cur[f] != b[pos]) {
  55.             f--;
  56.         }
  57.         cnt[pos][cur[f]]--;
  58.         if (cnt[pos][cur[f]] == 0) {
  59.             cnt[pos].erase(cur[f]);
  60.         }
  61.         p[f] = i;
  62.         for (int it = f; (it / sqrt_c) == pos && it < n; it++) {
  63.             if (p[it] != 0) {
  64.                 continue;
  65.             }
  66.             cnt[pos][cur[it]]--;
  67.             if (cnt[pos][cur[it]] == 0) {
  68.                 cnt[pos].erase(cur[it]);
  69.             }
  70.             cur[it] = (cur[it] + mod - q_mod[f]) % mod;
  71.             cnt[pos][cur[it]]++;
  72.         }
  73.         for (int it = pos + 1; it <= (n - 1) / sqrt_c; it++) {
  74.             b[it] = (b[it] + q_mod[f]) % mod;
  75.         }
  76.     }
  77.  
  78.     for (int i = 0; i < n; i++) {
  79.         cout << p[i] << ' ';
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment