Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "bits/stdc++.h"
- using namespace std;
- #define all(a) a.begin(), a.end()
- mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
- typedef long double ld;
- #define int long long
- const int N = 70000 + 1, sqrt_c = 320, mod = 1e13 + 7;
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n;
- cin >> n;
- vector<int> p(n), q(n), q_mod(n), cur(n);
- for (int i = 0; i < n; i++) {
- string s;
- cin >> s;
- int log_pw = 1;
- for (int j = (int)s.size() - 1; j >= 0; j--) {
- q[i] = (q[i] + (int)(s[j] - '0') * log_pw) % mod;
- log_pw = (log_pw * 10) % mod;
- }
- }
- unordered_map<int, int> cnt[sqrt_c];
- int sum = 1;
- q_mod[0] = 1;
- for (int i = 1; i < n; i++) {
- q_mod[i] = (q[i] + mod - q[i - 1]) % mod;
- }
- for (int i = 0; i < n; i++) {
- cur[i] = (sum - q_mod[i] + mod) % mod;
- sum = (sum + q_mod[i]) % mod;
- }
- for (int i = 0; i < n; i++) {
- cnt[i / sqrt_c][cur[i]]++;
- }
- vector<int> b(sqrt_c);
- auto find = [&]() {
- for (int it = (n - 1) / sqrt_c; it >= 0; it--) {
- if (cnt[it].find(b[it]) != cnt[it].end()) {
- return it;
- }
- }
- };
- for (int i = n; i >= 1; i--) {
- int pos = find();
- int f = min((pos + 1ll) * sqrt_c - 1ll, n - 1ll);
- while (p[f] != 0 || cur[f] != b[pos]) {
- f--;
- }
- cnt[pos][cur[f]]--;
- if (cnt[pos][cur[f]] == 0) {
- cnt[pos].erase(cur[f]);
- }
- p[f] = i;
- for (int it = f; (it / sqrt_c) == pos && it < n; it++) {
- if (p[it] != 0) {
- continue;
- }
- cnt[pos][cur[it]]--;
- if (cnt[pos][cur[it]] == 0) {
- cnt[pos].erase(cur[it]);
- }
- cur[it] = (cur[it] + mod - q_mod[f]) % mod;
- cnt[pos][cur[it]]++;
- }
- for (int it = pos + 1; it <= (n - 1) / sqrt_c; it++) {
- b[it] = (b[it] + q_mod[f]) % mod;
- }
- }
- for (int i = 0; i < n; i++) {
- cout << p[i] << ' ';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment