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
| #include <bits/stdc++.h>
#define x first #define y second #define y1 Y1 #define y2 Y2 #define mp make_pair #define pb push_back
using namespace std;
typedef long long LL; typedef pair<int, int> pii;
template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
inline void proc_status() { ifstream t ("/proc/self/status"); cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) <<endl; }
inline int read () { int sum = 0, fl = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0'; return sum * fl; }
const int Maxn = 1e5 + 100;
int N, M, B[Maxn], root, ch[Maxn][2], fa[Maxn]; pii A[Maxn];
set <int> S; set <int> :: iterator it;
namespace SEG { #define ls Tree[root << 1] #define rs Tree[root << 1 | 1] #define lson root << 1, l, mid #define rson root << 1 | 1, mid + 1, r struct tree { int sum, tag; }Tree[Maxn << 2];
inline void push_up (int root) { Tree[root].sum = ls.sum + rs.sum; }
inline void push_down (int root, int l, int r, int mid) { if (!Tree[root].tag) return ; ls.tag += Tree[root].tag; rs.tag += Tree[root].tag; ls.sum += Tree[root].tag * (mid - l + 1); rs.sum += Tree[root].tag * (r - mid); Tree[root].tag = 0; }
inline void update (int root, int l, int r, int x, int y, int z) { if (x <= l && r <= y) { Tree[root].sum += z * (r - l + 1); Tree[root].tag += z; return ; } int mid = l + r >> 1; push_down (root, l, r, mid); if (x <= mid) update (lson, x, y, z); if (y > mid) update (rson, x, y, z); push_up (root); }
inline void modify (int root, int l, int r, int x, int z) { if (l == r) { Tree[root].sum = z; Tree[root].tag = 0; return ; } int mid = l + r >> 1; push_down (root, l, r, mid); if (x <= mid) modify (lson, x, z); else modify (rson, x, z); push_up (root); }
inline int query (int root, int l, int r, int x) { if (l == r) return Tree[root].sum; int mid = l + r >> 1; push_down (root, l, r, mid); if (x <= mid) return query (lson, x); else return query (rson, x); } }
inline void link (int x, int f, int dir) { ch[f][dir] = x; fa[x] = f;}
inline int Insert (int x) { it = S.insert(x).x; if (!root) { root = x; SEG :: modify (1, 1, N, x, 1); return 1; } if (it != S.begin()) { if (!ch[*(--it)][1]) link (x, *it, 1); ++it; } if (!fa[x]) link (x, *(++it), 0); int ans = SEG :: query (1, 1, N, fa[x]) + 1; SEG :: modify (1, 1, N, x, ans); return ans; }
inline int Query_min () { int x = *S.begin(), ans = SEG :: query (1, 1, N, x); if (x == root) return 1; if (fa[x] != x + 1) SEG :: update (1, 1, N, x + 1, fa[x] - 1, -1); SEG :: update (1, 1, N, 1, N, 1); link (ch[x][1], fa[x], 0), link (root, x, 1); root = x; SEG :: modify (1, 1, N, x, 1); return ans; }
inline int Query_max () { int x = *S.rbegin(), ans = SEG :: query (1, 1, N, x); if (x == root) return 1; if (fa[x] != x - 1) SEG :: update (1, 1, N, fa[x] + 1, x - 1, -1); SEG :: update (1, 1, N, 1, N, 1); link (ch[x][0], fa[x], 1), link (root, x, 0); root = x; SEG :: modify (1, 1, N, x, 1); return ans; }
inline void Del_min () { SEG :: update (1, 1, N, 1, N, -1); S.erase(root); root = ch[root][1], fa[root] = 0; }
inline void Del_max () { SEG :: update (1, 1, N, 1, N, -1); S.erase(root); root = ch[root][0], fa[root] = 0; }
inline void Solve () { for (int i = 1; i <= M; ++i) { if (A[i].x == 1) printf("%d\n", Insert (A[i].y)); else if (A[i].x == 2) printf("%d\n", Query_min()); else if (A[i].x == 3) printf("%d\n", Query_max()); else if (A[i].x == 4) printf("%d\n", Query_min()), Del_min(); else if (A[i].x == 5) printf("%d\n", Query_max()), Del_max(); } }
inline void Input () { M = read(); for (int i = 1; i <= M; ++i) { A[i].x = read(); if (A[i].x == 1) A[i].y = B[++N] = read(); } sort(B + 1, B + N + 1), N = unique (B + 1, B + N + 1) - B - 1; for (int i = 1; i <= M; ++i) if (A[i].x == 1) A[i].y = lower_bound (B + 1, B + N + 1, A[i].y) - B; }
int main() { #ifdef hk_cnyali freopen("A.in", "r", stdin); freopen("A.out", "w", stdout); #endif Input(); Solve(); return 0; }
|