|
| 1 | +/* |
| 2 | + Idea: |
| 3 | + - Segment Tree. |
| 4 | + - In the beginning each element `x` equal to its value, |
| 5 | + and after each query the element with value `x` will |
| 6 | + change to be equal to `y`. |
| 7 | + - To maintain the previous effect we can use any data |
| 8 | + structure like Segment Tree, each node in the segment |
| 9 | + tree contains the mapping between the real values and |
| 10 | + the current values. |
| 11 | + - We should use lazy propagation to pass the time limit. |
| 12 | +*/ |
| 13 | + |
| 14 | +#include <bits/stdc++.h> |
| 15 | + |
| 16 | +using namespace std; |
| 17 | + |
| 18 | +int const N = 2e5 + 1, M = 1e2 + 1; |
| 19 | +int n, q, s, e, x, y, tar, a[N], seg[4 * N][M], lazy[4 * N]; |
| 20 | + |
| 21 | +void build(int at, int l, int r) { |
| 22 | + for(int i = 1; i < M; ++i) |
| 23 | + seg[at][i] = i; |
| 24 | + |
| 25 | + if(l == r) |
| 26 | + return; |
| 27 | + |
| 28 | + int mid = (l + r) >> 1; |
| 29 | + build(at << 1, l, mid); |
| 30 | + build(at << 1 | 1, mid + 1, r); |
| 31 | +} |
| 32 | + |
| 33 | +void pro(int at, int l, int r) { |
| 34 | + if(l != r) { |
| 35 | + lazy[at << 1] = lazy[at << 1 | 1] = 1; |
| 36 | + |
| 37 | + for(int i = 1; i < M; ++i) |
| 38 | + seg[at << 1][i] = seg[at][seg[at << 1][i]], |
| 39 | + seg[at << 1 | 1][i] = seg[at][seg[at << 1 | 1][i]]; |
| 40 | + |
| 41 | + lazy[at] = 0; |
| 42 | + for(int i = 1; i < M; ++i) |
| 43 | + seg[at][i] = i; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +void update(int at, int l, int r) { |
| 48 | + if(lazy[at] != 0) |
| 49 | + pro(at, l, r); |
| 50 | + |
| 51 | + if(l > e || r < s) |
| 52 | + return; |
| 53 | + |
| 54 | + if(l >= s && r <= e) { |
| 55 | + lazy[at] = 1; |
| 56 | + for(int i = 1; i < M; ++i) |
| 57 | + if(seg[at][i] == x) |
| 58 | + seg[at][i] = y; |
| 59 | + pro(at, l, r); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + int mid = (l + r) >> 1; |
| 64 | + update(at << 1, l, mid); |
| 65 | + update(at << 1 | 1, mid + 1, r); |
| 66 | +} |
| 67 | + |
| 68 | +int get(int at, int l, int r) { |
| 69 | + if(lazy[at] != 0) |
| 70 | + pro(at, l, r); |
| 71 | + |
| 72 | + if(l > e || r < s) |
| 73 | + return 0; |
| 74 | + |
| 75 | + if(l >= s && r <= e) |
| 76 | + return seg[at][tar]; |
| 77 | + |
| 78 | + int mid = (l + r) >> 1; |
| 79 | + return get(at << 1, l, mid) + get(at << 1 | 1, mid + 1, r); |
| 80 | +} |
| 81 | + |
| 82 | +int main() { |
| 83 | + scanf("%d", &n); |
| 84 | + for(int i = 1; i <= n; ++i) |
| 85 | + scanf("%d", a + i); |
| 86 | + |
| 87 | + build(1, 1, n); |
| 88 | + |
| 89 | + scanf("%d", &q); |
| 90 | + for(int i = 0; i < q; ++i) { |
| 91 | + scanf("%d %d %d %d", &s, &e, &x, &y); |
| 92 | + update(1, 1, n); |
| 93 | + } |
| 94 | + |
| 95 | + for(int i = 1; i <= n; ++i) { |
| 96 | + s = e = i; |
| 97 | + tar = a[i]; |
| 98 | + printf("%s%d", i == 1 ? "" : " ", get(1, 1, n)); |
| 99 | + } |
| 100 | + puts(""); |
| 101 | + |
| 102 | + return 0; |
| 103 | +} |
0 commit comments