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
| #include <bits/stdc++.h> using namespace std; using i64 = long long; #define endl '\n'
class FHQTreap { public: class Node { public: Node *left, *right; int sz; uint32_t pri; i64 val, lazy; Node(i64 val, uint32_t pri) : left(nullptr), right(nullptr), sz(1), pri(pri), val(val), lazy(0) {} };
private: vector<unique_ptr<Node>> pool; mt19937 rng;
public: explicit FHQTreap(int reserveNodes = 0) : rng(static_cast<uint32_t>( chrono::steady_clock::now().time_since_epoch().count())) { pool.reserve(reserveNodes + 5); }
int size(Node* root) const { return root ? root->sz : 0; }
Node* make(i64 value) { pool.emplace_back(make_unique<Node>(value, rng())); return pool.back().get(); }
void pushup(Node* root) { if (!root) return; root->sz = size(root->left) + size(root->right) + 1; }
void apply(Node* root, i64 value) { if (!root) return; root->val += value; root->lazy += value; }
void pushdown(Node* root) { if (!root || root->lazy == 0) return; i64 tag = root->lazy; apply(root->left, tag); apply(root->right, tag); root->lazy = 0; }
pair<Node*, Node*> split(Node* root, i64 key) { if (!root) return {nullptr, nullptr}; pushdown(root); if (root->val < key) { auto [a, b] = split(root->right, key); root->right = a; pushup(root); return {root, b}; } else { auto [a, b] = split(root->left, key); root->left = b; pushup(root); return {a, root}; } }
Node* merge(Node* a, Node* b) { if (!a || !b) return a ? a : b; if (a->pri < b->pri) { pushdown(a); a->right = merge(a->right, b); pushup(a); return a; } else { pushdown(b); b->left = merge(a, b->left); pushup(b); return b; } }
Node* insert(Node* root, i64 value) { auto [a, b] = split(root, value); Node* mid = make(value); return merge(merge(a, mid), b); }
Node* erase(Node* root, i64 value) { if (!root) return nullptr; pushdown(root); if (root->val == value) return merge(root->left, root->right); if (value < root->val) root->left = erase(root->left, value); else root->right = erase(root->right, value); pushup(root); return root; }
Node* update(Node* root, i64 l, i64 r, i64 value) { auto [a, b] = split(root, l); auto [c, d] = split(b, r); apply(c, value); return merge(merge(a, c), d); }
Node* popmin(Node* root) { if (!root) return nullptr; pushdown(root); if (!root->left) return root->right; root->left = popmin(root->left); pushup(root); return root; }
Node* popmax(Node* root) { if (!root) return nullptr; pushdown(root); if (!root->right) return root->left; root->right = popmax(root->right); pushup(root);
return root; }
i64 front(Node* root) { pushdown(root); while (root->left) { root = root->left; pushdown(root); } return root->val; }
i64 back(Node* root) { pushdown(root); while (root->right) { root = root->right; pushdown(root); } return root->val; } };
void solve() { int n, l, r; cin >> n;
FHQTreap tr(n);
using Node = FHQTreap::Node;
Node* f = nullptr;
for (int i = 0; i < n; i++) { cin >> l >> r;
auto [A, B] = tr.split(f, l); auto [C, D] = tr.split(B, r);
C = tr.update(C, l, r, 1);
D = tr.popmin(D);
C = tr.insert(C, l);
f = tr.merge(tr.merge(A, C), D); }
cout << tr.size(f) << endl; }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }
|