ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

BZOJ 1507 NOI 2003 Editor Splay
2015-07-20 17:34:50 À´Ô´: ×÷Õß: ¡¾´ó ÖРС¡¿ ä¯ÀÀ:1´Î
Tags£ºBZOJ 1507 NOI 2003 Editor Splay

ÌâÄ¿´óÒ⣺ά»¤Ò»ÖÖÊý¾Ý½á¹¹£¬Ëü¿ÉÒÔ£º

1.ÒÆ¶¯¹â±ê

2.ÔÚ¹â±êÖ®ºó²åÈëÒ»¶Î×Ö·û´®

3.ɾ³ý¹â±êÖ®ºóµÄn¸ö×Ö·û

4.Êä³ö¹â±êÖ®ºóµÄn¸ö×Ö·û

5.ÒÆ¶¯¹â±ê


˼·£ºSplay£¬Ã»Ê²Ã´ÌرðµÄ¡£µ«ÊÇÓм¸¸öÐèҪעÒâµÄµØ·½¡£1.ÌâÖÐ˵£ºdelete²Ù×÷²»»áÔ½½ç¡£µ«ÊÇÆäʵÓпÉÄÜ»áÔ½½ç£¬±ÈÈçÑùÀý¾ÍÔ½½çÁË¡£¡£

2.Êä³öµÄʱºòÒ»¶¨²»ÒªÍµÀÁ¡£ÎÒ¸Õ¿ªÊ¼Ð´µÄʱºò¾Í°ÑÊä³öд³ÉnlognÊä³öµÄÁË£¬È»ºó¹û¶ÏTÁËÂð£¬ÎÒ»¹ÒÔΪÊÇÄÄÀïËÀÑ­»·ÁË£¬½á¹ûÊÇÕâÀï±»¿¨ÁË£¡ÒÔºóÔÙÒ²²»ÍµÀÁËæ±ã¼ÓlognÁË¡£¡£¡£


CODE£º


#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #define MAX (1 << 22|100) using namespace std; struct Complex{ char c; int size; Complex *son[2],*father; void Combine(Complex *a,bool dir) { son[dir] = a; a->father = this; } bool Check() { return father->son[1] == this; } void PushUp() { size = son[0]->size + son[1]->size + 1; } }*nil = new Complex(),*root; void Pretreatment(); Complex *BuildTree(int l,int r); Complex *Kth(Complex *a,int k); Complex *NewComplex(char val); inline void Rotate(Complex *a,bool dir); inline void Splay(Complex *a,Complex *aim); void Print(Complex *a); char src[MAX]; int asks,now; char s[MAX]; int main() { Pretreatment(); cin >> asks; for(int x,i = 1;i <= asks; ++i) { scanf("%s",s); if(s[0] == 'M') scanf("%d",&now); else if(s[0] == 'I') { scanf("%d",&x); for(int j = 1;j <= x; ++j) while(scanf("%c",&src[j]),src[j] < 32); Splay(Kth(root,now + 1),nil); Splay(Kth(root,now + 2),root); root->son[1]->Combine(BuildTree(1,x),false); root->son[1]->PushUp(); root->PushUp(); } else if(s[0] == 'D') { scanf("%d",&x); Splay(Kth(root,now + 1),nil); Splay(Kth(root,min(root->size,now + x + 2)),root); root->son[1]->son[0] = nil; root->son[1]->PushUp(); root->PushUp(); } else if(s[0] == 'G') { scanf("%d",&x); Splay(Kth(root,now + 1),nil); Splay(Kth(root,min(now + x + 2,root->size)),root); Print(root->son[1]->son[0]); puts(""); } else if(s[0] == 'P') --now; else ++now; } return 0; } void Pretreatment() { root = BuildTree(1,2); root->father = nil; nil->father = nil->son[0] = nil->son[1] = nil; nil->size = 0; } Complex *BuildTree(int l,int r) { if(l > r) return nil; int mid = (l + r) >> 1; Complex *re = NewComplex(src[mid]); re->Combine(BuildTree(l,mid - 1),false); re->Combine(BuildTree(mid + 1,r),true); re->PushUp(); return re; } Complex *Kth(Complex *a,int k) { if(k <= a->son[0]->size) return Kth(a->son[0],k); k -= a->son[0]->size; if(k == 1) return a; return Kth(a->son[1],k - 1); } Complex *NewComplex(char val) { Complex *re = new Complex(); re->c = val; re->size = 1; re->son[0] = re->son[1] = nil; return re; } inline void Rotate(Complex *a,bool dir) { Complex *f = a->father; f->son[!dir] = a->son[dir]; f->son[!dir]->father = f; a->son[dir] = f; a->father = f->father; f->father->son[f->Check()] = a; f->father = a; f->PushUp(); if(root == f) root = a; } inline void Splay(Complex *a,Complex *aim) { while(a->father != aim) { if(a->father->father == aim) Rotate(a,!a->Check()); else if(!a->father->Check()) { if(!a->Check()) Rotate(a->father,true),Rotate(a,true); else Rotate(a,false),Rotate(a,true); } else { if(a->Check()) Rotate(a->father,false),Rotate(a,false); else Rotate(a,true),Rotate(a,false); } } a->PushUp(); } void Print(Complex *a) { if(a == nil) return ; Print(a->son[0]); putchar(a->c); Print(a->son[1]); }
     
    
   
  


¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
·ÖÏíµ½: 
ÉÏһƪ£ºhdu 1814 Peaceful Commission £¨.. ÏÂһƪ£ºhdu 5030 Rabbit's String(ºó..

ÆÀÂÛ

ÕÊ¡¡¡¡ºÅ: ÃÜÂë: (ÐÂÓû§×¢²á)
Ñé Ö¤ Âë:
±í¡¡¡¡Çé:
ÄÚ¡¡¡¡ÈÝ:

¡¤Linuxϵͳ¼ò½é (2025-12-25 21:55:25)
¡¤Linux°²×°MySQL¹ý³Ì (2025-12-25 21:55:22)
¡¤Linuxϵͳ°²×°½Ì³Ì£¨ (2025-12-25 21:55:20)
¡¤HTTP Åc HTTPS µÄ²î„ (2025-12-25 21:19:45)
¡¤ÍøÕ¾°²È«±ØÐ޿ΣºÍ¼ (2025-12-25 21:19:42)