1208: [HNOI2004]宠物收养所 支持删除的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
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
const int N=1e5+100;
const int mod=1000000;
int cur;
long long ans;
int times,root;
int x1,x2;
struct node
{
    int father,lson,rson,date;
}tree[N];
int abs(int x)
{
    if(x<0) return -x;
    else return x;
}
int findMin()
{
    int y=tree[root].lson;
    if(y==-1) return -1;
    while(tree[y].rson!=-1) y=tree[y].rson;
    return y;
}
int findMax()
{
    int y=tree[root].rson;
    if(y==-1) return y;
    while(tree[y].lson!=-1) y=tree[y].lson;
    return y;
}
void rightrotate(int x)
{
    int y=tree[x].father; int z=tree[y].father;
    tree[y].lson=tree[x].rson;
    if(tree[x].rson!=-1)
    {
        tree[tree[x].rson].father=y;
    }
    tree[x].father=z;
    if(z!=-1)
    {
        if(tree[z].lson==y) tree[z].lson=x;
        else tree[z].rson=x;
    }
    tree[x].rson=y;
    tree[y].father=x;
}
  
void leftrotate(int x)
{
    int y=tree[x].father; int z=tree[y].father;
    tree[y].rson=tree[x].lson;
    if(tree[x].lson!=-1)
    {
        tree[tree[x].lson].father=y;
    }
    tree[x].father=z;
    if(z!=-1)
    {
        if(tree[z].lson==y) tree[z].lson=x;
        else tree[z].rson=x;
    }
    tree[x].lson=y;  tree[y].father=x;
}
void splay(int x)
{
    while(tree[x].father!=-1)
    {
        int y=tree[x].father;  int z=tree[y].father;
        if(z==-1)
        {
            if(tree[y].lson==x) rightrotate(x);  else leftrotate(x);
        }
        else
        {
            if(tree[z].lson==y&&tree[y].lson==x) {rightrotate(y);rightrotate(x);}
            else if(tree[z].lson==y&&tree[y].rson==x) {leftrotate(x);rightrotate(x);}
            else if(tree[z].rson==y&&tree[y].rson==x){leftrotate(y);leftrotate(x);}
            else {rightrotate(x); leftrotate(x);}
        }
    }
    root=x;
}
void BIS_insert(int root,int date)
{
    if(tree[root].date>date)
    {
        if(tree[root].lson==-1)
        {
            tree[root].lson=times;
            tree[times].date=date; tree[times].lson=-1; tree[times].rson=-1;tree[times].father=root;
            return;
        }
        else
        {
            BIS_insert(tree[root].lson,date);
        }
    }
    else
    {
        if(tree[root].rson==-1)
        {
            tree[root].rson=times;
            tree[times].date=date; tree[times].lson=-1; tree[times].rson=-1; tree[times].father=root;
            return;
        }
        else BIS_insert(tree[root].rson,date);
    }
}
void insert(int date)
{
    times++;
    BIS_insert(root,date);
    splay(times);
}
/*void del(int x)
{
    splay(x);
    if(tree[x].lson==-1&&tree[x].rson==-1)
    {
        cur=0;
        return;
    }
    int temp=findMin();
    if(tree[x].lson==-1)
    {
        root=tree[x].rson;
        tree[root].father=-1;
    }
    else
    {
        if(tree[tree[temp].father].lson==temp) tree[tree[temp].father].lson=-1;
        else tree[tree[temp].father].rson=-1;
        tree[temp].father=-1;
        if(temp!=tree[x].lson)
        {tree[temp].lson=tree[root].lson; if(tree[root].lson!=-1) tree[tree[root].lson].father=temp;}
        tree[temp].rson=tree[root].rson;  if(tree[root].rson!=-1) tree[tree[root].rson].father=temp;
        root=temp;
    }
}*/
void del(int x)
{
    splay(x);
    if(tree[x].lson==-1&&tree[x].rson==-1)
    {
        cur=0;
        return;
    }
    else
    if (tree[x].lson==-1)
    {
        root=tree[x].rson;
        tree[root].father=-1;
    }
    else
    {
        int y=tree[x].lson;
        while (tree[y].rson!=-1) y=tree[y].rson;
        tree[tree[x].lson].father=-1;
        splay(y);
       // root=y;
        tree[y].father=-1;
        tree[y].rson=tree[x].rson;
        tree[tree[x].rson].father=y;
    }
}
void suc(int x,int y)
{
    if(x==-1) return;
    if(tree[x].date>=y) {x2=x; suc(tree[x].lson,y);}
    else suc(tree[x].rson,y);
}
void pre(int x,int y)
{
    if(x==-1) return;
    if(tree[x].date<=y) {x1=x; pre(tree[x].rson,y);}
    else pre(tree[x].lson,y);
}
void insertANDdel(int date)
{
    x1=-1; x2=-1;
    suc(root,date);
    pre(root,date);
    if(x1==-1)
    {
        ans=(ans+tree[x2].date-date)%mod;
        del(x2);
    }
    else if(x2==-1)
    {
        ans=(ans+date-tree[x1].date)%mod;
        del(x1);
    }
    else
    {
        if(date-tree[x1].date<=tree[x2].date-date)
        {
            ans=(ans+date-tree[x1].date)%mod;
            del(x1);
        }
        else
        {
            ans=(ans+tree[x2].date-date)%mod;
            del(x2);
        }
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    cur=0;
    for(int i=1;i<=n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        x++;
        if(cur==0)
        {
            cur=x;
            times++;
            root=times;
            tree[root].date=y;
            tree[root].father=-1;
            tree[root].lson=-1;
            tree[root].rson=-1;
        }
        else
        if(cur==x)
        {
            insert(y);
        }
        else
        {
            insertANDdel(y);
        }
    }
    printf("%lld\n",ans%mod);
}

  

posted @   Heilce  阅读(148)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示