#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SIZE(a) ((int)a.size()) typedef long long ll; const int inf = 1000*1000*1000 + 7; // Biblitotczka UJ, wersja WF 2017. // Vladyslav Hlembotskyi const int N = 500005; const int M = 25 * N; // dostosowac int l[M], r[M], cnt[M], cur = 1; int h[2][M]; int p[2][N], mod[2], seed[2] = {1010102011, 133333331}; int n, m; int a[N]; int prime(int n) { if (n == 1) return false; for (int i = 2; i*i <= n; ++i) { if (n % i == 0) { return false; } } return true; } void precalc() { REP(i, 2) { mod[i] = rand() % 10000005 + 1010102016; while (!prime(mod[i])) ++mod[i]; p[i][0] = 1; for (int j = 1; j < N; ++j) { p[i][j] = (1LL*p[i][j-1]*seed[i]) % mod[i]; } } } inline void merge(int v, int l, int r) { REP(i, 2) h[i][v] = (h[i][l] + 1LL*h[i][r]*p[i][cnt[l]]) % mod[i]; cnt[v] = cnt[l] + cnt[r]; } int build(int tl, int tr) { int v = cur++; if (tl == tr) { l[v] = r[v] = 0; cnt[v] = 1; REP(i, 2) h[i][v] = a[tl] + 1; return v; } int tm = (tl + tr) / 2; l[v] = build(tl, tm); r[v] = build(tm+1, tr); merge(v, l[v], r[v]); return v; } int update(int v, int tl, int tr, int pos, int val) { int nv = cur++; if (tl == tr) { l[nv] = r[nv] = 0; cnt[nv] = 1; REP(i, 2) h[i][nv] = val + 1; return nv; } int tm = (tl + tr) / 2; if (pos <= tm) { l[nv] = update(l[v], tl, tm, pos, val); r[nv] = r[v]; } else { l[nv] = l[v]; r[nv] = update(r[v], tm+1, tr, pos, val); } merge(nv, l[nv], r[nv]); return nv; } int compare(int x, int y) { if (h[0][x] == h[0][y] && h[1][x] == h[1][y]) return 0; if (cnt[x] == 1) { return h[0][x] < h[0][y] ? -1 : +1; } if (h[0][l[x]] != h[0][l[y]] || h[1][l[x]] != h[1][l[y]]) { return compare(l[x], l[y]); } return compare(r[x], r[y]); } int roots[N], result[N]; int cmp(int x, int y) { int val = compare(roots[x], roots[y]); return val < 0 || (!val && x < y); } int main() { ios::sync_with_stdio(false); cin.tie(0); srand(time(0)); precalc(); cin >> n >> m; REP(i, n) cin >> a[i]; roots[0] = build(0, n - 1); for (int i = 1; i < m; ++i) { int pos, val; cin >> pos >> val; --pos; roots[i] = update(roots[i - 1], 0, n - 1, pos, val+1); } // REP(i, m) { // cout << "COMPARING: " << 3 << ' ' << i+1 << endl; // cout << compare(roots[2], roots[i]) << endl; // } // exit(0); REP(i, m) result[i] = i; sort(result, result + m, cmp); REP(i, m) cout << result[i]+1 << ' '; cout << '\n'; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SIZE(a) ((int)a.size()) typedef long long ll; const int inf = 1000*1000*1000 + 7; // Biblitotczka UJ, wersja WF 2017. // Vladyslav Hlembotskyi const int N = 500005; const int M = 25 * N; // dostosowac int l[M], r[M], cnt[M], cur = 1; int h[2][M]; int p[2][N], mod[2], seed[2] = {1010102011, 133333331}; int n, m; int a[N]; int prime(int n) { if (n == 1) return false; for (int i = 2; i*i <= n; ++i) { if (n % i == 0) { return false; } } return true; } void precalc() { REP(i, 2) { mod[i] = rand() % 10000005 + 1010102016; while (!prime(mod[i])) ++mod[i]; p[i][0] = 1; for (int j = 1; j < N; ++j) { p[i][j] = (1LL*p[i][j-1]*seed[i]) % mod[i]; } } } inline void merge(int v, int l, int r) { REP(i, 2) h[i][v] = (h[i][l] + 1LL*h[i][r]*p[i][cnt[l]]) % mod[i]; cnt[v] = cnt[l] + cnt[r]; } int build(int tl, int tr) { int v = cur++; if (tl == tr) { l[v] = r[v] = 0; cnt[v] = 1; REP(i, 2) h[i][v] = a[tl] + 1; return v; } int tm = (tl + tr) / 2; l[v] = build(tl, tm); r[v] = build(tm+1, tr); merge(v, l[v], r[v]); return v; } int update(int v, int tl, int tr, int pos, int val) { int nv = cur++; if (tl == tr) { l[nv] = r[nv] = 0; cnt[nv] = 1; REP(i, 2) h[i][nv] = val + 1; return nv; } int tm = (tl + tr) / 2; if (pos <= tm) { l[nv] = update(l[v], tl, tm, pos, val); r[nv] = r[v]; } else { l[nv] = l[v]; r[nv] = update(r[v], tm+1, tr, pos, val); } merge(nv, l[nv], r[nv]); return nv; } int compare(int x, int y) { if (h[0][x] == h[0][y] && h[1][x] == h[1][y]) return 0; if (cnt[x] == 1) { return h[0][x] < h[0][y] ? -1 : +1; } if (h[0][l[x]] != h[0][l[y]] || h[1][l[x]] != h[1][l[y]]) { return compare(l[x], l[y]); } return compare(r[x], r[y]); } int roots[N], result[N]; int cmp(int x, int y) { int val = compare(roots[x], roots[y]); return val < 0 || (!val && x < y); } int main() { ios::sync_with_stdio(false); cin.tie(0); srand(time(0)); precalc(); cin >> n >> m; REP(i, n) cin >> a[i]; roots[0] = build(0, n - 1); for (int i = 1; i < m; ++i) { int pos, val; cin >> pos >> val; --pos; roots[i] = update(roots[i - 1], 0, n - 1, pos, val+1); } // REP(i, m) { // cout << "COMPARING: " << 3 << ' ' << i+1 << endl; // cout << compare(roots[2], roots[i]) << endl; // } // exit(0); REP(i, m) result[i] = i; sort(result, result + m, cmp); REP(i, m) cout << result[i]+1 << ' '; cout << '\n'; return 0; } |