#include <bits/stdc++.h> using namespace std; const int N = 6e5 + 7; const int treeSize = 1 << 19; const int Q = 1e6 + 9; const int Q2 = 1e6 + 23; const int MX = INT_MAX; const int MX2 = 1234567891; struct tree{ int hasz = 0, hasz2 = 0, pod = 0; tree *left = nullptr, *right = nullptr; tree(int _hasz = 0, int _hasz2 = 0, int _pod = 0, tree *_left = nullptr, tree *_right = nullptr){ hasz = _hasz; hasz2 = _hasz2; pod = _pod; left = _left; right = _right; } }; int n, m; tree *root[N]; int cnt = 0; tree poczatek[12 * Q]; int res[N]; int tab[N]; int pot[N]; int pot2[N]; inline void update(tree *cur){ cur -> pod = cur -> left -> pod + cur -> right -> pod; cur -> hasz = (1LL * (cur -> left -> hasz) * pot[cur -> right -> pod] + cur -> right -> hasz)%MX; cur -> hasz2 = (1LL * (cur -> left -> hasz2) * pot2[cur -> right -> pod] + cur -> right -> hasz2)%MX2; } tree* build(int h, int from, int to){ tree *res = &poczatek[cnt++]; if(from == to){ res -> pod = 1; res -> hasz = tab[from]; res -> hasz2 = tab[from]; return res; } res -> left = build(h - 1, from, (from + to) / 2); res -> right = build(h - 1, (from + to + 1) / 2, to); update(res); return res; } tree *add(tree *cur, int pl, int val, int p = 1, int k = treeSize){ tree *ret = &poczatek[cnt++]; if(p == k){ ret -> pod = 1; ret -> hasz = val; ret -> hasz2 = val; return ret; } ret -> left = cur -> left; ret -> right = cur -> right; int m = (p + k) / 2; if(pl <= m) ret -> left = add(cur -> left, pl, val, p, m); else ret -> right = add(cur -> right, pl, val, m + 1, k); update(ret); return ret; } bool check(tree *a, tree *b){ int cur = treeSize; while(cur > 1){ cur >>= 1; if(a -> left -> hasz == b -> left -> hasz && a -> left -> hasz2 == b -> left -> hasz2){ a = a -> right; b = b -> right; } else{ a = a -> left; b = b -> left; } } return a -> hasz < b -> hasz; } bool cmp(int a, int b){ if(root[a] -> hasz == root[b] -> hasz && root[a] -> hasz2 == root[b] -> hasz2) return a < b; return check(root[a], root[b]); } int main(){ pot[0] = 1; pot2[0] = 1; for(int i = 1; i < N; ++i){ pot[i] = (1LL * pot[i - 1] * Q)%MX; pot2[i] = (1LL * pot2[i - 1] * Q2)%MX; } scanf("%d %d", &n, &m); for(int i = 1; i <= n; ++i){ scanf("%d", &tab[i]); ++tab[i]; } root[1] = build(19, 1, treeSize); for(int i = 2; i <= m; ++i){ int x, y; scanf("%d %d", &x, &y); root[i] = add(root[i - 1], x, y + 1); } for(int i = 1; i <= m; ++i) res[i] = i; sort(res + 1, res + m + 1, cmp); for(int i = 1; i <= m; ++i) printf("%d ", res[i]); 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; const int N = 6e5 + 7; const int treeSize = 1 << 19; const int Q = 1e6 + 9; const int Q2 = 1e6 + 23; const int MX = INT_MAX; const int MX2 = 1234567891; struct tree{ int hasz = 0, hasz2 = 0, pod = 0; tree *left = nullptr, *right = nullptr; tree(int _hasz = 0, int _hasz2 = 0, int _pod = 0, tree *_left = nullptr, tree *_right = nullptr){ hasz = _hasz; hasz2 = _hasz2; pod = _pod; left = _left; right = _right; } }; int n, m; tree *root[N]; int cnt = 0; tree poczatek[12 * Q]; int res[N]; int tab[N]; int pot[N]; int pot2[N]; inline void update(tree *cur){ cur -> pod = cur -> left -> pod + cur -> right -> pod; cur -> hasz = (1LL * (cur -> left -> hasz) * pot[cur -> right -> pod] + cur -> right -> hasz)%MX; cur -> hasz2 = (1LL * (cur -> left -> hasz2) * pot2[cur -> right -> pod] + cur -> right -> hasz2)%MX2; } tree* build(int h, int from, int to){ tree *res = &poczatek[cnt++]; if(from == to){ res -> pod = 1; res -> hasz = tab[from]; res -> hasz2 = tab[from]; return res; } res -> left = build(h - 1, from, (from + to) / 2); res -> right = build(h - 1, (from + to + 1) / 2, to); update(res); return res; } tree *add(tree *cur, int pl, int val, int p = 1, int k = treeSize){ tree *ret = &poczatek[cnt++]; if(p == k){ ret -> pod = 1; ret -> hasz = val; ret -> hasz2 = val; return ret; } ret -> left = cur -> left; ret -> right = cur -> right; int m = (p + k) / 2; if(pl <= m) ret -> left = add(cur -> left, pl, val, p, m); else ret -> right = add(cur -> right, pl, val, m + 1, k); update(ret); return ret; } bool check(tree *a, tree *b){ int cur = treeSize; while(cur > 1){ cur >>= 1; if(a -> left -> hasz == b -> left -> hasz && a -> left -> hasz2 == b -> left -> hasz2){ a = a -> right; b = b -> right; } else{ a = a -> left; b = b -> left; } } return a -> hasz < b -> hasz; } bool cmp(int a, int b){ if(root[a] -> hasz == root[b] -> hasz && root[a] -> hasz2 == root[b] -> hasz2) return a < b; return check(root[a], root[b]); } int main(){ pot[0] = 1; pot2[0] = 1; for(int i = 1; i < N; ++i){ pot[i] = (1LL * pot[i - 1] * Q)%MX; pot2[i] = (1LL * pot2[i - 1] * Q2)%MX; } scanf("%d %d", &n, &m); for(int i = 1; i <= n; ++i){ scanf("%d", &tab[i]); ++tab[i]; } root[1] = build(19, 1, treeSize); for(int i = 2; i <= m; ++i){ int x, y; scanf("%d %d", &x, &y); root[i] = add(root[i - 1], x, y + 1); } for(int i = 1; i <= m; ++i) res[i] = i; sort(res + 1, res + m + 1, cmp); for(int i = 1; i <= m; ++i) printf("%d ", res[i]); return 0; } |