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
| #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) #define fin(name) freopen(name,"r",stdin) #define fout(name) freopen(name,"w",stdout) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); typedef pair<int, int> pii; typedef long long LL; const double PI = acos(-1.0); const int N = 80010; const int mod = 1000000; struct node { int ch[2], v, f; void init() { ch[0] = ch[1] = 0; } } T[N]; int rt, sz, pp, sp;
void init() { rt = sz = 0; } inline int getson(int x) { return T[T[x].f].ch[1] == x; } void rotate(int x, int &k) { int f = T[x].f, ff = T[f].f, l = getson(x), r = l ^ 1; if (f == k) k = x; else T[ff].ch[getson(f)] = x; T[x].f = ff; T[f].ch[l] = T[x].ch[r]; if (T[x].ch[r]) T[T[x].ch[r]].f = f; T[x].ch[r] = f; T[f].f = x; } void splay(int x, int &k) { while (x != k) { int f = T[x].f; if (f != k) { if (getson(f)^getson(x)) rotate(x, k); else rotate(f, k); } rotate(x, k); } } void ins(int &k, int f, int v) { if (!k) { T[k = ++sz].init(); T[k].f = f; T[k].v = v; splay(k, rt); } else if (v < T[k].v) ins(T[k].ch[0], k, v); else ins(T[k].ch[1], k, v); } void getpre(int k, int v) { if (!k) return ; if (v >= T[k].v) { pp = k; getpre(T[k].ch[1], v); } else getpre(T[k].ch[0], v); } void getsuc(int k, int v) { if (!k) return ; if (v <= T[k].v) { sp = k; getsuc(T[k].ch[0], v); } else getsuc(T[k].ch[1], v); } void del(int x) { splay(x, rt); if (T[x].ch[0] * T[x].ch[1] == 0) rt = T[x].ch[0] + T[x].ch[1]; else { int k = T[x].ch[1]; while (T[k].ch[0]) k = T[k].ch[0]; T[k].ch[0] = T[x].ch[0]; T[T[x].ch[0]].f = k; rt = T[x].ch[1]; } T[rt].f = 0; } int main(void) { int n, ops, i; while (~scanf("%d", &n)) { init(); int kind = -1, ans = 0; for (i = 0; i < n; ++i) { int v; scanf("%d%d", &ops, &v); if (!rt) { kind = ops; ins(rt, 0, v); } else if (kind == ops) ins(rt, 0, v); else { pp = sp = 0; getpre(rt, v); getsuc(rt, v); if (pp && sp) { int pv = v - T[pp].v, sv = T[sp].v - v; if (pv <= sv) { ans = (ans + pv) % mod; del(pp); } else { ans = (ans + sv) % mod; del(sp); } } else if (pp) { ans = (ans + v - T[pp].v) % mod; del(pp); } else if (sp) { ans = (ans + T[sp].v - v) % mod; del(sp); } } } printf("%d\n", ans); } return 0; }
|