#include <cstdio> #include <cassert> #include <algorithm> #include <queue> #include <map> struct Node { int b, e; int minValue; int s1, s2; }; struct Key { int b, e; int resIdx; int pIdx; int x; }; const int MAX = 500500; int n, m; int t[MAX], p[MAX], x[MAX], res[MAX]; Node tree[2 * MAX]; int tIdx; int pIdx2cIdxSize[MAX]; int pIdx2cIdxCnt[MAX]; int pIdx2cIdxTotal[MAX]; int *pIdx2cIdxV[MAX]; int cIdxsF[MAX]; int tCreate(int b, int e) { assert(b <= e); int i = tIdx++; tree[i].b = b; tree[i].e = e; tree[i].minValue = MAX; if (tree[i].b < tree[i].e) { int split = (tree[i].b + tree[i].e) / 2; tree[i].s1 = tCreate(b, split); tree[i].s2 = tCreate(split + 1, e); } return i; } void tSet(int cIdx, int value, int i = 0) { assert(tree[i].b <= cIdx); assert(cIdx <= tree[i].e); if (tree[i].b < tree[i].e) { int split = (tree[i].b + tree[i].e) / 2; if (cIdx <= split) { tSet(cIdx, value, tree[i].s1); } else { tSet(cIdx, value, tree[i].s2); } tree[i].minValue = std::min(tree[tree[i].s1].minValue, tree[tree[i].s2].minValue); } else { tree[i].minValue = value; } } int tGetMin(int b, int e, int i = 0) { assert(b <= e); assert(tree[i].b <= b); assert(e <= tree[i].e); if (tree[i].b == b && tree[i].e == e) { return tree[i].minValue; } assert(tree[i].b < tree[i].e); int split = (tree[i].b + tree[i].e) / 2; if (e <= split) { return tGetMin(b, e, tree[i].s1); } if (b > split) { return tGetMin(b, e, tree[i].s2); } return std::min( tGetMin(b, split, tree[i].s1), tGetMin(split + 1, e, tree[i].s2) ); } int getIdxs(int b, int e, int value, int *cIdxs) { assert(b <= e); assert(pIdx2cIdxV[value] != nullptr); auto bIter = std::lower_bound(pIdx2cIdxV[value], pIdx2cIdxV[value] + pIdx2cIdxSize[value], b); assert(*bIter >= b); int size = 0; while (bIter != pIdx2cIdxV[value] + pIdx2cIdxSize[value] && *bIter <= e) { cIdxs[size++] = *bIter; bIter++; } return size; } void calc(int b, int e, int resIdx, int pIdx, int tValue) { Key key; key.b = b; key.e = e; key.resIdx = resIdx; key.pIdx = pIdx; key.x = tValue; std::vector<Key> Q; Q.push_back(key); std::vector<std::pair<int, int> > xIdx; std::map<int, int> cSize; while (!Q.empty()) { key = Q.back(); Q.pop_back(); b = key.b; e = key.e; resIdx = key.resIdx; t[key.pIdx] = key.x; pIdx = (b == e ? MAX : tGetMin(b, e)); if (pIdx == MAX) { for (int c = b; c <= e; c++) { res[resIdx++] = c; } continue; } assert(pIdx < n); int *cIdxs = cIdxsF + 1; int cIdxsSize = getIdxs(b, e, pIdx, cIdxs); assert(cIdxsSize > 0); xIdx.resize(0); for (int c = 0; c < cIdxsSize; c++) { int cIdx = cIdxs[c]; tSet(cIdx, MAX); xIdx.push_back(std::pair<int, int>(x[cIdx], cIdx)); } if (b < cIdxs[0]) { --cIdxs; cIdxs[0] = b; ++cIdxsSize; xIdx.push_back(std::pair<int, int>(t[pIdx], b)); } cIdxs[cIdxsSize++] = e + 1; std::sort(xIdx.begin(), xIdx.end()); cSize.clear(); for (int c = cIdxsSize - 1; c > 0; c--) { cSize[cIdxs[c - 1]] = cIdxs[c] - cIdxs[c - 1]; } int sum = 0; for (int c = 0; c < xIdx.size(); c++) { int tmp = cSize[xIdx[c].second]; cSize[xIdx[c].second] = sum; sum += tmp; } assert(sum == e - b + 1); for (int c = cIdxsSize - 1; c > 0; c--) { key.b = cIdxs[c - 1]; key.e = cIdxs[c] - 1; key.resIdx = resIdx + cSize[key.b]; key.pIdx = pIdx; key.x = x[key.b]; Q.push_back(key); } } } int main(int argc, char *argv[]) { scanf("%i%i", &n, &m); for (int c = 0; c < n; c++) { scanf("%i", t + c); } tCreate(1, m); for (int c = 2; c <= m; c++) { scanf("%i%i", p + c, x + c); tSet(c, p[c] - 1); pIdx2cIdxSize[p[c] - 1]++; } int offset = 0; for (int c = 2; c <= m; c++) { int idx = p[c] - 1; if (pIdx2cIdxSize[idx] > 0 && pIdx2cIdxV[idx] == nullptr) { pIdx2cIdxV[idx] = pIdx2cIdxTotal + offset; offset += pIdx2cIdxSize[idx]; } } assert(offset == m - 1); for (int c = 2; c <= m; c++) { int idx = p[c] - 1; pIdx2cIdxV[idx][pIdx2cIdxCnt[idx]++] = c; } calc(1, m, 0, 0, t[0]); for (int c = 0; c < m; c++) { printf("%i ", res[c]); } printf("\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 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #include <cstdio> #include <cassert> #include <algorithm> #include <queue> #include <map> struct Node { int b, e; int minValue; int s1, s2; }; struct Key { int b, e; int resIdx; int pIdx; int x; }; const int MAX = 500500; int n, m; int t[MAX], p[MAX], x[MAX], res[MAX]; Node tree[2 * MAX]; int tIdx; int pIdx2cIdxSize[MAX]; int pIdx2cIdxCnt[MAX]; int pIdx2cIdxTotal[MAX]; int *pIdx2cIdxV[MAX]; int cIdxsF[MAX]; int tCreate(int b, int e) { assert(b <= e); int i = tIdx++; tree[i].b = b; tree[i].e = e; tree[i].minValue = MAX; if (tree[i].b < tree[i].e) { int split = (tree[i].b + tree[i].e) / 2; tree[i].s1 = tCreate(b, split); tree[i].s2 = tCreate(split + 1, e); } return i; } void tSet(int cIdx, int value, int i = 0) { assert(tree[i].b <= cIdx); assert(cIdx <= tree[i].e); if (tree[i].b < tree[i].e) { int split = (tree[i].b + tree[i].e) / 2; if (cIdx <= split) { tSet(cIdx, value, tree[i].s1); } else { tSet(cIdx, value, tree[i].s2); } tree[i].minValue = std::min(tree[tree[i].s1].minValue, tree[tree[i].s2].minValue); } else { tree[i].minValue = value; } } int tGetMin(int b, int e, int i = 0) { assert(b <= e); assert(tree[i].b <= b); assert(e <= tree[i].e); if (tree[i].b == b && tree[i].e == e) { return tree[i].minValue; } assert(tree[i].b < tree[i].e); int split = (tree[i].b + tree[i].e) / 2; if (e <= split) { return tGetMin(b, e, tree[i].s1); } if (b > split) { return tGetMin(b, e, tree[i].s2); } return std::min( tGetMin(b, split, tree[i].s1), tGetMin(split + 1, e, tree[i].s2) ); } int getIdxs(int b, int e, int value, int *cIdxs) { assert(b <= e); assert(pIdx2cIdxV[value] != nullptr); auto bIter = std::lower_bound(pIdx2cIdxV[value], pIdx2cIdxV[value] + pIdx2cIdxSize[value], b); assert(*bIter >= b); int size = 0; while (bIter != pIdx2cIdxV[value] + pIdx2cIdxSize[value] && *bIter <= e) { cIdxs[size++] = *bIter; bIter++; } return size; } void calc(int b, int e, int resIdx, int pIdx, int tValue) { Key key; key.b = b; key.e = e; key.resIdx = resIdx; key.pIdx = pIdx; key.x = tValue; std::vector<Key> Q; Q.push_back(key); std::vector<std::pair<int, int> > xIdx; std::map<int, int> cSize; while (!Q.empty()) { key = Q.back(); Q.pop_back(); b = key.b; e = key.e; resIdx = key.resIdx; t[key.pIdx] = key.x; pIdx = (b == e ? MAX : tGetMin(b, e)); if (pIdx == MAX) { for (int c = b; c <= e; c++) { res[resIdx++] = c; } continue; } assert(pIdx < n); int *cIdxs = cIdxsF + 1; int cIdxsSize = getIdxs(b, e, pIdx, cIdxs); assert(cIdxsSize > 0); xIdx.resize(0); for (int c = 0; c < cIdxsSize; c++) { int cIdx = cIdxs[c]; tSet(cIdx, MAX); xIdx.push_back(std::pair<int, int>(x[cIdx], cIdx)); } if (b < cIdxs[0]) { --cIdxs; cIdxs[0] = b; ++cIdxsSize; xIdx.push_back(std::pair<int, int>(t[pIdx], b)); } cIdxs[cIdxsSize++] = e + 1; std::sort(xIdx.begin(), xIdx.end()); cSize.clear(); for (int c = cIdxsSize - 1; c > 0; c--) { cSize[cIdxs[c - 1]] = cIdxs[c] - cIdxs[c - 1]; } int sum = 0; for (int c = 0; c < xIdx.size(); c++) { int tmp = cSize[xIdx[c].second]; cSize[xIdx[c].second] = sum; sum += tmp; } assert(sum == e - b + 1); for (int c = cIdxsSize - 1; c > 0; c--) { key.b = cIdxs[c - 1]; key.e = cIdxs[c] - 1; key.resIdx = resIdx + cSize[key.b]; key.pIdx = pIdx; key.x = x[key.b]; Q.push_back(key); } } } int main(int argc, char *argv[]) { scanf("%i%i", &n, &m); for (int c = 0; c < n; c++) { scanf("%i", t + c); } tCreate(1, m); for (int c = 2; c <= m; c++) { scanf("%i%i", p + c, x + c); tSet(c, p[c] - 1); pIdx2cIdxSize[p[c] - 1]++; } int offset = 0; for (int c = 2; c <= m; c++) { int idx = p[c] - 1; if (pIdx2cIdxSize[idx] > 0 && pIdx2cIdxV[idx] == nullptr) { pIdx2cIdxV[idx] = pIdx2cIdxTotal + offset; offset += pIdx2cIdxSize[idx]; } } assert(offset == m - 1); for (int c = 2; c <= m; c++) { int idx = p[c] - 1; pIdx2cIdxV[idx][pIdx2cIdxCnt[idx]++] = c; } calc(1, m, 0, 0, t[0]); for (int c = 0; c < m; c++) { printf("%i ", res[c]); } printf("\n"); return 0; } |