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
| #include <bits/stdc++.h> using namespace std;
#define rep(i, j, k) for (int i = (j); i <= (k); i++) #define per(i, j, k) for (int i = (j); i >= (k); i--) #define pb push_back #define mp make_pair #define fi first #define se second typedef vector<int> vi; typedef pair<int, int> pi;
const int N = 4e5 + 5; int n, q, L; array<int, 3> box[N]; vector<pi> qry; void Input() { cin >> n >> q >> L; rep(i, 1, n) { cin >> box[i][0] >> box[i][1]; box[i][2] = i; } rep(i, 1, q) { int op; cin >> op; if (op == 1) { n++; cin >> box[n][0] >> box[n][1]; box[n][2] = n; } else { int x; cin >> x; qry.pb(mp(x, n)); } } } struct BIT { int s[N]; void add(int d, int nw) { while (d < N) { s[d] += nw; d += d & -d; } } int query(int d) { int res = 0; while (d) { res += s[d]; d -= d & -d; } return res; } } T0, T1; vi G[N]; int dfn[N], id[N], dfc, fa[N], dep[N], sz[N]; int bz[N][19]; vector<pi> bel; void dfs(int u, int le, int ri) { bz[u][0] = fa[u]; rep(i, 1, 18) { bz[u][i] = bz[bz[u][i - 1]][i - 1]; } dfc++, dfn[u] = dfc, id[dfc] = u; T0.add(dfn[u], le); T0.add(dfn[u] + 1, -le); T1.add(dfn[u], ri); T1.add(dfn[u] + 1, -ri);
int L = box[u][0]; for (int v : G[u]) { ri += box[v][0] - L; L = box[v][1] + 2; } ri += box[u][1] - L + 2; L = box[u][0]; int pre = box[u][0]; for (int v : G[u]) { if (v == fa[u]) { continue; } if (pre < box[v][0]) { bel.pb(mp(pre, u)); } pre = box[v][1] + 1;
ri -= box[v][0] - L; le += box[v][0] - L; L = box[v][1] + 2;
dep[v] = dep[u] + 1; dfs(v, le, ri); } if (pre <= box[u][1]) { bel.pb(mp(pre, u)); } sz[u] = dfc - dfn[u] + 1; } int Getbel(int x) { return prev(upper_bound(bel.begin(), bel.end(), mp(x + 1, -1)))->se; } void solve() { sort(box + 1, box + n + 1, [](array<int, 3> A, array<int, 3> B) { return A[0] != B[0] ? A[0] < B[0] : A[1] > B[1]; }); box[0] = {1, L, 0}; rep(i, 1, n) { fa[i] = i - 1; while (box[fa[i]][1] < box[i][0]) { fa[i] = fa[fa[i]]; } G[fa[i]].pb(i); } dfs(0, 0, 0); auto Add = [&](int id, int nw) { int l0 = dfn[fa[id]] + 1, r0 = dfn[fa[id]] + sz[fa[id]] - 1; int l1 = dfn[id], r1 = dfn[id] + sz[id] - 1; T1.add(l0, nw); T1.add(l1, -nw); T0.add(r1 + 1, nw); T0.add(r0 + 1, -nw); }; vi fp(n + 1); rep(i, 1, n) { Add(i, box[i][1] - box[i][0] + 2); fp[box[i][2]] = i; } vector<char> vis(n + 1, 0); vis[0] = 1; int pt = 0; for (pi x : qry) { while (pt < x.se) { pt++; int id = fp[pt]; vis[id] = 1; Add(id, -(box[id][1] - box[id][0] + 2)); } auto chk = [&](int u) -> int { if (!vis[u]) { return 1; } int le = T1.query(dfn[u]), ri = T0.query(dfn[u]); return x.fi + ri > box[u][1] || x.fi - le < box[u][0]; }; int u = Getbel(x.fi); per(i, __lg(dep[u]), 0) { if (chk(bz[u][i])) { u = bz[u][i]; } } cout << dep[u] - chk(u) << '\n'; } } signed main() { freopen("box.in", "r", stdin); freopen("box.out", "w", stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); Input(); solve(); return 0; }
|