博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
营业额统计 ( Splay树 )
阅读量:4879 次
发布时间:2019-06-11

本文共 5707 字,大约阅读时间需要 19 分钟。

From http://www.lydsy.com/JudgeOnline/problem.php?id=1588

1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 7 const static int N = 40001; 8 int v[N],l[N],r[N],p[N]; 9 int cnt, root; 10 void init(){ cnt = 0; root = 0; v[0]=l[0]=r[0]=p[0]=0;} 11 int alloc_node(int Val){ 12 v[++cnt] = Val; 13 l[cnt] = r[cnt] = p[cnt] = 0; 14 return cnt; 15 } 16 void rot_l(int x){ 17 int y = p[x]; 18 r[y] = l[x]; p[l[x]] = y; 19 p[x] = p[y]; 20 if ( !p[y] ) root = x; 21 else if ( y == l[p[y]] ) l[p[y]] = x; 22 else r[p[y]] = x; 23 l[x] = y; p[y] = x; 24 } 25 void rot_r(int x){ 26 int y = p[x]; 27 l[y] = r[x]; p[r[x]] = y; 28 p[x] = p[y]; 29 if ( !p[y] ) root = x; 30 else if ( y == l[p[y]] ) l[p[y]] = x; 31 else r[p[y]] = x; 32 r[x] = y; p[y] = x; 33 } 34 void maintain(int i){ 35 while ( p[i] ){ 36 if ( l[p[i]] == i ){ 37 if ( !p[p[i]] ) rot_r(i); // zig 38 else if ( l[p[p[i]]] == p[i] ){
//zig-zig 39 rot_r(p[i]); rot_r(i); 40 }else{
//zig-zag 41 rot_r(i); rot_l(i); 42 } 43 }else{ 44 if ( !p[p[i]] ) rot_l(i); 45 else if ( r[p[p[i]]] == p[i] ){ 46 rot_l(p[i]); rot_l(i); 47 }else{ 48 rot_l(i); rot_r(i); 49 } 50 } 51 } 52 } 53 int insert(int Val){ 54 int k = alloc_node(Val); 55 int i = root, j = p[root]; 56 while ( i ) { 57 j = i; 58 if ( v[i] == Val ){ 59 maintain(i); 60 return 0; 61 } 62 if ( v[i] < Val ) i = r[i]; 63 else i = l[i]; 64 } 65 66 p[k] = j; 67 if ( j == 0 ) root = k; 68 else if ( v[j] < Val ) r[j] = k; 69 else l[j] = k; 70 maintain(k); 71 return 1; 72 } 73 int prev(){ 74 int y = l[root]; 75 while ( r[y] ) y = r[y]; 76 return y; 77 } 78 int succ(){ 79 int y = r[root]; 80 while ( l[y] ) y = l[y]; 81 return y; 82 } 83 84 #define ABS(x) (((x)<0)?(-(x)):(x)) 85 int main() 86 { 87 //const char path[] = "D:\\Project\\AlgorithmExam\\test.txt"; 88 //freopen(path, "r+", stdin); 89 90 int n; 91 while ( scanf("%d", &n) != EOF ){ 92 init(); 93 int m; 94 long long sum = 0; 95 for ( int i = 0; i < n; ++i ){ 96 if ( scanf("%d", &m) == EOF ) m = 0; 97 if ( !insert(m) ) continue; 98 int x = prev(); 99 int y = succ();100 if ( 0 == x && y == 0 ) sum += m;101 else if ( 0 == x ) sum += ABS(v[y]-m);102 else if ( 0 == y ) sum += ABS(v[x]-m);103 else sum += min(ABS(v[y]-m), ABS(v[x]-m));104 }105 printf("%lld\n", sum);106 }107 return 0;108 }

 

1 /************************************************************** 2     Problem: 1588 3     User: leezy 4     Language: C++ 5     Result: Accepted 6     Time:156 ms 7     Memory:2260 kb 8 ****************************************************************/ 9  10 #include 
11 #include
12 #include
13 #include
14 #define N 4001515 #define M 100000016 #define INF 0x6fffffff17 #define MAX_LEVEL 3218 #define NIL (0)19 #define min(a,b) ((a)<(b))?(a):(b)20 typedef struct tagSkipListNode{21 int key;22 tagSkipListNode* next, *prev, *up, *down;23 } Node;24 Node Nil;25 Node* List[MAX_LEVEL], *Upd[MAX_LEVEL];26 int top;27 void Init(int x)28 {29 top = -1;30 Nil.key = INF;31 srand(x);32 }33 Node* NewNode(int key){34 Node* p = (Node*)malloc(sizeof(Node));35 p->key = key;36 p->prev = p->next = &Nil;37 p->up = p->down = &Nil;38 return p;39 }40 int Find_LessEqual(int key){41 if ( top < 0 ) return 0;42 int k = top;43 Node* p = List[k];44 while ( k >= 0 ){45 for (Node* q = p->next; q->key <= key; p = q, q = p->next );46 Upd[k--] = p;47 p = p->down;48 }49 return (Upd[0]->key == key)?(1):(0);50 }51 int Ins(int key){52 if ( Find_LessEqual(key) ) return 0;53 int k = 0;54 for (float r = 0; r < 0.5 && k < MAX_LEVEL; r = ((float)rand())/RAND_MAX) { ++k; }55 for (; top + 1 < k; ){56 Node* p = NewNode(-INF);57 if ( -1 == top ) List[++top] = p;58 else {59 List[top]->up = p; p->down = List[top];60 List[++top] = p;61 }62 Upd[top] = p;63 }64 Node* top2 = 0;65 for (int i = 0; i < k; ++i ){66 Node* p = NewNode(key);67 Node* q = Upd[i]->next;68 p->next = q; q->prev = p;69 Upd[i]->next = p; p->prev = Upd[i];70 if ( !top2 ) top2 = p;71 else {72 p->down = top2; top2->up = p;73 top2 = p;74 }75 }76 77 Node* x = Upd[0], *y = x->next, *z = y->next;78 if ( x->key == -INF && z->key == INF ) return y->key;79 if ( x->key == -INF && z->key != INF ) return abs(y->key-z->key);80 if ( x->key != -INF && z->key == INF ) return abs(x->key-y->key);81 return min(abs(x->key-y->key), abs(y->key-z->key));82 }83 int main()84 {85 int n, sum = 0;86 while ( scanf("%d", &n) != EOF ){87 //char op[5];88 Init(n+sum);89 sum = 0;90 for ( int i = 0; i < n; ++i ){91 int d = 0;92 if ( scanf("%d", &d) == EOF ) d = 0;93 sum += Ins(d);94 //Dump();95 }96 printf("%d\n", sum);97 }98 return 0;99 }

 

转载于:https://www.cnblogs.com/leezy/p/3418557.html

你可能感兴趣的文章
COJ 1003 WZJ的数据结构(三)ST表
查看>>
sbrk and coreleft
查看>>
树型DP
查看>>
怎么在ubuntu上使用pidgin登陆QQ
查看>>
思维的惰性
查看>>
2018-2019-2 网络对抗技术 20165115 Exp3 免杀原理与实践
查看>>
【Android】学习记录<1> -- 初识ffmpeg
查看>>
定位页面元素的位置
查看>>
关于IAsyncResult接口的CompletedSynchronously属性
查看>>
Python:一篇文章掌握Numpy的基本用法
查看>>
序列化与ArrayList 的elementData的修饰关键字transient
查看>>
学习进度17
查看>>
编译原理——算符优先分析文法(附源代码)
查看>>
jboss的启动过程
查看>>
渲染部分
查看>>
力扣——所有可能的路径
查看>>
关于VS项目平台的x86,x64,Any CPU以及Debug和Release的区别
查看>>
解密module_init幕后的故事
查看>>
9个移动网站优化的最佳实践
查看>>
李昌镐:苍老的青春(转载) 韩国围棋职业棋手
查看>>