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
| #include <bits/stdc++.h> #define DB double #define IL inline namespace Fast { const int L = (1 << 20) + 5; char buf[L], out[L], *iS, *iT; int l = 0; #define gh() (iT == iS ? iT = (iS = buf) + fread(buf, 1, L, stdin), (iT == iS ? EOF : *iS++) : *iS++) template<class T> IL void read(T &x) { x = 0; char ch = gh(), t = 0; while (ch < '0' || ch > '9') t |= ch == '-', ch = gh(); while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = gh(); if (t) x = -x; } IL void flus() { fwrite(out, 1, l, stdout); l = 0; } IL void putc(char x) { out[l++] = x; if (l == L - 5) flus(); } template<class T> IL void write(T x) { if (x < 0) putc('-'), x = -x; if (x > 9) write(x / 10); out[l++] = x % 10 + 48; if (l == L - 5) flus(); } } using Fast::flus; using Fast::putc; using Fast::read; using Fast::write; using namespace std; const int M = 2e5 + 5, D = 2; int n; struct Point { int x[D], w; } ; namespace KDT { const DB a = 0.725; int rt, top, rub[M], cur, tot; Point p[M]; struct Node { int mn[D], mx[D], sum, lc, rc, si, k; Point p; } tr[M]; #define mn(x) tr[(x)].mn #define mx(x) tr[(x)].mx #define sum(x) tr[(x)].sum #define lc(x) tr[(x)].lc #define rc(x) tr[(x)].rc #define si(x) tr[(x)].si #define k(x) tr[(x)].k #define p(x) tr[(x)].p IL int newnode() { if (top) return rub[top--]; return ++cur; } IL void up(int u) { int ls = lc(u), rs = rc(u); for (int i = 0; i < D; ++i) { mn(u)[i] = mx(u)[i] = p(u).x[i]; if (ls) mn(u)[i] = min(mn(u)[i], mn(ls)[i]), mx(u)[i] = max(mx(u)[i], mx(ls)[i]); if (rs) mn(u)[i] = min(mn(u)[i], mn(rs)[i]), mx(u)[i] = max(mx(u)[i], mx(rs)[i]); } sum(u) = sum(ls) + sum(rs) + p(u).w, si(u) = si(ls) + si(rs) + 1; } IL int build(int l, int r, int k) { if (l > r) return 0; int mid = l + r >> 1, u = newnode(); nth_element(p + l, p + mid, p + r + 1, [&](Point a, Point b){ return a.x[k] < b.x[k]; }); k(u) = k, p(u) = p[mid], lc(u) = build(l, mid - 1, k ^ 1), rc(u) = build(mid + 1, r, k ^ 1); return up(u), u; } IL void get_p(int u) { if (!u) return ; get_p(lc(u)), p[++tot] = p(u), rub[++top] = u, get_p(rc(u)); } IL void check(int &u) { if (si(u) * a < max(si(lc(u)), si(rc(u)))) tot = 0, get_p(u), u = build(1, tot, k(u)); } IL void ins(int &u, Point x) { if (!u) { u = newnode(); lc(u) = rc(u) = k(u) = 0, p(u) = x; return up(u); } if (x.x[k(u)] <= p(u).x[k(u)]) ins(lc(u), x); else ins(rc(u), x); up(u), check(u); } IL bool in(int mx[D], int mn[D], int l[D], int r[D]) { for (int i = 0; i < D; ++i) if (r[i] < mx[i] || l[i] > mn[i]) return false; return true; } IL bool out(int mx[D], int mn[D], int l[D], int r[D]) { for (int i = 0; i < D; ++i) if (l[i] > mx[i] || r[i] < mn[i]) return true; return false; } IL int ask(int u, int l[D], int r[D]) { if (!u || out(mx(u), mn(u), l, r)) return 0; if (in(mx(u), mn(u), l, r)) return sum(u); return (in(p(u).x, p(u).x, l, r) ? p(u).w : 0) + ask(lc(u), l, r) + ask(rc(u), l, r); } } int main() { read(n); for (int op, x[D], y[D], last = 0; read(op), op != 3; ) { read(x[0]), read(x[1]), read(y[0]), x[0] ^= last, x[1] ^= last, y[0] ^= last; if (op == 1) KDT::ins(KDT::rt, {x[0], x[1], y[0]}); else read(y[1]), y[1] ^= last, write(last = KDT::ask(KDT::rt, x, y)), putc('\n'); } return flus(), 0; }
|