ZOJ 3765 Lights (SPLAY)
来源:互联网 发布:淘宝换货 编辑:程序博客网 时间:2024/06/13 11:20
splay。只为了贴代码保存,没用其他作用。
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;#define maxn 500007#define inf 1000000000struct Node{ Node *fa,*ch[2]; int val,gcd[2],size,statu;};Node pool[maxn];Node *nil;int cnt = 0;int gcd(int a,int b){ if(b <= 0) return a; return gcd(b,a%b);}void init(){ cnt = 1; nil = pool; nil->size = 0; nil->fa = nil; nil->gcd[0] = nil->gcd[1] = -1;}Node *newnode(int val,int statu,Node *f){ pool[cnt].fa = f; pool[cnt].ch[0]=pool[cnt].ch[1]=nil; pool[cnt].val = val; pool[cnt].size = 1; pool[cnt].gcd[statu] = val; pool[cnt].gcd[!statu] = -1; pool[cnt].statu = statu; return &pool[cnt++];}//splay向上更新信息******void update(Node *x){ x->size = x->ch[0]->size + x->ch[1]->size + 1; int statu = x->statu; x->gcd[statu] = x->val; x->gcd[!statu] = -1; if(x->ch[0] != nil){ x->gcd[0] = gcd(x->ch[0]->gcd[0],x->gcd[0]); x->gcd[1] = gcd(x->ch[0]->gcd[1],x->gcd[1]); } if(x->ch[1] != nil){ x->gcd[0] = gcd(x->ch[1]->gcd[0],x->gcd[0]); x->gcd[1] = gcd(x->ch[1]->gcd[1],x->gcd[1]); }}//将结点x旋转至splay中父亲的位置******void rotate(Node *x){ Node *f = x->fa, *ff = f->fa; int t = (f->ch[1] == x); if(ff != nil) ff->ch[ff->ch[1] == f] = x; x->fa = ff; f->ch[t] = x->ch[t^1]; x->ch[t^1]->fa = f; x->ch[t^1] = f; f->fa = x; update(f);}//将结点x旋转至x所在splay的根位置******void splay(Node *x,Node *end){ Node *f, *ff; while(x->fa!=end){ f = x->fa,ff = f->fa; if(f->fa != end) if((ff->ch[1]==f)&&(f->ch[1] == x)) rotate(f); else rotate(x); rotate(x); } update(x);}Node*find(int k,Node *root){ if(root->ch[0]->size == k-1) return root; if(root->ch[0]->size >= k) return find(k,root->ch[0]); return find(k-root->ch[0]->size-1,root->ch[1]);}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF){ init(); Node* root = newnode(-1,0,nil); Node* left,*right; int l,r,u,statu,val; char x[2]; for(int i = 0;i < n; i++){ scanf("%d%d",&val,&statu); left = newnode(val,statu,root); root->ch[1] = left; splay(left,nil); root = left; } left = newnode(-1,0,root); root->ch[1] = left; splay(left,nil); root = left; while(m--){ scanf("%s",x); if(x[0] == 'Q'){ scanf("%d%d%d",&l,&r,&statu); root = find(l,root); splay(root,nil); right = find(r+2,root); splay(right,root); printf("%d\n",right->ch[0]->gcd[statu]); } else if(x[0] == 'I'){ scanf("%d%d%d",&l,&val,&statu); root = find(l+1,root); splay(root,nil); right = newnode(val,statu,root); right->ch[1] = root->ch[1]; root->ch[1]->fa = right; root->ch[1] = right; update(right); splay(right,nil); root = right; } else if(x[0] == 'D'){ scanf("%d",&l); root = find(l,root); splay(root,nil); right = find(l+2,root); splay(right,root); right->ch[0] = nil; splay(root=right,nil); } else if(x[0] == 'R'){ scanf("%d",&l); root = find(l+1,root); splay(root,nil); root->statu = !root->statu; update(root); } else if(x[0] == 'M'){ scanf("%d%d",&l,&val); root = find(l+1,root); splay(root,nil); root->val = val; update(root); } } } return 0;}
0 0
- zoj 3765 Lights splay
- ZOJ 3765 Lights (SPLAY)
- [ZOJ 3765 Lights] Splay
- zoj 3765 Lights(Splay)
- ZOJ 3765 Lights (SPLAY)
- 【splay tree】 ZOJ 3765 Lights
- ZOJ-3765 Lights(Splay树)
- ZOJ 3765 Lights Splay Tree的几种基本操作
- ZOJ Monthly, March 2014,3765 Lights (Splay 基本操作,并维护区间上的信息 * 模板)
- ZOJ 3765 Lights
- ZOJ 3765 Lights(SplayTree)
- zoj 3765 Lights(伸展树)
- ZOJ 3765 Lights (伸展树)
- ZOJ--3765--Lights【伸展树】
- ZOJ 3765 splay
- zoj3765 Lights ----splay
- ZOJ 3765 —— Lights(伸展树)
- ZOJ 1354 Extended Lights Out
- [前端开发] 垂直居中,flexbox/table-cell/absolute
- 【CPP课程实训】-----银行系统
- poj3278
- OC-类目
- POJ2828------Buy Tickets
- ZOJ 3765 Lights (SPLAY)
- hdu2044(组合数学)
- 【转载】豆瓣最受欢迎的书评——《透明的哀伤》
- 第一周第四天([大小写变换问题][判断是否为email][求对角线的值] [生成数组][数字中添加逗号][生成10个两位随机数,然后再进行排序] [复制数组||将指定数组的指定范围复制到新的数组])
- NYoj 动态规划 聪明的KK
- 【小结】2-sat
- 【android】android studio下二维码类库zxing精简包的配置及第三方类库的导入
- Android StageFrightMediaScanner源代码解析
- mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法