Bzoj 1861 [Zjoi2006]book Bookcase--splay

Source: Internet
Author: User

"Problem Analysis"

Template title.

Two virtual nodes, very convenient operation.

Code

#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <map># Include <set> #include <queue> #include <string> #include <iostream> #include <algorithm> using namespace std; #define MAXN 500005#define inf 0x3f3f3f3f#define F (I,J,K) for (int i=j;i<=k;++i) #define D (i,j,k) fo R (int i=j;i>=k;--i) #define L ch[o][0] #define R ch[o][1] void Finout () {#ifndef Online_judge freopen ("In.txt", "R"    , stdin);//Freopen ("OUT.txt", "w", stdout);    #endif}int Getint () {int x=0,f=1; char Ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') f=-1; Ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int n,m;struct bit_tree{int a[maxn],b[maxn];void Add (int x,int y,int z) {for (int i=x;i<=n;i+=i& (-i)) b[i]+=z;for ( int i=y+1;i<=n;i+=i& (-i)) b[i]-=z;for (int i=x;i<=n;i+=i& (-i)) a[i]+= (n-x) *z;for (int i=y+1;i<=n;i+= i& (-i)) a[i]-= (n-y-1) *z;} INT getsum (int x) {int ret=0,tmp=0;for (int i=x;i;i-=i& (-i)) ret+=a[i];for (int i=x;i;i-=i& (-i)) Tmp+=b[i];return ret-(n-x-1) *tmp;} void Init () {memset (a,0,sizeof a); Memset (b,0,sizeof b);}} T;int Rt=0,a[maxn],s,t,id[maxn],cnt=0;char Opt[10];int num[maxn],ch[maxn][2],siz[maxn],fa[maxn],list[maxn];void Update (int o) {siz[o]=siz[l]+siz[r]+1;}    void rot (int x,int &k) {//cout<< "Rot" <<x<<endl;    int y=fa[x],z=fa[y],l,r; if (ch[y][0]==x) l=0;    else l=1;    r=l^1;    if (y==k) k=x;        else {if (ch[z][0]==y) ch[z][0]=x;    else ch[z][1]=x;    } fa[x]=z;    Fa[y]=x;    Fa[ch[x][r]]=y;    CH[Y][L]=CH[X][R];    Ch[x][r]=y; Update (y); Update (x);    }void splay (int x,int &k) {int y,z;        while (x!=k) {y=fa[x];z=fa[y];            if (y!=k) {if ((ch[z][0]==y) ^ (ch[y][0]==x)) rot (x,k);        else rot (y,k);    } rot (x,k); }}void ins (int & o,int X,int lst) {if (!o) {O=++cnt;fa[o]=lst;num[o]=x;siz[o]=1;list[o]=a[X-1];splay (O,RT); return;} if (X<num[o]) ins (ch[o][0],x,o); else ins (ch[o][1],x,o); update (o);} int find (int o,int x) {//cout<< "Qnum" <<num[o]<< "" "<<x<<endl; if (siz[l]+1==x) return o;if (siz[l]>=x) return find (l,x); else return find (r,x-siz[l]-1);} void print (int o) {if (!o) return;p rint (L);//cout<< "Now is" <<o<<endl;//cout<<L<< "" < <R<<endl;//cout<<num[o]<< "<<siz[o]<<endl;cout<<num[o]<<";p rint ( R);} void Mov (int o,int k) {splay (O,RT); int pre=find (rt,siz[l]), Nxt=find (rt,siz[l]+2); splay (PRE,RT); splay (Nxt,ch[rt][1]) ; ch[nxt][0]=0; Fa[o]=0;update (NXT); Update (pre); splay (NXT,RT);p re=find (rt,k-1); Nxt=find (rt,k); splay (PRE,RT); Splay (nxt,ch[rt][1]); ch[nxt][0]=o; Fa[o]=nxt;splay (O,RT);}    int main () {finout ();    scanf ("%d%d", &n,&m);    F (i,1,n) scanf ("%d", &a[i]), id[a[i]]=i+1;    F (i,0,n+1) ins (rt,i,0);//print (RT);//cout<<rt<<endl; F (i,1,m) {//PRint (RT);//cout<<endl;    scanf ("%s", opt);    scanf ("%d", &s);    if (opt[0]== ' Q ') {//cout<< "QUERY" <<endl;printf ("%d\n", A[find (rt,s+1)-1]);}    else if (opt[0]== ' A ') {//cout<< "ASK" <<endl;    Splay (ID[S],RT); printf ("%d\n", siz[ch[rt][0]]-1);} else if (opt[0]== ' T ') {//cout<< "TOP" <<endl; Mov (id[s],1+1);} else if (opt[0]== ' B ') {//cout<< "BOT" <<endl; Mov (id[s],n+1);} Else{t=getint (); splay (id[s],rt); int tmp=siz[ch[rt][0]]; Mov (id[s],tmp+t+1);}}}

  

Bzoj 1861 [Zjoi2006]book Bookcase--splay

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.