# BZOJ 1588 [HNOI2002]营业额统计

6
5
1
2
5
4
6

12

## HINT

这是一道裸题，BST。可以用TREAP，十分经典。不多说了。（LMY给我们一个小时做）

 1 /**************************************************************
2     Problem: 1588
3     User: Doggu
4     Language: C++
5     Result: Accepted
6     Time:192 ms
7     Memory:1448 kb
8 ****************************************************************/
9
10 #include <cstdio>
11 #include <cmath>
12 #include <algorithm>
13 const int N = 40000;
14 struct Node {
15     int key, rd;
16     Node *ch[2];
17     int cmp(int x) {
18         if(x==key) return -1;
19         return x>key;
20     }
21 }pool[N], *tail=pool, *root=pool, *null=pool;
22 int rand() {
23     static int seed=1000007;
24     return seed=(int)seed*48271LL%2147483647;
25 }
26 Node* newnode(int x) {
27     Node *nd = tail++;
28     nd->ch[0]=nd->ch[1]=null;
29     nd->key=x;nd->rd=rand();
30     return nd;
31 }
32 void rotate(Node *&nd,int d) {//d=0 right(1) to left(0) d=1 left(0) to right(1)
33     Node *k=nd->ch[d^1];nd->ch[d^1]=k->ch[d];k->ch[d]=nd;nd=k;
34 }
35 int temp;
36 void query(Node *nd,int x) {
37     if(nd==null) temp=std::min(temp,x);
38     int d=nd->cmp(x);
39     temp=std::min(temp,std::abs(nd->key-x));
40     if(d==-1||nd->ch[d]==null) return ;
41     query(nd->ch[d],x);
42 }
43 void insert(Node *&nd,int x) {
44     if(nd==null) nd=newnode(x);
45     else {
46         int d=nd->cmp(x);
47         if(d==-1) return ;
48         insert(nd->ch[d],x);
49         if(nd->ch[d]->rd>nd->rd) rotate(nd,d^1);
50     }
51 }
52 int main() {
53     null=newnode(0);
54     int n, x, ans = 0;
55     scanf("%d",&n);
56     for( int i = 1; i <= n; i++ ) {
57         scanf("%d",&x);
58         temp=0x3f3f3f3f;query(root,x);ans+=temp;
59         insert(root,x);
60     }
61     printf("%d",ans);
62     return 0;
63 }
64 
Treap(rand)

再说一下，用SBT也可以，长度相似，但会慢一些。

 1 /**************************************************************
2     Problem: 1588
3     User: Doggu
4     Language: C++
5     Result: Accepted
6     Time:208 ms
7     Memory:1448 kb
8 ****************************************************************/
9
10 #include <cstdio>
11 #include <cmath>
12 #include <algorithm>
13 const int N = 40000;
14 struct Node {
15     int key, size;
16     Node *ch[2];
17     int cmp(int x) {
18         if(x==key) return -1;
19         return x>key;
20     }
21     void update() {size = ch[0]->size + ch[1]->size + 1;}
22 }pool[N], *tail=pool, *root=pool, *null=pool;
23 Node* newnode(int x) {
24     Node *nd = tail++;
25     nd->key=x;nd->size=0;
26     nd->ch[0]=nd->ch[1]=null;
27     return nd;
28 }
29 void rotate(Node *&nd,int d) {
30     Node *k=nd->ch[d^1];nd->ch[d^1]=k->ch[d];k->ch[d]=nd;
31     nd->update();k->update();nd=k;
32 }
33 int temp;
34 void query(Node *nd,int x) {
35     if(nd==null) temp=std::min(temp,x);
36     int d=nd->cmp(x);
37     temp=std::min(temp,std::abs(nd->key-x));
38     if(d==-1||nd->ch[d]==null) return ;
39     query(nd->ch[d],x);
40 }
41 void insert(Node *&nd,int x) {
42     if(nd==null) nd=newnode(x);
43     else {
44         int d=nd->cmp(x);
45         if(d==-1) return ;
46         insert(nd->ch[d],x);
47         nd->update();
48         while(nd->ch[d]->size>nd->ch[d^1]->size) rotate(nd,d^1);
49     }
50 }
51 int main() {
52     null=newnode(0);
53     int n, x, ans = 0;
54     scanf("%d",&n);
55     for( int i = 1; i <= n; i++ ) {
56         scanf("%d",&x);
57         temp=0x3f3f3f3f;query(root,x);ans+=temp;
58         insert(root,x);
59     }
60     printf("%d",ans);
61     return 0;
62 }
63 
SBT(size)

posted @ 2017-05-26 09:28  Doggu  阅读(61)  评论(0编辑  收藏