//将从第pos个数开始的连续的tot个数修改为c void Make_Same(int pos,int tot,int c) { Splay(Get_kth(root,pos),0); Splay(Get_kth(root,pos+tot+1),root); Update_Same(Key_value,c); push_up(ch[root][1]); push_up(root); }
//将第pos个数开始的连续tot个数进行反转 void Reverse(int pos,int tot) { Splay(Get_kth(root,pos),0); Splay(Get_kth(root,pos+tot+1),root); Update_Rev(Key_value); push_up(ch[root][1]); push_up(root); }
//在第pos个数后面插入tot个数 void Insert(int pos,int tot) { for(int i = 0;i < tot;i++)scanf("%d",&a[i]); Splay(Get_kth(root,pos+1),0); Splay(Get_kth(root,pos+2),root); Build(Key_value,0,tot-1,ch[root][1]); push_up(ch[root][1]); push_up(root); }
//从第pos个数开始连续删除tot个数 void Delete(int pos,int tot) { Splay(Get_kth(root,pos),0); Splay(Get_kth(root,pos+tot+1),root); erase(Key_value); pre[Key_value] = 0; Key_value = 0; push_up(ch[root][1]); push_up(root); }
void Init() { root = tot1 = tot2 = 0; ch[root][0] = ch[root][1] = size[root] = pre[root] = 0; same[root] = rev[root] = sum[root] = key[root] = 0; lx[root] = rx[root] = mx[root] = -INF; NewNode(root,0,-1); NewNode(ch[root][1],root,-1); for(int i = 0;i < n;i++) scanf("%d",&a[i]); Build(Key_value,0,n-1,ch[root][1]); push_up(ch[root][1]); push_up(root); }
void rotate(int x) { int y=fa[x],p=ch[y][1]==x; ch[y][p]=ch[x][!p]; fa[ch[y][p]] = y; fa[x]=fa[y]; fa[y]=x; ch[x][!p]=y; if(rt[y]) rt[y]=false,rt[x]=true; else ch[fa[x]][ch[fa[x]][1]==y]=x; push_up(y); push_up(x); }
inline void Splay(int x, int goal) { push_down(x); while (pre[x] != goal) { if (pre[pre[x]] == goal) { Rotate(x , ch[pre[x]][0] == x); } else { int y = pre[x] , z = pre[y]; int f = (ch[z][0] == y); if (ch[y][f] == x) { Rotate(x , !f) , Rotate(x , f); } else { Rotate(y , f) , Rotate(x , f); } } } push_up(x); if (goal == 0) root = x; }
//Splay调整,将r结点调整到goal下面 void Splay(int r,int goal) { push_down(r); while(pre[r] != goal) { if(pre[pre[r]] == goal) { push_down(pre[r]); push_down(r); Rotate(r,ch[pre[r]][0] == r); } else { push_down(pre[pre[r]]); push_down(pre[r]); push_down(r); int y = pre[r]; int kind = ch[pre[y]][0]==y; if(ch[y][kind] == r) { Rotate(r,!kind); Rotate(r,kind); } else { Rotate(y,kind); Rotate(r,kind); } } } push_up(r); if(goal == 0) root = r; }
inline void push_down(int x) { int l=t[x].ls,r=t[x].rs; Inv(l); Inv(r); t[x].tag=0; push_up(x); }
inline void init(int n) {/*这是题目特定函数*/ ch[0][0] = ch[0][1] = pre[0] = sz[0] = 0; add[0] = sum[0] = 0; root = top1 = 0; //为了方便处理边界,加两个边界顶点 NewNode(root , -1); NewNode(ch[root][1] , -1); pre[top1] = root; sz[root] = 2; for (int i = 0 ; i < n ; i ++) scanf("%d",&num[i]); makeTree(keyTree , 0 , n-1 , ch[root][1]); push_up(ch[root][1]); push_up(root); }
void newline() { if (cursor_row + 1 >= ROWS) push_up(); else ++cursor_row; cursor_col = 0; }
void push_b2(t_box *box) { push_down(box->b, box->size); B[0] = A[0]; push_up(box->a, box->size); box->size_a--; box->size_b++; box->nb_op++; }
void push_a2(t_box *box) { push_down(box->a, box->size); A[0] = B[0]; push_up(box->b, box->size); box->size_a++; box->size_b--; box->nb_op++; }
void Build(int &x,int l,int r,int father) { if(l > r)return; int mid = (l+r)/2; NewNode(x,father,a[mid]); Build(ch[x][0],l,mid-1,x); Build(ch[x][1],mid+1,r,x); push_up(x); }
/*初始化*/ inline void makeTree(int &x,int l,int r,int f) { if(l > r) return ; int m = (l + r)>>1; NewNode(x , num[m]); /*num[m]权值改成题目所需的*/ makeTree(ch[x][0] , l , m - 1 , x); makeTree(ch[x][1] , m + 1 , r , x); pre[x] = f; push_up(x); }
void cut(int x,int y) { mroot(x); splay(y); fa[ch[y][0]]=fa[y]; fa[y]=0; rt[ch[y][0]]=true; ch[y][0]=0; push_up(y); }
void update(int ul,int rt){ if(ul==e[rt].l&&ul==e[rt].r){ e[rt].lflag=e[rt].rflag=ul; return ; } int mid=(e[rt].l+e[rt].r)>>1; if(ul<=mid) update(ul,lson); else update(ul,rson); push_up(rt); }
void access(int x) { int y=0,z=x; for(;x;x=fa[y=x]) { splay(x); rt[ch[x][1]]=true,rt[ch[x][1]=y]=false; push_up(x); } splay(z); }
void push_a(t_box *box) { push_down(box->a, box->size); A[0] = B[0]; push_up(box->b, box->size); box->size_a++; box->size_b--; if (box->b_color == 1) PURPLE; ft_putstr("pa\n"); box->nb_op++; }
void push_b(t_box *box) { push_down(box->b, box->size); B[0] = A[0]; push_up(box->a, box->size); box->size_a--; box->size_b++; if (box->b_color == 1) RED; ft_putstr("pb\n"); box->nb_op++; }
void update(int v, int p, int l, int r, int rt) { if (l == r) { left[rt] = right[rt] = v; return; } int mid = (l + r) >> 1; if (p <= mid) update(v, p, l, mid, rt<<1); else update(v, p, mid+1, r, rt<<1|1); push_up(l, r, rt); }
void build(int l, int r, int rt) { if (l == r) { left[rt] = right[rt] = 1; return; } int mid = (l + r) >> 1; build(l, mid, rt<<1); build(mid+1, r, rt<<1|1); push_up(l, r, rt); }
void Change(int x,int l,int r,int v) { if(l<=t[x].l&&r>=t[x].r&&(t[x].max[0]<=v||t[x].min[0]>v)) { if(t[x].max[0]<=v) Inv(x); return; } if(t[x].tag) push_down(x); int mid=t[x].l+t[x].r>>1; if(l<=mid) Change(t[x].ls,l,r,v); if(r>mid) Change(t[x].rs,l,r,v); push_up(x); }
void build(int l, int r, int rt) { if (l == r) { flag[rt] = 1; sum[rt] = 1; return; } int mid = (l + r) >> 1; build(l, mid, rt<<1); build(mid+1, r, rt<<1|1); push_up(rt); }
void update(int v, int L, int R, int l, int r, int rt) { if (L <= l && R >= r) { sum[rt] = v * (r - l + 1); flag[rt] = v; return; } int mid = (l + r) >> 1; push_down(rt); if (L <= mid) update(v, L, R, l, mid, rt<<1); if (R > mid) update(v, L, R, mid + 1, r, rt<<1|1); push_up(rt); }
//旋转,0为左旋,1为右旋 void Rotate(int x,int kind) { int y = pre[x]; push_down(y); push_down(x); ch[y][!kind] = ch[x][kind]; pre[ch[x][kind]] = y; if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y] = x; pre[x] = pre[y]; ch[x][kind] = y; pre[y] = x; push_up(y); }
int Build(int l,int r) { int x=++tot; t[x].l=l;t[x].r=r; if(l==r) { for(int i=0;i<=1;i++) t[x].min[i]=t[x].max[i]=t[x].sum[i]=a[l][i]; return x; } int mid=l+r>>1; t[x].ls=Build(l,mid); t[x].rs=Build(mid+1,r); push_up(x); return x; }
void run() { char ch; while(1) { menu(); ch=getch(); switch(ch) { case '1': push_down(); break; case '2': pop_down(); break; case '3': push_up(); break; case '4': pop_up(); break; case '5': printall(); break; case '6': return; } system("pause"); } }
int BinaryHeap::push_down2(int val) { int k = val; int value=A[k]; bool end = false; int count=0; do { if(((2*val)+1)<size && A[(2*val)+1] <= A[2*val]) k=(2*val)+1; else if((2*val) < size) k=2*val; A[val]=A[k]; A[k]=value; val=k; count++; if((2*val)>=size) end=true; }while(!end); count+=push_up(val); return count; }
void update(int l,int r,int c,int L,int R,int rt) { if(l<=L && r>=R) { cnt[rt]+=c; push_up(L,R,rt); return; } int m=(L+R)>>1; if(l<m) update(l,r,c,L,m,rt<<1); if(r>m) update(l,r,c,m,R,rt<<1|1); push_up(L,R,rt); }