#include <bits/stdc++.h> using namespace std; const int N = 5e5; long long mod = ((long long) 1e18) + 3; long long wsp[N + 7]; long long xx = 738957185983571L; int n, m; const int M = (1 << 19); long long mnoz(long long a, long long b) { long long res = 0; while(b > 0) { if(b & 1) res = (res + a) % mod; a = (a + a) % mod; b /= 2; } return res; } struct node { long long val; node *l; node *r; node() { l = r = 0; val = 0; } }; node T[2 * M + 20 * (N + 7)]; int gi = 0; node *kor[N + 7]; int t[N + 7]; int ot[N + 7]; node *insert(int x, long long w, node *v, int p = 0, int k = M - 1) { if(p == k) { gi++; T[gi].val = (v -> val) + w; if(T[gi].val > mod) T[gi].val -= mod; return &T[gi]; } int s = (p + k) / 2; node nowy; if(x <= s) { nowy.l = insert(x, w, v -> l, p, s); nowy.r = v -> r; } else { nowy.r = insert(x, w, v -> r, s + 1, k); nowy.l = v -> l; } nowy.val = (nowy.l -> val) + (nowy.r -> val); if(nowy.val > mod) nowy.val -= mod; gi++; T[gi] = nowy; return &T[gi]; } int lcp(int a, int b) { //cout << "lcp " << a << ' ' << b << " = "; int len = M; int wynik = 0; node *va = kor[a]; node *vb = kor[b]; while(1) { if(va -> val == vb -> val) { wynik += len; break; } if(len == 1) break; if(va -> l -> val == vb -> l -> val) { wynik += len / 2; va = va -> r; vb = vb -> r; } else { va = va -> l; vb = vb -> l; } len /= 2; } // cout << min(n + 1, wynik) << endl; return min(n + 1, wynik); } vector <pair <int, int>> zmi[N + 7]; int wart(int i, int j) { if(zmi[j].empty()) return t[j]; int res = t[j]; int p = 0; int k = (int) zmi[j].size() - 1; while(p <= k) { int s = (p + k) / 2; if(zmi[j][s].first <= i) { res = zmi[j][s].second; p = s + 1; } else k = s - 1; } return res; } int kol[N + 7]; bool cmp(int a, int b) { int len = lcp(a, b); if(len > n) return a < b; int wa = wart(a, len); int wb = wart(b, len); if(wa == wb) return a < b; return wa < wb; } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; // n = m = N; wsp[0] = 1; for(int i = 1; i <= n; ++i) wsp[i] = mnoz(xx, wsp[i - 1]); for(int i = 0; i < 2 * M; ++i) { T[i].l = &T[2 * i]; T[i].r = &T[2 * i + 1]; } kor[1] = &T[0]; gi = 2 * M; srand(385101); for(int i = 1; i <= n; ++i) { cin >> t[i]; //t[i] = rand() % 100000; ot[i] = t[i]; } for(int i = 2; i <= m; ++i) { int p, x; cin >> p >> x; //p = 1 + rand() % n; // x = rand() % 100000; zmi[p].push_back({i, x}); kor[i] = insert(p, mnoz(wsp[p], (mod + x - ot[p]) % mod), kor[i - 1]); ot[p] = x; } for(int i = 1; i <= m; ++i) kol[i] = i; sort(kol + 1, kol + m + 1, cmp); for(int i = 1; i <= m; ++i) cout << kol[i] << ' '; cout << endl; 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | #include <bits/stdc++.h> using namespace std; const int N = 5e5; long long mod = ((long long) 1e18) + 3; long long wsp[N + 7]; long long xx = 738957185983571L; int n, m; const int M = (1 << 19); long long mnoz(long long a, long long b) { long long res = 0; while(b > 0) { if(b & 1) res = (res + a) % mod; a = (a + a) % mod; b /= 2; } return res; } struct node { long long val; node *l; node *r; node() { l = r = 0; val = 0; } }; node T[2 * M + 20 * (N + 7)]; int gi = 0; node *kor[N + 7]; int t[N + 7]; int ot[N + 7]; node *insert(int x, long long w, node *v, int p = 0, int k = M - 1) { if(p == k) { gi++; T[gi].val = (v -> val) + w; if(T[gi].val > mod) T[gi].val -= mod; return &T[gi]; } int s = (p + k) / 2; node nowy; if(x <= s) { nowy.l = insert(x, w, v -> l, p, s); nowy.r = v -> r; } else { nowy.r = insert(x, w, v -> r, s + 1, k); nowy.l = v -> l; } nowy.val = (nowy.l -> val) + (nowy.r -> val); if(nowy.val > mod) nowy.val -= mod; gi++; T[gi] = nowy; return &T[gi]; } int lcp(int a, int b) { //cout << "lcp " << a << ' ' << b << " = "; int len = M; int wynik = 0; node *va = kor[a]; node *vb = kor[b]; while(1) { if(va -> val == vb -> val) { wynik += len; break; } if(len == 1) break; if(va -> l -> val == vb -> l -> val) { wynik += len / 2; va = va -> r; vb = vb -> r; } else { va = va -> l; vb = vb -> l; } len /= 2; } // cout << min(n + 1, wynik) << endl; return min(n + 1, wynik); } vector <pair <int, int>> zmi[N + 7]; int wart(int i, int j) { if(zmi[j].empty()) return t[j]; int res = t[j]; int p = 0; int k = (int) zmi[j].size() - 1; while(p <= k) { int s = (p + k) / 2; if(zmi[j][s].first <= i) { res = zmi[j][s].second; p = s + 1; } else k = s - 1; } return res; } int kol[N + 7]; bool cmp(int a, int b) { int len = lcp(a, b); if(len > n) return a < b; int wa = wart(a, len); int wb = wart(b, len); if(wa == wb) return a < b; return wa < wb; } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; // n = m = N; wsp[0] = 1; for(int i = 1; i <= n; ++i) wsp[i] = mnoz(xx, wsp[i - 1]); for(int i = 0; i < 2 * M; ++i) { T[i].l = &T[2 * i]; T[i].r = &T[2 * i + 1]; } kor[1] = &T[0]; gi = 2 * M; srand(385101); for(int i = 1; i <= n; ++i) { cin >> t[i]; //t[i] = rand() % 100000; ot[i] = t[i]; } for(int i = 2; i <= m; ++i) { int p, x; cin >> p >> x; //p = 1 + rand() % n; // x = rand() % 100000; zmi[p].push_back({i, x}); kor[i] = insert(p, mnoz(wsp[p], (mod + x - ot[p]) % mod), kor[i - 1]); ot[p] = x; } for(int i = 1; i <= m; ++i) kol[i] = i; sort(kol + 1, kol + m + 1, cmp); for(int i = 1; i <= m; ++i) cout << kol[i] << ' '; cout << endl; return 0; } |