ç®æ¬¡ïŒçŸåšïŒ
ããŒã1.説æãæäœãã¢ããªã±ãŒã·ã§ã³ã
ããŒã2ãããªãŒå
ã®è²Žéãªæ
å ±ãšããã«ããè€æ°ã®æäœã
ããŒã3.æé»çãªããŒã«ãããã«ã«ãããªãŒã
ç¶è¡ããã«ã¯...
éåžžã«åŒ·ãéè¡
ãã«ã«ãã®æšãåã®2ã€ã®éšåã§ç§ãã¡ã«äžãããã¹ãŠã®æ©äŒã®åŸãä»æ¥ç§ã¯åœŒãšå¥åŠã§ç¥èãªäœããããŸãã ããã«ããããããããã®ã¢ã¯ã·ã§ã³ã«ãããå®å
šã«æ°ããåæ»ç¶æ
ã«ããããªãŒããè¿œå æ©èœãåããé«åºŠã§åŒ·åãªé
åã®äžçš®ãšããŠèããããšãã§ããŸãã ãã®æäœæ¹æ³ã瀺ãã2çªç®ã®éšåã®ããŒã¿ã䜿çšãããã¹ãŠã®æäœãå€æŽãããããªãŒçšã«ä¿åãããããšã瀺ãããã®åŸãããã€ãã®æ°ããæçšãªãã®ãæäŸããŸãã
ãã©ããã®æ§é ãããäžåºŠæãåºããŠãã ããã ããã«ã¯ããã©ãããæ€çŽ¢ããªãŒã§ããããŒ
x ããã©ãããæã§ããã©ã³ãã ããŒ
y ãããã³å Žåã«ãã£ãŠã¯ïŒã³ã¹ãïŒã
æã€ãŠãŒã¶ãŒæ
å ±ãå«ãŸããŸãã ããŒxãªãã§äžå¯èœãªããšãè¡ãããã©ãããèããŠã¿ãŸãããã ã€ãŸããããŒxããŸã£ãããªããããŒyãã©ã³ãã ãªããªãŒãã§ããŸãã ãããã£ãŠããªããããå¿
èŠãªã®ãã¯äžè¬çã«ç解ã§ããŸãã:)
å®éããã®ãããªæ§é ãèæ
®ããããšã¯ãããŒxããŸã ã©ããã«ååšãããã«ã«ãããªãŒã®ãããªãã®ã§ããããããã¯ç§ãã¡ã«éç¥ãããŸããã§ããã ãããã圌ãã¯åœŒãã®ããã«ãäºæ³éããäºåæ¢çŽ¢æšã®æ¡ä»¶ãæºããããŠãããšèªããŸãã 次ã«ããããã®æªç¥ã®Xã¯0ãã
N-1ãŸã§ã®æ°åã§ãããããªãŒã®æ§é ã«åŸã£ãŠ
æé»çã«é
眮ããããšæ³åã§ããŸãã
ããªãŒã§ã¯ãé ç¹ã®ããŒãä»å ãããŠããªãããã«èŠããŸãããé ç¹èªäœã«ã¯çªå·ãä»ããããŠããŸãã ããã«ããããã¯ãæåŸã®éšåãããã§ã«ããªãã¿ã®é åºéãã®ãã€ãã¹é åºã§çªå·ãä»ããããŠããŸãã æ確ã«çªå·ä»ããããé ç¹ãæã€ããªãŒã¯ãã€ã³ããã¯ã¹ãåãæé»çãªããŒã§ãããã³ã³ãã³ãããŠãŒã¶ãŒæ
å ±
c
ã§ããé
åãšèŠãªãããšãã§ããŸãã ãã¬ãŒã€ãŒã¯ããã©ã³ã¹ãåãããã«ã®ã¿å¿
èŠã§ã;ãããã¯ããŠãŒã¶ãŒã«ãšã£ãŠäžèŠãªããŒã¿æ§é ã®å
éšçãªè©³çŽ°ã§ãã X
ã¯å®éã«ã¯ååçã«ã¯æ ŒçŽãããŠããŸããã
åã®éšåãšã¯ç°ãªãããã®é
åã¯ãœãŒããªã©ã®ããããã£ãèªåçã«ååŸããŸããã çµå±ã®ãšãããæ
å ±ã«é¢ããæ§é äžã®å¶éã¯ãªãããšã«ããæäžéšã«ä¿åã§ããŸãã
äž»ãªçšé
ããŠããªããã®ãããªè§£éãå¿
èŠãªã®ãã«ã€ããŠè©±ã䟡å€ã¯ãããŸãã
ããšãã°ã2ã€ã®é
åãããŒãžããããšæã£ãããšã¯ãããŸããïŒ ã€ãŸããã«ãŒãå
ã®OïŒNïŒã«ãã2çªç®ã®èŠçŽ ã®ãã¹ãŠã®èŠçŽ ãã³ããŒããããšãªããåçŽã«1ã€ã®èŠçŽ ãä»ã®èŠçŽ ã®æåŸã«å²ãåœãŠãã ãã§ãã æé»çãªããŒã«ãããã«ã«ãããªãŒã䜿çšãããšããã®ãããªæ©äŒãåŸãããŸããçµå±ã®ãšãããMergeæäœãç§ãã¡ãã奪ã£ã人ã¯ããŸããã
Stop-stop-stopã§ãããMergeã¯æ瀺çãªãã«ã«ãããªãŒçšã«äœæãããŸããã 圌女ã®ã¢ã«ãŽãªãºã ã¯ããã§äœãçŽããªããã°ãªããªãã§ããããïŒ ããã§ããªãã 圌女ã®ã³ãŒããããäžåºŠèŠãŠãã ããã
public static Treap Merge(Treap L, Treap R) { if (L == null) return R; if (R == null) return L; if (Ly > Ry) { var newR = Merge(L.Right, R); return new Treap(Lx, Ly, L.Left, newR); } else { var newL = Merge(L, R.Left); return new Treap(Rx, Ry, newL, R.Right); } }
æãåºãããã«ãããŒãžæäœã¯ãå·Šå
¥åããªãŒ
Lã®ãã¹ãŠã®ããŒãå³å
¥åããªãŒ
Rã®ããŒãè¶
ããªããšããäºå®ã«äŸåããŠããŸã
ã ãã®æ¡ä»¶ãæºãããããšããä»®å®ã®äžã§ãååãšããŠããŒã«æ³šæãæããã«ããŒãžããŸããã¢ã«ãŽãªãºã ãå®è¡ããããã»ã¹ã§ã¯ãåªå
é äœã®ã¿ãæ¯èŒãããŸãã
ããªããšç§ã¯ããã§æãoperationæ
¢ãªæ¹æ³ã§ããŒãžæäœã欺ããŠããããšãããããŸãïŒåœŒå¥³ã¯é åºä»ããããããŒãæã€ããªãŒãäžããããããšãæåŸ
ããŠããŸãããããŠç§ãã¡ã¯ããŒãªãã§ããªãŒããã€ã·ãåãé€ããŸã:)ããããããªãŒã«æ瀺çãªããŒããããé åºä»ããããŠãããšããä»®å®ã¯åœŒå¥³ã匷å¶ããŸãæ€çŽ¢ããªãŒã®æ¡ä»¶ãæºãããªããã°ãªããªããããããŒLãããŒRãããäœããã®æå³ã§ããªãŒæ§é å
ã«ããããã«ããªãŒãããŒãžããŸãã ã€ãŸããããªãŒLã«Nåã®èŠçŽ ããããããªãŒRã«ããããMåã®èŠçŽ ãããå ŽåãããªãŒRã®èŠçŽ ãããŒãžãããšãNãã
N + M-1ãŸã§ã®æé»ã®æ°ãèªåçã«ååŸãããŸãã ããªãŒæ§é ã«ããã°ãããŒãžæäœã¯èªåçã«ããããé©åã«é
åžããèæ
®ãããåªå
é äœã¯ãæºãã©ã³ã¹ããå®è¡ããŸãã ãããã£ãŠããé
åãRããé
åãLã®å³åŽã«ãåž°å±ããããŸããã
ãœãŒã¹ã«é¢ããŠã¯ã
x
ããŒãªãã§æ°ãã
ImplicitTreap
ããŒã¿åãååŸããã ãã§ãããããã«å¯Ÿå¿ãããã©ã€ããŒãã³ã³ã¹ãã©ã¯ã¿ãŒãå¿
èŠã§ãã ãã¹ãŠã®ããŒãžã³ãŒãã¯åããŸãŸã§ãã ãã¡ãããããã¯ããã«è€æ°ã®ã¯ãšãªãèšç®ããªãããŒãžã§ã³ã§ãããšèããå Žåã§ãã2çªç®ã®éšåã§å®è£
ããããæ£çŸ©ã®å埩ãããã³ãããã·ã¥çŽæãã®æ©èœãå€ãå Žæã®Mergeã«æ®ããŸãã
æ確ã«ããããã«ã2ã€ã®ã©ã³ãã ãªæé»ã®ãã«ã«ãããªãŒãååŸããããŒãžçµæãšãšãã«å³ã«ç€ºããŸãã åªå
é äœã¯ã©ã³ãã ã«éžæããããããäž¡æ¹ã®ããªãŒã®å®éã®æ§é ãšçµæã¯éåžžã«ç°ãªãå ŽåããããŸãã ããããããã¯éèŠã§ã¯ãããŸãã-é
åã®æ§é ãã€ãŸã èŠçŽ
c
ã®ã·ãŒã±ã³ã¹ã¯åžžã«ä¿æãããŸãã
ãªã¬ã³ãžè²ã®ç¢å°ã¯ãMergeã®ååž°ãã¹ã§ãã
ä»ãåå²ããæéã§ãã 圌女ã欺ãã®ã¯ããã»ã©ç°¡åã§ã¯ãããŸãããéã«ãã¹ããªããæäœã¯åªå
é äœã«é¢å¿ããªããããŒãæ¯èŒããã ãã§ãã æé»ã®ããªãŒã®ããŒã¯ã圌女ãã©ã®ããã«æ¯èŒãããã«ã€ããŠèããå¿
èŠããããŸãã åé¡ã¯å®éã«ã¯ããé«ããã®ã§ãããæ°ããããŒã¿æ§é ã§åå²æäœã¯äœãããŸããïŒ åœŒå¥³ã¯ãã€ãŠããŒã§æšãåããŸããããããã«ã¯åãåãå¿
èŠã®ããããŒããããŸããã
ããŒã¯ãããŸãããããããã®æé»çãªè¡šçŸ-é
åã€ã³ããã¯ã¹ããããŸãã ãããã£ãŠãåãåãã®æ¬è³ªã¯ããããå€æŽãããŸãããæ£ç¢ºã«
x 0ã®èŠçŽ ãå·ŠåŽã«ãä»ã®ãã¹ãŠãå³åŽã«è¡šç€ºãããããã«ãããªãŒã2ã€ã«åå²ããŸãã ã倧èŠæš¡ãªã解éã§ã¯ãããã¯é
åã®å
é ããx
0åã®èŠçŽ ãæ°ããé
åã«åé¢ããããšãæå³ããŸãã
æ°ããåå²æäœãå®è¡ããæ¹æ³ã¯ïŒ åãšåãããã«ã圌女ã¯2ã€ã®ã±ãŒã¹ãæ€èšããŸããã«ãŒã
Tã¯å·Šã®çµæ
LãŸãã¯å³ã®
Rã«è¡šç€ºãããŸã
ã é
åå
ã®ã€ã³ããã¯ã¹ãx
0ããå°ããå Žåã¯å·Šã«è¡šç€ºãããããã§ãªãå Žåã¯å³ã«è¡šç€ºãããŸãã ãããŠãé
åå
ã®ããªãŒã®æäžéšã®ã€ã³ããã¯ã¹ã¯äœã§ããïŒ ç§ãã¡ã¯ãã§ã«ã2çªç®ã®ããŒããã圌ãšã®äœæ¥æ¹æ³ãç¥ã£ãŠããŸãã
ãµãããªãŒã®
ãµã€ãºãããªãŒã®æäžéšã«æ ŒçŽããã ãã§ååã§ãã ãã®åŸãéžæããã»ã¹ã¯ç°¡åã«åŸ©å
ãããŸãã
S Lãå·ŠãµãããªãŒã®ãµã€ãºïŒ
T.Left.Size
ïŒãšããŸãã
S L +1â€x 0ã®å Žåãã«ãŒãã¯å·Šã®çµæã«ãªããŸãã ãã®ãããæ£ãããµãããªãŒãååž°çã«ã«ããããå¿
èŠããããŸãã ãã ããå¥ã®ããŒã
x 0 -S L -1ã§ã«ããããŸããããã¯ã
S L +1ã®èŠçŽ ããã§ã«ç®çã®å·Šã®çµæã«ãªã£ãŠããããã§ãã
S L +1> x 0ã®å Žåãã«ãŒãã¯æ£ããçµæã«ãªããŸãã 次ã«ãå·ŠåŽã®ãµãããªãŒãååž°çã«ã«ããããå¿
èŠããããŸãã ãã®å Žåã以åã®ããã«å®å
šã«å¯Ÿç§°çã§ã¯ãããŸããããã®ã¹ãããã§ã¯ãååž°ã«ãã£ãŠèŠçŽ ãå·Šã®çµæã§ã¯ãªãå³ã®çµæã«åå²ãããããããµãããªãŒããã¹ãŠåãããŒx
0ã§ã«ããããŸãã
ãã®å³ã¯ããµãããªãŒãæ·»ä»ããã
x 0 = 6ã«åå²ããããã«ã«ãããªãŒã瀺ããŠããŸãã
æ°ããã¹ããªããã®ãœãŒã¹ã³ãŒãã¯ãæ°ããã¯ã©ã¹ã®ã¯ãŒã¯ããŒã¹ãšäžç·ã«ããŒã¬ã¹ã§ãããå¥ã®ãã©ã€ããŒãã³ã³ã¹ãã©ã¯ã¿ãŒãåããŠããŸãã
ãããŸã§ã®ãšãããè€æ°ã®æäœãè¡ããã«Splitãå床æäŸããŠããŸããèªè
ã¯ãããã®è¡ãåå¥ã«åŸ©å
ã§ããŸãããå€ãå Žæããã¯æ¶ããŠããŸããã ãããããµãããªãŒã®ãµã€ãºãåã«ãŠã³ãããããšãå¿ããŠã¯ãªããŸããã
private int y; public double Cost; public ImplicitTreap Left; public ImplicitTreap Right; public int Size = 1; private ImplicitTreap(int y, double cost, ImplicitTreap left = null, ImplicitTreap right = null) { this.y = y; this.Cost = cost; this.Left = left; this.Right = right; } public static int SizeOf(ImplicitTreap treap) { return treap == null ? 0 : treap.Size; } public void Recalc() { Size = SizeOf(Left) + SizeOf(Right) + 1; } // - Split public void Split(int x, out ImplicitTreap L, out ImplicitTreap R) { ImplicitTreap newTree = null; int curIndex = SizeOf(Left) + 1; if (curIndex <= x) { if (Right == null) R = null; else Right.Split(x - curIndex, out newTree, out R); L = new ImplicitTreap(y, Cost, Left, newTree); L.Recalc(); } else { if (Left == null) L = null; else Left.Split(x, out L, out newTree); R = new ImplicitTreap(y, Cost, newTree, Right); R.Recalc(); } }
察æ°æéã§åäœããSplitãšMergeã®é
åãäœæããã®ã§ãããããã©ããã§äœ¿çšããŸãã é
åãããã£ãŠã¿ãŸãããã
é
åã®ããã²ãŒã
æ¿å
¥
ãã©ãŒã«ã¹No. 1-éåžžã©ãããOïŒNïŒã§ã¯ãªãOïŒlog
2 NïŒã®å¿
èŠãªäœçœ®
Posã«èŠçŽ ãé
åå
ã«æ¿å
¥ããŸãã
ååãšããŠãéåžžã®ãã«ã«ãããªãŒã§ãããè¡ãæ¹æ³ã¯æ¢ã«ããã£ãŠããŸãããä»ã§ã¯ããŒãã€ã³ããã¯ã¹ã«çœ®ãæããããŠããŸãã ãããŠãæ®ãã®æé ã¯å€æŽãããŠããŸããã
â¢é
å
T [0;ãã«ããã
ãŸãã NïŒé
åã®ã€ã³ããã¯ã¹Pos
L [0; PosïŒããã³
R [Pos; NïŒ ã
â¢æ¿å
¥ãããèŠçŽ ã®1ã€ã®é ç¹ããé
åããªãŒãäœæããŸãã
â¢äœæãããé
åãå³ããå·Šã®çµæLã«å²ãåœãŠãäž¡æ¹ã«å³ã®çµæRãå²ãåœãŠãŸãã
â¢
T '[0;ã®é
åãååŸããŸãã
ã N + 1ïŒ ãç®çã®èŠçŽ ã¯äœçœ®Posã«ãããå³åŽã®æ®ãã®éšåãã·ãããããŸãã
æ¿å
¥ã®ãœãŒã¹ã³ãŒãã¯å®å
šã«ã¯å€æŽãããŠããŸããã
public ImplicitTreap Add(int pos, double elemCost) { ImplicitTreap l, r; Split(pos, out l, out r); ImplicitTreap m = new ImplicitTreap(rand.Next(), elemCost); return Merge(Merge(l, m), r); }
åé€ãã
ãã©ãŒã«ã¹No. 2-ãã®äœçœ®
Posã«ããé
åããèŠçŽ ãåãåããŸãã
ç¹°ãè¿ããŸãããæé ã¯éåžžã®ãã«ã«ãæšãšåãã§ãã
â¢é
å
T [0;ãã«ããã
ãŸãã NïŒé
åã®ã€ã³ããã¯ã¹Pos
L [0; PosïŒããã³
R [Pos; NïŒ ã
â¢æ£ããçµæRã¯ãã€ã³ããã¯ã¹1ïŒåäœïŒïŒã§ã«ãããããŸãã é
å
MãååŸã
ãŸã[Pos; Pos + 1ïŒ 1ã€ã®èŠçŽ ïŒä»¥åã«Posäœçœ®ã«ç«ã£ãŠããïŒãããã³é
å
R '[Pos + 1; NïŒ ã
â¢é
åLãšR 'ãããŒãžããŸãã
ãœãŒã¹ã³ãŒãã®ããŠã³ããŒãïŒ
public ImplicitTreap Remove(int pos) { ImplicitTreap l, m, r; Split(pos, out l, out r); r.Split(1, out m, out r); return Merge(l, r); }
ã»ã°ã¡ã³ãããšã«è€æ°ã®ãªã¯ãšã¹ã
ãã©ãŒã«ã¹No.3ïŒOïŒlog
2 NïŒã®å Žåãé
åãµãã»ã°ã¡ã³ãã§ãã¹ãŠåãã¯ãšãªãå®è¡ã§ããŸãïŒåèš/æ倧/æå°/ååšãŸãã¯ã©ãã«æ°ãªã©ïŒã
ããªãŒæ§é ã¯åã®éšåããå€æŽãããŸãããé ç¹ã«ã¯ããµãã»ã°ã¡ã³ãå
šäœã«å¯ŸããŠèšç®ãããç®çã®å€ã«å¯Ÿå¿ãããã©ã¡ãŒã¿ãŒãæ ŒçŽãããŸãã MergeãšSplitã®æåŸã«ãåã
Recalc()
åŒã³åºãã
Recalc()
ããã®åå«ã®èšç®ããããã©ã¡ãŒã¿ãŒã«åºã¥ããŠé ç¹ã®å€ã®å€ãåèšç®ããŸãã
ã»ã°ã¡ã³ãã®ãªã¯ãšã¹ã
[A; BïŒæšæºçãªæ¹æ³ã䜿çšããŸãïŒé
åããç®çã®ã»ã°ã¡ã³ããåãåããŸãïŒæåã®åãåãã®åŸãæ£ããçµæã®ç®çã®ã€ã³ããã¯ã¹ãæžå°ããããšãå¿ããªãã§ãã ããïŒïŒãããŠããã®ã«ãŒãã«æ ŒçŽãããŠãããã©ã¡ãŒã¿ãŒã®å€ãè¿ããŸãã
ãœãŒã¹ã³ãŒã-äŸãšããŠãæ倧ã
public double MaxTreeCost; public static double CostOf(ImplicitTreap treap) { return treap == null ? double.NegativeInfinity : treap.MaxTreeCost; } public void Recalc() { Size = SizeOf(Left) + SizeOf(Right) + 1; MaxTreeCost = Math.Max(Cost, Math.Max(CostOf(Left), CostOf(Right))); } public double MaxCostOn(int A, int B) { ImplicitTreap l, m, r; this.Split(A, out l, out r); r.Split(B - A, out m, out r); return CostOf(m); }
ã»ã°ã¡ã³ãäžã®è€æ°ã®æäœ
ãã©ãŒã«ã¹No.4ïŒOïŒlog
2 NïŒã®å Žåãé
åã®ãµãã»ã°ã¡ã³ãã®2çªç®ã®éšåããæäœãå®è¡ããŸãïŒå®æ°ã®è¿œå ããã€ã³ããåäžå€ãžã®èšå®ãªã©ã
MergeãšSplitã䜿çšãããšããã«ã«ãããªãŒã§ã®é
延èšç®ã®å®è£
ã¯ãŸã£ããå€æŽãããŸããã ä»äºã®åºæ¬ååã¯åãã§ããæäœãå®è¡ããåã«ãåå«ã«ãçŽæãæŒããŸããã åã®ã»ã¯ã·ã§ã³ã®è€æ°ã®ã¯ãšãªããµããŒãããå¿
èŠãããå Žåã¯ãæäœã®å®äºåŸã«ãæ£çŸ©ã埩å
ãããå¿
èŠããããŸãã
ã»ã°ã¡ã³ãã§æäœãå®è¡ããã«ã¯ãæåã«2ã€ã®SplitåŒã³åºãã§ãã®ã»ã°ã¡ã³ããããªãŒããåãåãã2ã€ã®MergeåŒã³åºãã§å床貌ãä»ããå¿
èŠããããŸãã
æ ãè
ã®ããã«ãè¿œå ã®å®å
šãªãœãŒã¹ã³ãŒããšãæ°ããMerge / Splitã®å®è£
ïŒ1ã2è¡ãç°ãªããŸãïŒãããã³
Push
ããã·ã¥æ©èœãæäŸããŸãã
public double Add; public static void Push(ImplicitTreap treap) { if (treap == null) return; treap.Cost += treap.Add; if (treap.Left != null) treap.Left.Add += treap.Add; if (treap.Right != null) treap.Right.Add += treap.Add; treap.Add = 0; } public void Recalc() { Size = SizeOf(Left) + SizeOf(Right) + 1; } public static ImplicitTreap Merge(ImplicitTreap L, ImplicitTreap R) { // ! Push( L ); Push( R ); if (L == null) return R; if (R == null) return L; ImplicitTreap answer; if (Ly > Ry) { var newR = Merge(L.Right, R); answer = new ImplicitTreap(Ly, L.Cost, L.Left, newR); } else { var newL = Merge(L, R.Left); answer = new ImplicitTreap(Ry, R.Cost, newL, R.Right); } answer.Recalc(); return answer; } public void Split(int x, out ImplicitTreap L, out ImplicitTreap R) { Push(this); // ! ImplicitTreap newTree = null; int curIndex = SizeOf(Left) + 1; if (curIndex <= x) { if (Right == null) R = null; else Right.Split(x - curIndex, out newTree, out R); L = new ImplicitTreap(y, Cost, Left, newTree); L.Recalc(); } else { if (Left == null) L = null; else Left.Split(x, out L, out newTree); R = new ImplicitTreap(y, Cost, newTree, Right); R.Recalc(); } } public ImplicitTreap IncCostOn(int A, int B, double Delta) { ImplicitTreap l, m, r; this.Split(A, out l, out r); r.Split(B - A, out m, out r); m.Add += Delta; return Merge(Merge(l, m), r); }
èš±å¯ãããæäœã«é¢ããå°ããªäœè«ïŒ
å®éãååž°çãªããªãŒæ§é ãšé
延èšç®ã«ãããã¢ãã€ãæäœãå®è£
ã§ããŸãã ã¢ãã€ãã¯ããã€ããªæŒç®âŠãæå®ãããã»ããã§ããã次ã®ããããã£ããããŸãã
â¢çµåæ§-ä»»æã®èŠçŽ aãbãcã«å¯ŸããŠïŒaâŠbïŒâŠc = aâŠïŒbâŠcïŒããããŸãã
â¢äžç«èŠçŽ ã®ååš-ã»ããã«ã¯èŠçŽ eããããä»»æã®èŠçŽ aã«å¯ŸããŠâŠe = eâŠa = aã§ãã
次ã«ããã®ãããªæäœã®ããã«ãã»ã°ã¡ã³ãã®ããªãŒ ãããã³åæ§ã®çç±ã§ãã«ã«ãããªãŒãå®è£
ããããšãã§ããŸãã
é
åãå転
ãã©ãŒã«ã¹No. 5ã¯ããµãã»ã°ã¡ã³ãããªããã§ããã€ãŸããèŠçŽ ãéé ã«äžŠã¹æ¿ããŸãã
ãããŠããã®æç¹ã§ç§ã¯ãã詳现ã«åæ¢ããŸãã é
åãå転ãããããšã¯ãã¢ãã€ãã«æäœã§ã¯ãããŸãã-ççŽã«èšã£ãŠãããã¯ã©ã®ã»ããã§ããã€ããªæäœã§ã¯ãããŸãã-ããã«ããããããã§ãã ãã®ã¿ã¹ã¯ã®ç¬èªæ§ã¯ããã®ããã®ããã·ã¥æ©èœãæãã€ãããšãã§ãããšããããšã§ãã ãŸãããªãã¬ãŒã·ã§ã³ãããã·ã¥ããæ©äŒããããããé
延ãªãã¬ãŒã·ã§ã³ãšããŠå®çŸã§ããããšãæå³ããŸãã
ãã®ãããåé ç¹ã«ããŒã«å€ïŒæ ŒçŽãããïŒãæ ŒçŽããŸãã ããã¯ããã¢ã¬ã€ã®ãã®ã»ã°ã¡ã³ããå°æ¥çã«å±éããå¿
èŠãããããšãã延æãããçŽæã§ãã 次ã«ããã®ãããã
Push
é¢æ°ã®ç¹ç°ãªããŒãžã§ã³ã§åå«ã«ããã·ã¥ã§ãããšä»®å®ãããšãããªãŒã¯åžžã«ææ°ã®ç¶æ
ã«ä¿ãããŸã-é
åèŠçŽ ã«ã¢ã¯ã»ã¹ããïŒæ€çŽ¢ïŒæäœã®åãããã³ããŒãžãšåå²ã®éå§æã«ããã·ã¥ãå®è¡ãããŸãã ãã®ãçŽæã®å±¥è¡ããã©ã®ããã«å®çŸããããç解ããããšã¯æ®ã£ãŠããŸãã
ããé ç¹
Tã§ãµãã»ã°ã¡ã³ããå転ããçŽæããããšä»®å®ããŸãã å®éã«éå§ããã«ã¯ã次ã®æé ãå®è¡ããŸãã
â¢çŸåšã®é ç¹ã§çŽæãããŸãã
T.Reversed = false;
â¢å·Šãšå³ã®æ¯åã亀æããŸãã
temp = T.Left;
T.Left = T.Right;
T.Right = temp;
â¢çŽæãåŸäžã«å€ããã 泚ïŒtrueã«èšå®ããªãã§ãã ããïŒãã®ãããããããŸã§ã«åå«ã«ãã£ããã©ããã¯ããããŸããïŒïŒããå€æŽããŠãã ããã ããã«ã¯æäœ^ã䜿çšãããŸãã
T.Left.Reversed ^ = true;
T.Right.Reversed ^ = true;
å®éããã¢ã¬ã€ãå®éã«ããªããããšã¯äœã§ããïŒ ãã®é
åã®2ã€ã®éšåïŒãµãããªãŒïŒãå®éã«äº€æããå°æ¥ãããã®2ã€ã®ãµãé
å
ãå転ããããšã
çŽæããŸãã ãã¹ãŠã®çŽæãæåŸãŸã§æºãããããšãå
ã®é
åã®èŠçŽ ãå転ããããšãç°¡åã«ããããŸãã
泚-éåžžã®ãã«ã«ãããªãŒã§ã¯ãæ€çŽ¢ããªãŒã®ããããã£ã«éåããããããã®ãããªè©æ¬ºã¯å®è¡ã§ããŸãã-å³ã®ãµãããªãŒã®ããŒã¯å·Šã®ããŒãããå°ãããªããŸãã ãã ããæé»ã®ãã«ã«ãããªãŒã«ã¯ããŒããŸã£ãããªããã€ã³ããã¯ã¹ã®å Žåã¯åžžã«ããããã£ãå°éããããããããªãŒã§å£ããããšã¯ãããŸããã
ã»ã°ã¡ã³ããåããŠãŒã¶ãŒå®çŸ©é¢æ°ã¯ãä»ã®æäœãšåæ§ã«äžå€ã®åçã§æ©èœããŸããç®çã®ã»ã°ã¡ã³ããåãåããã«ãŒãã«ãããã¹ãèšå®ããã»ã°ã¡ã³ãã貌ãä»ããŸãã ããã·ã¥ããã³ããªããæ©èœã®ãœãŒã¹ã³ãŒãã¯æ¬¡ã®ãšããã§ãã
public bool Reversed; public static void Push(ImplicitTreap treap) { if (treap == null) return; // - if (!treap.Reversed) return; var temp = treap.Left; treap.Left = treap.Right; treap.Right = temp; treap.Reversed = false; if (treap.Left != null) treap.Left.Reversed ^= true; if (treap.Right != null) treap.Right.Reversed ^= true; } public ImplicitTreap Reverse(int A, int B) { ImplicitTreap l, m, r; this.Split(A, out l, out r); r.Split(B - A, out m, out r); m.Reversed ^= true; return Merge(Merge(l, m), r); }
ä»ãããªãã¯å®å
šã«æ°ããæ¹æ³ã§å€å
žçãªã€ã³ã¿ãã¥ãŒã®ã¿ã¹ã¯ã解決ã§ããŸã:)
ã«ãŒãé
å
ãã©ãŒã«ã¹ïŒ6ïŒåŸªç°ã·ããã ãã®æäœã®æ¬è³ªã¯ãç¥ããªã人ã«ãšã£ãŠã¯ãå³ã§èª¬æããæ¹ãç°¡åã§ãã
ãã¡ãããå·¡åã·ããã¯åžžã«OïŒNïŒã§å®è¡ã§ããŸãããé
åãæé»çãªãã«ã«ãããªãŒãšããŠå®è£
ããããšã«ãããOïŒlog
2 NïŒã§ã·ããã§ããŸãã
Kã ãå·Šã«ã·ããããæé
ã¯ç°¡åã§ããã€ã³ããã¯ã¹Kã§ããªãŒãåããéã®é åºã§è²Œãä»ããŸãã å³ãžã®ã·ããã¯å¯Ÿç§°çã§ã
NKã€ã³ããã¯ã¹ã§ã«ããããå¿
èŠãããã ãã§ãã
public ImplicitTreap ShiftLeft(int K) { ImplicitTreap l, r; this.Split(K, out l, out r); return Merge(r, l); }
ç¹°ãè¿ããŸãããéåžžã®ãã«ã«ãããªãŒã§ãããã©ããã«ãããããã2ã€ã®Splitçµæãæ¥çããããšã¯åãå
¥ããããªããããæé»ã®ããŒã«ããæäœã¯ãã«ã«ãããªãŒã«å¯ŸããŠäžæã§ããçµå±ãMergeã¯é åºä»ããããããªãŒãç§ãã¡ããæåŸ
ããééã£ãé åºã§åŒæ°ããã£ãŒãããŸãã
ãŸãšã
æé»çãªããŒã«ãããã«ã«ãããªãŒã¯ãããªãŒåœ¢åŒã®é
åã®åçŽãªè¡šçŸã§ããããã«ããã察æ°æéã§ãã®ãµãé
åã䜿çšããŠäžé£ã®æäœãå®è¡ã§ããŸãã åæã«ãOïŒNïŒã¯ãŸã ã¡ã¢ãªã浪費ããŠããŸãã ãã¡ãããOè¡šèšã䜿çšããŠãç§ã¯å°ãå¿ãæ²ãããŸãããå®éã®ç掻ã§ã¯ãããªãŒãå ããå®éã®èšæ¶ãéèŠã§ããããã«ã«ãããªãŒã¯ãã®ãªãŒããŒãããã§æåã ããã§ãã èªåã®å€æïŒæ
å ±ã®Nãåªå
床ã®Nãåå«ãžã®ãªã³ã¯ã®2NããµãããªãŒã®ãµã€ãºã®N-ããã¯ç掻è³éã§ããè€æ°ã®ãªã¯ãšã¹ããè¿œå ãããšãæäœããšã«å¥ã®NãåŸãããŸãã
æ
å ±ããåªå
é äœãäœæããããšã§ãããªãã®äººçãå°ãæ¹åããããšãã§ããŸãããããã¯ã第äžã«ãæµ·ã®äœäžïŒãã€ãã¹NïŒã§ããã第äºã«ãã»ãã¥ãªãã£ã®èŠ³ç¹ããçµæã«æºã¡ãŠããŸãïŒèª°ããäœæããæ©èœãèŠã€ããå Žååªå
é äœãèšå®ãããšããã«ã«ãããªãŒã®ãã©ã³ã¹ã倧å¹
ã«åŽ©ãããã«ãæªæã®ããæ¹æ³ã§æ°ããã¬ã³ãŒããäœæã§ããå¯èœæ§ããããŸãã æçµçã«ããã¹ãŠã®ããŒã¿ãèããé
ããªãç¶æ³ãçºçããå¯èœæ§ããããŸã-ãã¡ããããã®ãããªå Žåã¯èªã¿åãããé¡èãªäœæ¥ãå¿
èŠã§ãã ããªãŒã®ç°ãªãé ç¹ã«ç°ãªãçŽ æ°Pã䜿çšããããšã§å±éºãåãé€ãããšãã§ããŸã...ããããããã¯å¥ã®ç§åŠçç 究ã®ãããã¯ã§ãã å人çã«ã¯ããã«ã«ãããªãŒã®æ©èœãšãã®ã³ãŒãã®åçŽãã¯ãé«ãã¡ã¢ãªæ¶è²»ã®åé¡ãè¶
ããå©ç¹ã§ãã ãã¡ãããããã°ã©ã ã¯ããã°ã©ã ã«ãã£ãŠç°ãªããŸããã
èå³æ·±ãäºå®ããïŒè¥¿æŽã®æåŠãèšäºãã€ã³ã¿ãŒãããã§ã¯ãæé»ã®ããŒã«ãããã«ã«ãã®ããªãŒã«ã€ããŠã®åäžã®èšåãèŠã€ããããšãã§ããŸããã§ããã ãã¡ãããè±èªã®çšèªã§äœãšåŒã°ããã¹ããã¯èª°ã«ãããããŸãããããã©ãŒã©ã ãStackOverflowã§ã®è³ªåãäœã«ãã€ãªãããŸããã§ããã ãã·ã¢ã®ã¹ããŒãããã°ã©ãã³ã°ACM ICPCã®å®è·µã«ãããŠããã®æ§é ã¯2000幎ã«åããŠäœ¿çšãããåœé競æ倧äŒã®å€æ°ã®åè
ã§ããåç«ã³ã³ãã¥ãŒãã£ã³ã°ããŒã ã®ãã³ã©ã€ãã¥ããã®ã¡ã³ããŒã«ãã£ãŠçºæãããŸããïŒãã ããRunetã¯åœŒã®
å
åŒPavelãç¥ã£ãŠããŸãïŒã
ãã«ã«ãã®æšã«é¢ãã矩åçãªããã°ã©ã ã¯ãç§ãçµãããšããã§ãã ã»ãšãã©ã®å Žåãå°ãªããšã1ã€ãŸãã¯2ã€ã®éšåããããŸã-ããªãŒæäœã®ä»£æ¿å®è£
ãããã³ãã®æ©èœå®è£
ã«ã€ããŠ-ãã ããååãšããŠãã§ã«èšè¿°ãããŠãã3ã€ã¯ã人çã§ãã©ãããå®å
šã«äœ¿çšããã®ã«ååãªåŒŸè¬ãæ§æããŸãã ãã®ãã¥ãŒããªã¢ã«ã®è¡ãéããŠæ£çŽã«ç§ãšæ Œéãããã¹ãŠã®äººã«æè¬ããŸã:)ããªããèå³ãæã£ãŠããããšãé¡ã£ãŠããŸãã