HNOI2002 营业额统计 splay
2015-04-03 22:04
267 查看
解题思路:找边上最近的值。
解题思路:splay
解题代码:
View Code
解题思路:splay
解题代码:
// File Name: poj3468.sz.cpp // Author: darkdream // Created Time: 2014年11月07日 星期五 08时45分48秒 #include<limits.h> #include<vector> #include<list> #include<map> #include<set> #include<deque> #include<stack> #include<bitset> #include<algorithm> #include<functional> #include<numeric> #include<utility> #include<sstream> #include<iostream> #include<iomanip> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<ctime> #define LL long long #define maxn 1000010 using namespace std; #define valtree ch[ch[root][1]][0] struct SplayTree{ int ch[maxn][2]; int pre[maxn]; int root ; int top1; inline void Rotate(int x ,int f){ int y = pre[x]; ch[y][!f] = ch[x][f]; pre[ch[x][f]] = y ; pre[x] = pre[y]; if(pre[x]) ch[pre[y]][ch[pre[y]][1] == y] = x; ch[x][f] = y ; pre[y] = x; } void Splay(int x, int goal){ 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); } } } if(goal == 0 ) root = x; } int ABS(int x) { if(x < 0) return -x; return x ; } int Rotateto(int k ,int goal) { int r = root ; int mi = INT_MAX; while(1) { mi = min(mi,ABS(val[r]-k)); if(!ch[r][val[r] < k]) break; r = ch[r][val[r] < k ]; } int &tt = ch[r][val[r] < k]; newnode(tt,k); pre[tt] = r; Splay(tt,0); return mi; } /*void erase(int x ){ int father = pre[x]; int head = 0 , tail = 0 ; for() }*/ /*void debug(){printf("%d\n",root);Treaval(root);} void Treaval(int x) { if(x) { Treaval(ch[x][0]); printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,val = %2d sum = %3lld\n",x,ch[x][0],ch[x][1],pre[x],sz[x],val[x],sum[x]); Treaval(ch[x][1]); } }*/ void newnode(int &x,int c) { x = ++ top1; ch[x][0] = ch[x][1] = pre[x] = 0 ; val[x] = c; } void init(int v) { ch[0][0] = ch[0][1] = pre[0] =0 ; root = top1 = 0 ; newnode(root,v); //这里注意 root 是从1 开始的 } int val[maxn]; }sp; int main(){ int n ; scanf("%d",&n); if(n == 0 ) { printf("%d\n",0); return 0; } int v; scanf("%d",&v); sp.init(v); int ans = v; for(int i = 1;i < n;i ++) { if(scanf("%d",&v) == EOF) v = 0 ; ans += sp.Rotateto(v,0); } printf("%d\n",ans); return 0; }
View Code
相关文章推荐
- HYSBZ 1588: [HNOI2002]营业额统计(splay)
- 【Splay】BZOJ 1588 [HNOI2002]营业额统计
- BZOJ 1588: [HNOI2002]营业额统计 Splay版(模板题)
- BZOJ 1588 [HNOI2002]营业额统计(Splay)
- [bzoj1588][HNOI2002]营业额统计——splay
- BZOJ 1588: [HNOI2002]营业额统计 双向链表 / splay / treap
- HNOI_2002 营业额统计(Splay)
- Bzoj 1588: [HNOI2002]营业额统计(splay)
- HNOI 2002 营业额统计(Splay入门)
- [BZOJ1588]HNOI2002营业额统计|splay
- HNOI_2002 营业额统计(Splay)
- HNOI 2002 营业额统计(Splay树)
- 1588: [HNOI2002]营业额统计(Splay树入门)
- 1588: [HNOI2002]营业额统计 splay 入门
- bzoj1588: [HNOI2002]营业额统计 splay瞎写
- bzoj1588: [HNOI2002]营业额统计 平衡树 Splay
- BZOJ 1588 [HNOI 2002] 营业额统计 Splay
- 【SPLAY】 [HNOI2002] 营业额统计 模板
- bzoj 1588: [HNOI2002]营业额统计 splay
- 【BZOJ1588】【HNOI2002】营业额统计 splay