[HNOI2002] 营业额统计

Splay

BZOJ 题面不完整......简直没法做。

每天的最小波动值只可能由之前数值小于它的最大值大于它的最小值更新而来。

对于每天的营业额用一个 Splay 查找其在树上的前驱与后继并计算答案,然后将其加入树中。

代码如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include<iostream>
#include<algorithm>
#include<cstdio>

using namespace std;

inline void set_file_IO(string);
inline void close_IO(void);
inline void work(void);

int main(void) {
    #ifndef ONLINE_JUDGE
        set_file_IO("tb_kp");
    #endif
    ios::sync_with_stdio(false);
    work();
    #ifndef ONLINE_JUDGE
        close_IO();
    #endif
    return 0;
}

template<class T>

class Splay {
public:
    struct Node {
        Node *e[3], **root;
        T key;
        int size, cnt;
        
        Node(Node **_root, Node *_p, T _key) {
            root = _root;
            e[2] = _p;
            key = _key;
            cnt = size = 1;
            e[0] = e[1] = NULL;
        }
        
        int relation(void) {
            return this == e[2]->e[0]? 0: 1;
        }
        
        void update(void) {
            size = cnt;
            if (e[0] != NULL) {
                size += e[0]->size;
            }
            if (e[1] != NULL) {
                size += e[1]->size;
            }
        }
        
        void rotate(void) {
            Node *old = e[2];
            const int r = relation();
            e[2] = old->e[2];
            if (old->e[2]) {
                old->e[2]->e[old->relation()] = this;
            }
            if (e[r^1]) {
                e[r^1]->e[2] = old;
            }
            old->e[r] = e[r^1];
            old->e[2] = this;
            e[r^1] = old;
            
            old->update();
            update();
            if (e[2] == NULL) {
                *root = this;
            }
        }
        
        void splay(Node* tgt = NULL) {
            while (e[2] != tgt) {
                if (e[2]->e[2] == tgt) {
                } else if (e[2]->relation() == relation()) {
                    e[2]->rotate();
                } else {
                    rotate();
                }
                rotate();
            }
        }
        
        Node* pred(void) {
            Node *v = e[0];
            while (v->e[1]) {
                v = v->e[1];
            }
            return v;
        }
        
        Node* succ(void) {
            Node *v = e[1];
            while (v->e[0]) {
                v = v->e[0];
            }
            return v;
        }
        
        int rank(void) {
            return e[0] == NULL? 0: e[0]->size;
        }
        
    } *root;
    
    void inOrder(Node* cur) {
        if (cur == NULL) {
            return ;
        }
        cout << "(" << " ";
        inOrder(cur->e[0]);
        for (int i=0; i<cur->cnt; ++i) {
            cout << cur->key << " ";
        }
        inOrder(cur->e[1]);
        cout << ")" << " ";
    }
    
public:
    
    Splay() {
        root = NULL;
    }
    
    Node* insert(T key) {
        Node **v = &root, *p = NULL;
        while (*v != NULL && (*v)->key != key) {
            p = *v;
            ++ (p->size);
            v = &p->e[key>=p->key];
        }
        if (*v != NULL) {
            ++ ((*v)->cnt);
            ++ ((*v)->size);
        } else {
            (*v) = new Node(&root, p, key);
        }
        (*v)->splay();
        return root;
    }
    
    Node* find(T key) {
        Node *v = root;
        while (v != NULL && v->key != key) {
            v = v->e[v->key < key];
        }
        if (v) {
            v->splay();
        }
        return v;
    }
    
    void erase(Node* v) {
        Node *pred = v->pred(), *succ = v->succ();
        pred->splay();
        succ->splay(pred);
        if (v->size > 1) {
            -- (v->size);
            -- (v->cnt);
        } else {
            delete succ->e[0];
            succ->e[0] = NULL;
        }
        -- (succ->size);
        -- (pred->size);
    }
    
    void erase(T key) {
        Node *v = find(key);
        if (v != NULL) {
            erase(v);
        }
    }
    
    T pred(T key) {
        Node *v = find(key);
        int res;
        if (v == NULL) {
            v = insert(key);
            res = v->pred()->key;
            erase(v);
        } else {
            res = v->pred()->key;
        }
        return res;
    }
    
    T succ(T key) {
        Node *v = find(key);
        int res;
        if (v == NULL) {
            v = insert(key);
            res = v->succ()->key;
            erase(v);
        } else {
            res = v->succ()->key;
        }
        return res;
    }

    int rank(T key) {
        Node *v = find(key);
        int res;
        if (v == NULL) {
            v = insert(key);
            res = v->rank();
            erase(v);
        } else {
            res = v->rank();
        }
        return res + 1;
    }
    
    T kth(int k) {
        -- k;
        Node *v = root;
        while (!(k >= v->rank() && k < v->rank() + v->cnt)) {
            if (k < v->rank()) {
                v = v->e[0];
            } else {
                k -= v->rank() + v->cnt;
                v = v->e[1];
            }
        }
        v->splay();
        return v->key;
    }
    
    char inOrder(void) {
        inOrder(root);
        return '\0';
    }
    
    T maximum(void) {
        Node *v = root;
        while (v->e[1] != NULL) {
            v = v->e[1];
        }
        return v->key;
    }
    
    T minimum(void) {
        Node *v = root;
        while (v->e[0] != NULL) {
            v = v->e[0];
        }
        return v->key;
    }
};

Splay<int> tree;

inline void work(void) {
    tree.insert(-0x3f3f3f3f);
    tree.insert(+0x3f3f3f3f);
    int n;
    cin >> n;
    for (int i=1; i<=n; ++i) {
        int opt, arg;
        cin >> opt;
        if (opt < 7) {
            cin >> arg;
        }
        if (opt == 1) {
            tree.insert(arg);
        } else if (opt == 2) {
            tree.erase(arg);
        } else if (opt == 3) {
            cout << tree.rank(arg)-1 << endl;
        } else if (opt == 4) {
            cout << tree.kth(arg+1) << endl;
        } else if (opt == 5) {
            cout << tree.pred(arg) << endl;
        } else if (opt == 6) {
            cout << tree.succ(arg) << endl;
        } else if (opt == 7) {
            cout << tree.succ(-0x3f3f3f3f) << endl;
        } else {
            cout << tree.pred(+0x3f3f3f3f) << endl;
        }
        //tree.inOrder();
        //cout << endl;
    }
}

inline void set_file_IO(string name) {
    freopen((name + ".in" ).c_str(), "r", stdin );
    freopen((name + ".out").c_str(), "w", stdout);
}

inline void close_IO(void) {
    fclose(stdin);
    fclose(stdout);
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注