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
| const int maxn = 200000 + 10; const int NIL = 0; int n, d, m;
// == Point definition == class Point { public: int x, y; } PO[maxn]; // == Point finsihed ==
// == Node definition == int ID[maxn]; /* used for mapping ID */
int K = 2, cd;
class KDTree { public: int xy[2], xymin[2], xymax[2]; int cld[2]; int sum, fa, num;
bool operator< (const KDTree& rhs) const { return xy[cd] < rhs.xy[cd]; } inline void update(); inline void _init(int i); } T[maxn];
inline void KDTree::_init(int i) { ID[fa] = i; _for(j, 0, K) xymax[j] = xymin[j] = xy[j]; num = sum = 0; cld[0] = cld[1] = NIL; }
inline void KDTree::update() { _for(i, 0, K) if(cld[i]) _for(j, 0, K) { xymin[j] = min(xymin[j], T[cld[i]].xymin[j]); xymax[j] = max(xymax[j], T[cld[i]].xymax[j]); } }
int build(int l, int r, int dep, int _fa) { if(l > r) return NIL; int mid = (l + r) >> 1;
cd = dep % K; nth_element(T + l, T + mid, T + r + 1); KDTree& cur = T[mid]; //assert(cur.fa != 0);
cur._init(mid); assert(ID[cur.fa] == mid); cur.fa = _fa;
if(l < mid) cur.cld[0] = build(l, mid - 1, dep + 1, mid); if(r > mid) cur.cld[1] = build(mid + 1, r, dep + 1, mid); cur.update();
return mid; }
int root; // == Node definition finished ==
// == KDTree query == ll ANS[maxn];
inline bool inRange(int x, int l, int r) { return l <= x && x <= r; } int query(int u, int x1, int x2, int y1, int y2) { int res = 0;
const KDTree& cur = T[u]; if(cur.xymin[0] > x2 || cur.xymax[0] < x1 || cur.xymin[1] > y2 || cur.xymax[1] < y1 || cur.sum == 0) { return 0; } if(x1 <= cur.xymin[0] && cur.xymax[0] <= x2 && y1 <= cur.xymin[1] && cur.xymax[1] <= y2) { return cur.sum; }
if(inRange(cur.xy[0], x1, x2) && inRange(cur.xy[1], y1, y2)) res += cur.num; _for(i, 0, K) if(cur.cld[i]) { res += query(cur.cld[i], x1, x2, y1, y2); }
return res; } // == KDTree query finished ==
// == Mo algo == int belong[maxn]; int sz, t;
class Qry { public: int l, r, id; } qry[maxn];
void block() { sz = sqrt(n); t = n / sz; _rep(i, 1, t) _rep(k, (i - 1) * sz + 1, i * sz) belong[k] = i; if(t * sz < n) { t++; _rep(k, (t - 1) * sz + 1, n) belong[k] = t; } }
bool cmp(const Qry& a, const Qry& b) { if(belong[a.l] ^ belong[b.l]) return belong[a.l] < belong[b.l]; return a.r < b.r; }
void add(int pos, ll& ans) { ans += query(root, PO[pos].x - d, PO[pos].x + d, PO[pos].y - d, PO[pos].y + d); int ti = ID[pos]; T[ti].num = 1; while (ti) T[ti].sum++, ti = T[ti].fa; }
void del(int pos, ll& ans) { int ti = ID[pos]; T[ti].num = 0; while (ti) T[ti].sum--, ti = T[ti].fa; ans -= query(root, PO[pos].x - d, PO[pos].x + d, PO[pos].y - d, PO[pos].y + d); } // == Mo algo finished ==
void init() { Set(ID, 0); Set(belong, 0); }
int main() { freopen("input.txt", "r", stdin); for(int t = 1, x, y; scanf("%d%d%d", &n, &d, &m) == 3; t++) { init(); printf("Case %d:\n", t);
// input point data _rep(i, 1, n) { KDTree& cur = T[i]; scanf("%d%d", &x, &y); cur.xy[0] = PO[i].x = x + y; cur.xy[1] = PO[i].y = x - y; cur.fa = i; }
// build tree root = build(1, n, 0, 0);
// block for Mo algorithm // remember sort query then Mo algorithm _rep(i, 1, m) { scanf("%d%d", &qry[i].l, &qry[i].r); qry[i].id = i; } block(); sort(qry + 1, qry + 1 + m, cmp);
// use Mo algo and KDTree query int l = 1, r = 0; ll ans = 0; _rep(i, 1, m) { int ql = qry[i].l, qr = qry[i].r; while (r < qr) add(++r, ans); while (r > qr) del(r--, ans); while (l < ql) del(l++, ans); while (l > ql) add(--l, ans);
ANS[qry[i].id] = ans; }
_rep(i, 1, m) printf("%lld\n", ANS[i]); } }
|