先发两个模板,都是网上找的略作修改后的。

  1.普通版。支持lazy操作、重复值、找前驱后继、单个删除、值区间删除等。代码对应的题是BZOJ1588 - 营业额统计。

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 const int MAXN = 100011;
  4 
  5 struct SplayTree{
  6     int cnt, rt;
  7     int Add[MAXN];
  8 
  9     struct Node{
 10         int key, cnt, size, fa, son[2];
 11     }T[MAXN];
 12 
 13     inline void PushUp(int x){
 14         T[x].size=T[T[x].son[0]].size+T[T[x].son[1]].size+T[x].cnt;
 15     }
 16 
 17     inline void PushDown(int x){
 18         if(Add[x]){
 19             if(T[x].son[0]){
 20                 T[T[x].son[0]].key+=Add[x];
 21                 Add[T[x].son[0]]+=Add[x];
 22             }
 23             if(T[x].son[1]){
 24                 T[T[x].son[1]].key+=Add[x];
 25                 Add[T[x].son[1]]+=Add[x];
 26             }
 27             Add[x]=0;
 28         }
 29     }
 30 
 31     inline int Newnode(int key, int fa){ //新建一个节点并返回
 32         ++cnt;
 33         T[cnt].key=key;
 34         T[cnt].cnt=T[cnt].size=1;
 35         T[cnt].fa=fa;
 36         T[cnt].son[0]=T[cnt].son[1]=0;
 37         return cnt;
 38     }
 39 
 40     inline void Rotate(int x, int p){ //0左旋 1右旋
 41         int y=T[x].fa;
 42         PushDown(y);
 43         PushDown(x);
 44         T[y].son[!p]=T[x].son[p];
 45         T[T[x].son[p]].fa=y;
 46         T[x].fa=T[y].fa;
 47         if(T[x].fa)
 48             T[T[x].fa].son[T[T[x].fa].son[1] == y]=x;
 49         T[x].son[p]=y;
 50         T[y].fa=x;
 51         PushUp(y);
 52         PushUp(x);
 53     }
 54 
 55     void Splay(int x, int to){ //将x节点移动到To的子节点中
 56         while(T[x].fa != to){
 57             if(T[T[x].fa].fa == to)
 58                 Rotate(x, T[T[x].fa].son[0] == x);
 59             else{
 60                 int y=T[x].fa, z=T[y].fa;
 61                 int p=(T[z].son[0] == y);
 62                 if(T[y].son[p] == x)
 63                     Rotate(x, !p), Rotate(x, p); //之字旋
 64                 else
 65                     Rotate(y, p), Rotate(x, p); //一字旋
 66             }
 67         }
 68         if(to == 0) rt=x;
 69     }
 70 
 71     int GetKth(int k){
 72         if(!rt || k > T[rt].size) return -1e9;  // 若要节点id,改为0
 73         int x=rt;
 74         while(x){
 75             PushDown(x);
 76             if(k >= T[T[x].son[0]].size+1 && k <= T[T[x].son[0]].size+T[x].cnt)
 77                 break;
 78             if(k > T[T[x].son[0]].size+T[x].cnt){
 79                 k-=T[T[x].son[0]].size+T[x].cnt;
 80                 x=T[x].son[1];
 81             }
 82             else
 83                 x=T[x].son[0];
 84         }
 85         return T[x].key;   // 若要节点id,改为x
 86     }
 87 
 88     int Find(int key){ //返回值为key的节点 若无返回0 若有将其转移到根处
 89         if(!rt) return 0;
 90         int x=rt;
 91         while(x){
 92             PushDown(x);
 93             if(T[x].key == key) break;
 94             x=T[x].son[key > T[x].key];
 95         }
 96         if(x) Splay(x, 0);
 97         return x;
 98     }
 99 
100     int Prev(){ //返回根节点的前驱 非重点
101         if(!rt || !T[rt].son[0]) return 0;
102         int x=T[rt].son[0];
103         while(T[x].son[1]){
104             PushDown(x);
105             x=T[x].son[1];
106         }
107         Splay(x, 0);
108         return x;
109     }
110 
111     int Succ(){ //返回根结点的后继 非重点
112         if(!rt || !T[rt].son[1]) return 0;
113         int x=T[rt].son[1];
114         while(T[x].son[0]){
115             PushDown(x);
116             x=T[x].son[0];
117         }
118         Splay(x, 0);
119         return x;
120     }
121 
122     void Insert(int key){ //插入key值
123         if(!rt)
124             rt=Newnode(key, 0);
125         else{
126             int x=rt, y=0;
127             while(x){
128                 PushDown(x);
129                 y=x;
130                 if(T[x].key == key){
131                     T[x].cnt++;
132                     T[x].size++;
133                     break;
134                 }
135                 T[x].size++;
136                 x=T[x].son[key > T[x].key];
137             }
138             if(!x)
139                 x=T[y].son[key > T[y].key]=Newnode(key, y);
140             Splay(x, 0);
141         }
142     }
143 
144     void Delete(int key){ //删除值为key的节点1个
145         int x=Find(key);
146         if(!x) return;
147         if(T[x].cnt>1){
148             T[x].cnt--;
149             PushUp(x);
150             return;
151         }
152         int y=T[x].son[0];
153         while(T[y].son[1])
154             y=T[y].son[1];
155         int z=T[x].son[1];
156         while(T[z].son[0])
157             z=T[z].son[0];
158         if(!y && !z){
159             rt=0;
160             return;
161         }
162         if(!y){
163             Splay(z, 0);
164             T[z].son[0]=0;
165             PushUp(z);
166             return;
167         }
168         if(!z){
169             Splay(y, 0);
170             T[y].son[1]=0;
171             PushUp(y);
172             return;
173         }
174         Splay(y, 0);
175         Splay(z, y);
176         T[z].son[0]=0;
177         PushUp(z);
178         PushUp(y);
179     }
180 
181     int GetRank(int key){ //获得值<=key的节点个数
182         if(!Find(key)){
183             Insert(key);
184             int tmp=T[T[rt].son[0]].size;
185             Delete(key);
186             return tmp;
187         }
188         else
189             return T[T[rt].son[0]].size+T[rt].cnt;
190     }
191 
192     void Delete(int l, int r){ //删除值在[l, r]中的所有节点 l!=r
193         if(!Find(l)) Insert(l);
194         int p=Prev();
195         if(!Find(r)) Insert(r);
196         int q=Succ();
197         if(!p && !q){
198             rt=0;
199             return;
200         }
201         if(!p){
202             T[rt].son[0]=0;
203             PushUp(rt);
204             return;
205         }
206         if(!q){
207             Splay(p, 0);
208             T[rt].son[1]=0;
209             PushUp(rt);
210             return;
211         }
212         Splay(p, q);
213         T[p].son[1]=0;
214         PushUp(p);
215         PushUp(q);
216     }
217 
218     int solve(int key){
219         if(!rt) return 0;
220         int x = rt, res = 2e9;
221         while(x){
222             res = min(res,abs(T[x].key-key));
223             x = T[x].son[key > T[x].key];
224         }
225         return res;
226     }
227 }spt;
228 
229 int n,x,ans;
230 
231 int main(){
232     cin >> n >> x;
233     spt.Insert(x);
234     ans = x;
235     for(int i = 1;i < n;++i){
236         scanf("%d",&x);
237         ans += spt.solve(x);
238         spt.Insert(x);
239     }
240     cout << ans << endl;
241 
242     return 0;
243 }
View Code

相关文章:

  • 2021-07-07
  • 2022-12-23
  • 2021-08-22
  • 2022-12-23
  • 2022-12-23
  • 2022-02-21
  • 2021-05-22
  • 2021-11-17
猜你喜欢
  • 2021-07-14
  • 2021-09-06
  • 2021-08-15
  • 2021-10-24
  • 2022-12-23
  • 2022-02-02
  • 2021-10-25
相关资源
相似解决方案