#include <stdio.h> #include <iostream> #include <algorithm> using namespace std; static const int MAX_SIZE = 500002; struct Change { int m_iPos; int m_iNewValue; }; struct Segment { int m_iStart; int m_iEnd; }; struct SegmentDesc { Segment m_Segm; int m_iValue; bool operator<(const SegmentDesc & rhs) const { return m_iValue < rhs.m_iValue; } }; struct UsefulNumber { int m_iSize; int m_iCurrSize; SegmentDesc * m_aSegm; SegmentDesc * m_aSortedSegms; }; struct StackElem { int m_iLevel; // 0 ... iUsefulNumbers - 1 Segment m_segm; // for narrowing the search }; static int iNumbers, iChildren, iUsefulNumbers, iStackElemCnt, iResults; static int aNumbers[MAX_SIZE]; // 1 ... iNumbers static int aChangeCnt[MAX_SIZE]; // 1 ... iNumbers static int aPos2Useful[MAX_SIZE]; // 1 ... iNumbers static int aResults[MAX_SIZE]; // 1 ... iNumbers static Change aChanges[MAX_SIZE]; // 2 ... iChildren static UsefulNumber aUsefulNumbers[MAX_SIZE]; // 0 ... iUsefulNumbers - 1 static StackElem aStack[MAX_SIZE * 2]; static inline void Push(StackElem se) { aStack[iStackElemCnt++] = se; } static inline bool IsStackEmpty() { return iStackElemCnt == 0; } static inline StackElem Pop() { return aStack[--iStackElemCnt]; } static inline bool SegmsIntersect(Segment & seA, Segment & seB) { if (seA.m_iStart < seB.m_iStart) { return seA.m_iEnd >= seB.m_iStart; } else { return seB.m_iEnd >= seA.m_iStart; } } static inline Segment IntersectSegms(Segment & seA, Segment & seB) { Segment se; se.m_iStart = max(seA.m_iStart, seB.m_iStart); se.m_iEnd = min(seA.m_iEnd, seB.m_iEnd); return se; } static void ReadData() { int x, y; scanf("%d", &iNumbers); scanf("%d", &iChildren); for (int i = 1; i <= iNumbers; ++i) { scanf("%d", &x); aNumbers[i] = x; aChangeCnt[i] = 0; } for (int i = 2; i <= iChildren; ++i) { scanf("%d", &x); scanf("%d", &y); aChanges[i].m_iPos = x; aChanges[i].m_iNewValue = y; aChangeCnt[x]++; } } static void PrepareData() { iUsefulNumbers = 0; for (int i = 1; i <= iNumbers; ++i) { if (aChangeCnt[i] == 0) continue; aPos2Useful[i] = iUsefulNumbers; UsefulNumber & un = aUsefulNumbers[iUsefulNumbers++]; un.m_iSize = aChangeCnt[i] + 1; un.m_iCurrSize = 1; un.m_aSegm = new SegmentDesc[un.m_iSize]; un.m_aSortedSegms = new SegmentDesc[un.m_iSize]; un.m_aSegm[0].m_iValue = aNumbers[i]; un.m_aSegm[0].m_Segm.m_iStart = 1; un.m_aSegm[0].m_Segm.m_iEnd = iChildren; } for (int i = 2; i <= iChildren; ++i) { int iPos = aChanges[i].m_iPos; int iNewValue = aChanges[i].m_iNewValue; int iUseful = aPos2Useful[iPos]; UsefulNumber & un = aUsefulNumbers[iUseful]; un.m_aSegm[un.m_iCurrSize - 1].m_Segm.m_iEnd = i - 1; un.m_aSegm[un.m_iCurrSize].m_iValue = iNewValue; un.m_aSegm[un.m_iCurrSize].m_Segm.m_iStart = i; un.m_aSegm[un.m_iCurrSize].m_Segm.m_iEnd = iChildren; un.m_iCurrSize++; } } static bool LeftPred(const SegmentDesc & sd, int x) { return sd.m_Segm.m_iEnd < x; } static inline int FindFirstSegment(StackElem & se) { UsefulNumber & un = aUsefulNumbers[se.m_iLevel]; auto it = lower_bound(un.m_aSegm, un.m_aSegm + un.m_iSize, se.m_segm.m_iStart, LeftPred); if (it == un.m_aSegm + un.m_iSize) return -1; return it - un.m_aSegm; } static void ProcessLastLevel(UsefulNumber & un) { for (int i = 0; i < un.m_iCurrSize; ++i) { Segment & se = un.m_aSortedSegms[i].m_Segm; for (int j = se.m_iStart; j <= se.m_iEnd; ++j) { aResults[iResults++] = j; } } } static void ProcessStackElem(StackElem & se) { int iLeft = FindFirstSegment(se); if (iLeft == -1) return; UsefulNumber & un = aUsefulNumbers[se.m_iLevel]; un.m_iCurrSize = 0; for (int i = iLeft; i < un.m_iSize; ++i) { if (!SegmsIntersect(un.m_aSegm[i].m_Segm, se.m_segm)) break; un.m_aSortedSegms[un.m_iCurrSize].m_iValue = un.m_aSegm[i].m_iValue; un.m_aSortedSegms[un.m_iCurrSize++].m_Segm = IntersectSegms(un.m_aSegm[i].m_Segm, se.m_segm); } if (un.m_iCurrSize == 0) return; sort(un.m_aSortedSegms, un.m_aSortedSegms + un.m_iCurrSize); if (se.m_iLevel == iUsefulNumbers - 1) { ProcessLastLevel(un); return; } for (int i = un.m_iCurrSize - 1; i >= 0; --i) { StackElem seNew; seNew.m_iLevel = se.m_iLevel + 1; seNew.m_segm = un.m_aSortedSegms[i].m_Segm; Push(seNew); } } static void Solve() { iResults = 0; StackElem se; iStackElemCnt = 0; se.m_iLevel = 0; se.m_segm.m_iStart = 1; se.m_segm.m_iEnd = iChildren; Push(se); while (!IsStackEmpty()) { se = Pop(); ProcessStackElem(se); } } static void WriteResults() { for (int i = 0; i < iResults; ++i) { printf("%d", aResults[i]); if (i != iResults - 1) { printf(" "); } } } int main(int argc, char * argv[]) { ReadData(); PrepareData(); Solve(); WriteResults(); 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | #include <stdio.h> #include <iostream> #include <algorithm> using namespace std; static const int MAX_SIZE = 500002; struct Change { int m_iPos; int m_iNewValue; }; struct Segment { int m_iStart; int m_iEnd; }; struct SegmentDesc { Segment m_Segm; int m_iValue; bool operator<(const SegmentDesc & rhs) const { return m_iValue < rhs.m_iValue; } }; struct UsefulNumber { int m_iSize; int m_iCurrSize; SegmentDesc * m_aSegm; SegmentDesc * m_aSortedSegms; }; struct StackElem { int m_iLevel; // 0 ... iUsefulNumbers - 1 Segment m_segm; // for narrowing the search }; static int iNumbers, iChildren, iUsefulNumbers, iStackElemCnt, iResults; static int aNumbers[MAX_SIZE]; // 1 ... iNumbers static int aChangeCnt[MAX_SIZE]; // 1 ... iNumbers static int aPos2Useful[MAX_SIZE]; // 1 ... iNumbers static int aResults[MAX_SIZE]; // 1 ... iNumbers static Change aChanges[MAX_SIZE]; // 2 ... iChildren static UsefulNumber aUsefulNumbers[MAX_SIZE]; // 0 ... iUsefulNumbers - 1 static StackElem aStack[MAX_SIZE * 2]; static inline void Push(StackElem se) { aStack[iStackElemCnt++] = se; } static inline bool IsStackEmpty() { return iStackElemCnt == 0; } static inline StackElem Pop() { return aStack[--iStackElemCnt]; } static inline bool SegmsIntersect(Segment & seA, Segment & seB) { if (seA.m_iStart < seB.m_iStart) { return seA.m_iEnd >= seB.m_iStart; } else { return seB.m_iEnd >= seA.m_iStart; } } static inline Segment IntersectSegms(Segment & seA, Segment & seB) { Segment se; se.m_iStart = max(seA.m_iStart, seB.m_iStart); se.m_iEnd = min(seA.m_iEnd, seB.m_iEnd); return se; } static void ReadData() { int x, y; scanf("%d", &iNumbers); scanf("%d", &iChildren); for (int i = 1; i <= iNumbers; ++i) { scanf("%d", &x); aNumbers[i] = x; aChangeCnt[i] = 0; } for (int i = 2; i <= iChildren; ++i) { scanf("%d", &x); scanf("%d", &y); aChanges[i].m_iPos = x; aChanges[i].m_iNewValue = y; aChangeCnt[x]++; } } static void PrepareData() { iUsefulNumbers = 0; for (int i = 1; i <= iNumbers; ++i) { if (aChangeCnt[i] == 0) continue; aPos2Useful[i] = iUsefulNumbers; UsefulNumber & un = aUsefulNumbers[iUsefulNumbers++]; un.m_iSize = aChangeCnt[i] + 1; un.m_iCurrSize = 1; un.m_aSegm = new SegmentDesc[un.m_iSize]; un.m_aSortedSegms = new SegmentDesc[un.m_iSize]; un.m_aSegm[0].m_iValue = aNumbers[i]; un.m_aSegm[0].m_Segm.m_iStart = 1; un.m_aSegm[0].m_Segm.m_iEnd = iChildren; } for (int i = 2; i <= iChildren; ++i) { int iPos = aChanges[i].m_iPos; int iNewValue = aChanges[i].m_iNewValue; int iUseful = aPos2Useful[iPos]; UsefulNumber & un = aUsefulNumbers[iUseful]; un.m_aSegm[un.m_iCurrSize - 1].m_Segm.m_iEnd = i - 1; un.m_aSegm[un.m_iCurrSize].m_iValue = iNewValue; un.m_aSegm[un.m_iCurrSize].m_Segm.m_iStart = i; un.m_aSegm[un.m_iCurrSize].m_Segm.m_iEnd = iChildren; un.m_iCurrSize++; } } static bool LeftPred(const SegmentDesc & sd, int x) { return sd.m_Segm.m_iEnd < x; } static inline int FindFirstSegment(StackElem & se) { UsefulNumber & un = aUsefulNumbers[se.m_iLevel]; auto it = lower_bound(un.m_aSegm, un.m_aSegm + un.m_iSize, se.m_segm.m_iStart, LeftPred); if (it == un.m_aSegm + un.m_iSize) return -1; return it - un.m_aSegm; } static void ProcessLastLevel(UsefulNumber & un) { for (int i = 0; i < un.m_iCurrSize; ++i) { Segment & se = un.m_aSortedSegms[i].m_Segm; for (int j = se.m_iStart; j <= se.m_iEnd; ++j) { aResults[iResults++] = j; } } } static void ProcessStackElem(StackElem & se) { int iLeft = FindFirstSegment(se); if (iLeft == -1) return; UsefulNumber & un = aUsefulNumbers[se.m_iLevel]; un.m_iCurrSize = 0; for (int i = iLeft; i < un.m_iSize; ++i) { if (!SegmsIntersect(un.m_aSegm[i].m_Segm, se.m_segm)) break; un.m_aSortedSegms[un.m_iCurrSize].m_iValue = un.m_aSegm[i].m_iValue; un.m_aSortedSegms[un.m_iCurrSize++].m_Segm = IntersectSegms(un.m_aSegm[i].m_Segm, se.m_segm); } if (un.m_iCurrSize == 0) return; sort(un.m_aSortedSegms, un.m_aSortedSegms + un.m_iCurrSize); if (se.m_iLevel == iUsefulNumbers - 1) { ProcessLastLevel(un); return; } for (int i = un.m_iCurrSize - 1; i >= 0; --i) { StackElem seNew; seNew.m_iLevel = se.m_iLevel + 1; seNew.m_segm = un.m_aSortedSegms[i].m_Segm; Push(seNew); } } static void Solve() { iResults = 0; StackElem se; iStackElemCnt = 0; se.m_iLevel = 0; se.m_segm.m_iStart = 1; se.m_segm.m_iEnd = iChildren; Push(se); while (!IsStackEmpty()) { se = Pop(); ProcessStackElem(se); } } static void WriteResults() { for (int i = 0; i < iResults; ++i) { printf("%d", aResults[i]); if (i != iResults - 1) { printf(" "); } } } int main(int argc, char * argv[]) { ReadData(); PrepareData(); Solve(); WriteResults(); return 0; } |