Sgu-263 Towers

Source: Internet
Author: User

Sgu-263 Towers
Question:

Slave 1 ~ 106 Yes 106 Base. At first, there was no building block, and the height was 0 , The continuous section height is greater 0 The base is counted as one Tower Apparently Tower Number 0 .
The following two operations are available:
1. put x c Set C Blocks are placed in X Base.
2. tput t x c Set C Blocks are placed in T Items Tower The X Base ( Tower Arrange from left to right and ensure this base exists ) .
Then there are four questions:
1. towers How many Tower .
2. cubes t Qidi T Items Tower The total number of building blocks.
3. length t Qidi T Items Tower The number of substrates, that is, the width.
4. tcube t x Qidi T Items Tower The X Number of blocks on the base.


Solution:

Bare Question of the Balance Tree ( It seems like the practice of a cable segment tree, but I won't. QAQ) .
We construct a Balance Tree for maintenance. Tower , Record the left endpoint, right endpoint, and the total number of blocks. If no blocks exist on the current base each time the building blocks are added, consider whether a new one will be added. Tower , Or expand Tower , Or merge two Tower . In general, it is quite simple.


AC code:
#include 
  
   #include 
   
    #include 
    
     #include 
     
      #include #include 
      
       using namespace std;int QAQ;struct tree_{ int son[2],fa,Size,left,right,cubes; inline void clear() {son[0]=son[1]=fa=Size=left=right=cubes=0;}}tower[1000010];int tower_end=0;int root;int height[1000010]={0};inline void read_(int &x){ x=0; char ch=getchar(); for(;ch=='\n' || ch=='\r' || ch==' ';ch=getchar()); for(;ch>='0' && ch<='9';ch=getchar()) x=x*10+ch-'0'; return;}void rotate(int k){ int Fa=tower[k].fa; int g=(tower[Fa].son[1]==k); tower[k].fa=tower[Fa].fa; tower[k].Size=tower[Fa].Size; if(tower[k].son[g^1]!=0) tower[tower[k].son[g^1]].fa=Fa; tower[Fa].Size=tower[tower[Fa].son[g^1]].Size+tower[tower[k].son[g^1]].Size+1; tower[Fa].son[g]=tower[k].son[g^1]; tower[Fa].fa=k; tower[k].son[g^1]=Fa; if(tower[k].fa!=0) { g=(tower[tower[k].fa].son[1]==tower[k].son[g^1]); tower[tower[k].fa].son[g]=k; } return;}inline void splay(int k){ for(;tower[k].fa!=0;) { if(tower[k].fa==root) rotate(k); else { int Fa=tower[k].fa; int g1=(tower[tower[k].fa].son[1]==k); int g2=(tower[tower[Fa].fa].son[1]==Fa); if(g1==g2) { rotate(Fa); rotate(k); } else { rotate(k); rotate(k); } } } root=k; return;}inline int Findtower(int k,int now){ int l=tower[now].son[0]; int r=tower[now].son[1]; if(tower[l].Size>=k) return Findtower(k,l); if(tower[l].Size+1
       
        k) return Findtower2(k,tower[now].son[0]); if(tower[now].right
        
         0;QAQ--) { char ch[10]="\0"; scanf("%s",ch); if(strcmp("put",ch)==0) { int x,c; read_(x),read_(c); if(height[x]!=0) { int tt=Findtower2(x,root); tower[tt].cubes+=c; } else { if(height[x-1]==0 && height[x+1]==0) { int tt; if(root==0) { tt=++tower_end; tower[tt].left=tower[tt].right=x; tower[tt].Size=1; root=tt; } else { tt=Add(x,root); splay(tt); } tower[tt].cubes+=c; } else if((height[x-1]>0)+(height[x+1]>0)==1) { int xx=height[x-1]>0?x-1:x+1; int tt=Findtower2(xx,root); tower[tt].cubes+=c; if(xx==x-1) tower[tt].right=x; else tower[tt].left=x; splay(tt); } else { int tt1=Findtower2(x-1,root); int tt2=Findtower2(x+1,root); splay(tt2);splay(tt1); tower[tt1].cubes+=tower[tt2].cubes+c; tower[tt1].right=tower[tt2].right; tower[tt1].son[1]=tower[tt2].son[1]; tower[tt1].Size--; if(tower[tt2].son[1]!=0) tower[tower[tt2].son[1]].fa=tt1; tower[tt2].clear(); } } height[x]+=c; } else if(strcmp("tput",ch)==0) { int t,x,c; read_(t); //scanf("%d",&t); t=Findtower(t,root); read_(x),read_(c); // scanf("%d%d",&x,&c); height[tower[t].left+x-1]+=c; tower[t].cubes+=c; splay(t); } else if(strcmp("towers",ch)==0) printf("%d towers\n",tower[root].Size); else if(strcmp("cubes",ch)==0) { int t; read_(t); //scanf("%d",&t); int tt=Findtower(t,root); splay(tt); printf("%d cubes in %dth tower \n",tower[tt].cubes,t); } else if(strcmp("length",ch)==0) { int t; read_(t); //scanf("%d",&t); int tt=Findtower(t,root); splay(tt); printf("length of %dth tower is %d\n",t,tower[tt].right-tower[tt].left+1); } else if(strcmp("tcubes",ch)==0) { int t,x; read_(t),read_(x); //scanf("%d%d",&t,&x); int tt=Findtower(t,root); splay(tt); printf("%d cubes in %dth column of %dth tower\n",height[tower[tt].left+x-1],x,t); } } return 0;}
        
       
      
     
    
   
  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.