1 /*Author:WNJXYK*/
2 #include<cstdio>
3 using namespace std;
4
5 const int M=1000000;
6 const int Inf=1e9;
7
8 const int Maxn=1000000;
9 struct SBT{
10 int left;
11 int right;
12 int size;
13 int key;
14 };
15 SBT tree[Maxn+10];
16 int root,cnt;
17
18 inline void rotate_l(int &x){
19 int y=tree[x].right;
20 tree[x].right=tree[y].left;
21 tree[y].left=x;
22 tree[y].size=tree[x].size;
23 tree[x].size=1+tree[tree[x].left].size+tree[tree[x].right].size;
24 x=y;
25 }
26
27 inline void rotate_r(int &x){
28 int y=tree[x].left;
29 tree[x].left=tree[y].right;
30 tree[y].right=x;
31 tree[y].size=tree[x].size;
32 tree[x].size=tree[tree[x].left].size+1+tree[tree[x].right].size;
33 x=y;
34 }
35
36 void maintain(int &x,bool flag){
37 //printf("MainTain %d\n",x);
38 if (flag==false){
39 if (tree[tree[tree[x].left].left].size>tree[tree[x].right].size){
40 rotate_r(x);
41 }
42 else if (tree[tree[tree[x].left].right].size>tree[tree[x].right].size){
43 rotate_l(tree[x].left);
44 rotate_r(x);
45 }else return ;
46 }else{
47 if (tree[tree[tree[x].right].right].size>tree[tree[x].left].size){
48 rotate_l(x);
49 }else if (tree[tree[tree[x].right].left].size>tree[tree[x].left].size){
50 rotate_r(tree[x].right);
51 rotate_l(x);
52 }else return ;
53 }
54 maintain(tree[x].left,false);
55 maintain(tree[x].right,true);
56 maintain(x,true);
57 maintain(x,false);
58 }
59
60 void insert(int &x,int sp){
61 //printf("%d\n",x);
62 if (!x){
63 x=++cnt;
64 tree[x].left=tree[x].right=0;
65 tree[x].size=1;
66 tree[x].key=sp;
67 }else{
68 tree[x].size++;
69 if (sp<tree[x].key){
70 insert(tree[x].left,sp);
71 }else{
72 insert(tree[x].right,sp);
73 }
74 maintain(x,sp>=tree[x].key);
75 }
76 }
77
78 int del(int &x,int sp){
79 tree[x].size--;
80 if (sp==tree[x].key || ( sp<tree[x].key && tree[x].left==0) || (sp>tree[x].key && tree[x].right==0)){
81 int y=tree[x].key;
82 if (tree[x].left==0 ||tree[x].right==0){
83 x=tree[x].left+tree[x].right;
84 }else{
85 tree[x].key=del(tree[x].left,tree[x].key+1);
86 }
87 return y;
88 }else{
89 if (sp<tree[x].key){
90 return del(tree[x].left,sp);
91 }else{
92 return del(tree[x].right,sp);
93 }
94 }
95 }
96
97 int pred(int &x,int y,int sp){
98 if (x==0) return y;
99 if (tree[x].key<sp){
100 return pred(tree[x].right,x,sp);
101 }
102 return pred(tree[x].left,y,sp);
103 }
104
105 int succ(int &x,int y,int sp){
106 if (x==0) return y;
107 if (tree[x].key>sp){
108 return succ(tree[x].left,x,sp);
109 }
110 return succ(tree[x].right,y,sp);
111 }
112
113 inline void init(){
114 root=cnt=0;
115 }
116
117 inline int abs(int x){
118 if (x<0) return -x;
119 return x;
120 }
121
122 int main(){
123 init();
124 int n;scanf("%d",&n);
125 int p=0;
126 int Ans=0;
127 for (;n--;){
128 int a,b;
129 scanf("%d%d",&a,&b);
130 if (a==p || tree[root].size==0){
131 p=a;
132 insert(root,b);
133 }else{
134 int f1=pred(root,0,b);
135 int f2=succ(root,0,b);
136 if (f1!=0) f1=tree[f1].key; else f1=Inf;
137 if (f2!=0) f2=tree[f2].key; else f2=Inf;
138 if (abs(f1-b)<=abs(f2-b)){
139 del(root,f1);
140 Ans+=abs(f1-b)%M;
141 }else{
142 del(root,f2);
143 Ans+=abs(f2-b)%M;
144 }
145 Ans%=M;
146 }
147 }
148 printf("%d\n",Ans);
149 return 0;
150 }