Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 9,927 Bytes
f7ba5f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <algorithm>
#include <iostream>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

using int64 = long long;

const int MAX_COORD = 300000;

struct Rect {
  int x1, y1, x2, y2, id;
  int mappedX1, mappedY1, mappedX2, mappedY2;
  bool ends_alive;

  Rect() {}
  Rect(int _x1, int _y1, int _x2, int _y2, int _id)
      : x1(_x1), y1(_y1), x2(_x2), y2(_y2), id(_id) {}

  bool intersects(const Rect &o) const {
    return x2 >= o.x1 && x1 <= o.x2 && y1 <= o.y2 && y2 >= o.y1;
  }

  void merge_in(const Rect &o) {
    x1 = min(x1, o.x1);
    mappedX1 = min(mappedX1, o.mappedX1);
    y1 = min(y1, o.y1);
    mappedY1 = min(mappedY1, o.mappedY1);
    x2 = max(x2, o.x2);
    mappedX2 = max(mappedX2, o.mappedX2);
    y2 = max(y2, o.y2);
    mappedY2 = max(mappedY2, o.mappedY2);
  }

  bool operator <(const Rect &o) {
    return id < o.id;
  }
};

class InnerST {
  int lo, hi, delta;
  unique_ptr<InnerST> lchild, rchild;

  void init_children() {
    if (!lchild) {
      int mid = lo + (hi - lo) / 2;
      lchild = make_unique<InnerST>(lo, mid);
      rchild = make_unique<InnerST>(mid + 1, hi);
    }
  }

 public:
  InnerST(int l, int h)
      : lo(l), hi(h), delta(0), lchild(nullptr), rchild(nullptr) {}

  void add(int l, int h, int d) {
    if (l > hi || h < lo) {
      return;
    }
    if (l <= lo && h >= hi) {
      delta += d;
      return;
    }
    init_children();
    lchild->add(l, h, d);
    rchild->add(l, h, d);
  }

  int query(int at) {
    if (lo == hi || !lchild) {
      return delta;
    }
    init_children();
    return delta + (at <= lchild->hi ? lchild : rchild)->query(at);
  }
};

class OuterST {
  int lo, hi, inner_lo, inner_hi;
  unique_ptr<OuterST> lchild, rchild;
  InnerST inner;

 public:
  OuterST(int l, int h, int inner_l, int inner_h)
      : lo(l),
        hi(h),
        inner_lo(inner_l),
        inner_hi(inner_h),
        inner(inner_l, inner_h) {
    if (lo != hi) {
      int mid = lo + (hi - lo) / 2;
      lchild = make_unique<OuterST>(lo, mid, inner_lo, inner_hi);
      rchild = make_unique<OuterST>(mid + 1, hi, inner_lo, inner_hi);
    }
  }

  // Adds a line to the inner dimension spanning inner_lo to inner_hi on
  // outer_idx
  void add_inner(int64 outer_idx, int inner_lo, int inner_hi) {
    inner.add(inner_lo, inner_hi, 1);
    if (lo != hi) {
      if (outer_idx <= lchild->hi) {
        lchild->add_inner(outer_idx, inner_lo, inner_hi);
      } else {
        rchild->add_inner(outer_idx, inner_lo, inner_hi);
      }
    }
  }

  // Queries all y's that have something here.
  vector<int> query_inner(int outer_lo, int outer_hi, int inner_idx) {
    if (outer_lo > hi || outer_hi < lo || inner.query(inner_idx) == 0)
      return {};
    if (lo == hi) {
      return {lo};
    }
    auto lres = lchild->query_inner(outer_lo, outer_hi, inner_idx);
    auto rres = rchild->query_inner(outer_lo, outer_hi, inner_idx);
    vector<int> sum(lres.size() + rres.size());
    for (int i = 0; i < (int)sum.size(); i++) {
      sum[i] = i < (int)lres.size() ? lres[i] : rres[i - lres.size()];
    }
    return sum;
  }

  void remove_inner(int outer_idx, int inner_lo, int inner_hi) {
    inner.add(inner_lo, inner_hi, -1);
    if (lo != hi) {
      if (outer_idx <= lchild->hi) {
        lchild->remove_inner(outer_idx, inner_lo, inner_hi);
      } else {
        rchild->remove_inner(outer_idx, inner_lo, inner_hi);
      }
    }
  }
};

class RectHolder {
  struct RectMapping {
    int value;
    Rect *r;
    bool v1;

    bool operator<(const RectMapping &r) const { return value < r.value; }
  };

  int sz;
  vector<int64> mapsXMajor, mapsYMajor;
  unordered_set<Rect*> aliveRects;
  OuterST xMajorST, yMajorST;
  vector<Rect*> usingXMapping, usingYMapping;
  vector<int> firstOfXMajor, lastOfXMajor;
  vector<int> firstOfYMajor, lastOfYMajor;

 public:
  RectHolder(vector<Rect> &rects)
      : sz(rects.size()),
        xMajorST(0, 2 * sz, 0, 2 * sz),
        yMajorST(0, 2 * sz, 0, 2 * sz),
        usingXMapping(2 * sz),
        usingYMapping(2 * sz),
        firstOfXMajor(2 * sz, (int)1e9),
        lastOfXMajor(2 * sz, -1),
        firstOfYMajor(2 * sz, (int)1e9),
        lastOfYMajor(2 * sz, -1) {
    vector<RectMapping> xMappings(2 * sz), yMappings(2 * sz);
    for (int i = 0; i < sz; i++) {
      xMappings[i * 2] = (RectMapping){rects[i].x1, &rects[i], true};
      xMappings[i * 2 + 1] = (RectMapping){rects[i].x2, &rects[i], false};
      yMappings[i * 2] = (RectMapping){rects[i].y1, &rects[i], true};
      yMappings[i * 2 + 1] = (RectMapping){rects[i].y2, &rects[i], false};
    }
    sort(xMappings.begin(), xMappings.end());
    sort(yMappings.begin(), yMappings.end());
    for (int i = 0; i < (int)xMappings.size(); i++) {
      usingXMapping[i] = xMappings[i].r;
      usingYMapping[i] = yMappings[i].r;
      if (xMappings[i].v1) {
        xMappings[i].r->mappedX1 = i;
        int orig = xMappings[i].r->x1;
        firstOfXMajor[orig] = min(firstOfXMajor[orig], i);
        lastOfXMajor[orig] = max(lastOfXMajor[orig], i);
      } else {
        xMappings[i].r->mappedX2 = i;
        int orig = xMappings[i].r->x2;
        firstOfXMajor[orig] = min(firstOfXMajor[orig], i);
        lastOfXMajor[orig] = max(lastOfXMajor[orig], i);
      }
      if (yMappings[i].v1) {
        yMappings[i].r->mappedY1 = i;
        int orig = yMappings[i].r->y1;
        firstOfYMajor[orig] = min(firstOfYMajor[orig], i);
        lastOfYMajor[orig] = max(lastOfYMajor[orig], i);
      } else {
        yMappings[i].r->mappedY2 = i;
        int orig = yMappings[i].r->y2;
        firstOfYMajor[orig] = min(firstOfYMajor[orig], i);
        lastOfYMajor[orig] = max(lastOfYMajor[orig], i);
      }
    }
  }

  void add_rect(Rect *r) {
    aliveRects.insert(r);
    usingXMapping[r->mappedX1] = r;
    usingXMapping[r->mappedX2] = r;
    usingYMapping[r->mappedY1] = r;
    usingYMapping[r->mappedY2] = r;
    xMajorST.add_inner(r->mappedX1, r->y1, r->y2);
    yMajorST.add_inner(r->mappedY1, r->x1, r->x2);
    xMajorST.add_inner(r->mappedX2, r->y1, r->y2);
    yMajorST.add_inner(r->mappedY2, r->x1, r->x2);
  }

  void remove_rect(Rect *r) {
    aliveRects.erase(r);
    xMajorST.remove_inner(r->mappedX1, r->y1, r->y2);
    yMajorST.remove_inner(r->mappedY1, r->x1, r->x2);
    xMajorST.remove_inner(r->mappedX2, r->y1, r->y2);
    yMajorST.remove_inner(r->mappedY2, r->x1, r->x2);
  }

  unordered_set<Rect*> get_alive_rect_intersecting(Rect *r) {
    unordered_set<Rect*> res;
    auto xsToTake1 =
        xMajorST.query_inner(firstOfXMajor[r->x1], lastOfXMajor[r->x2], r->y1);
    auto xsToTake2 =
        xMajorST.query_inner(firstOfXMajor[r->x1], lastOfXMajor[r->x2], r->y2);
    auto ysToTake1 =
        yMajorST.query_inner(firstOfYMajor[r->y1], lastOfYMajor[r->y2], r->x1);
    auto ysToTake2 =
        yMajorST.query_inner(firstOfYMajor[r->y1], lastOfYMajor[r->y2], r->x2);
    for (int x : xsToTake1) res.insert(usingXMapping[x]);
    for (int x : xsToTake2) res.insert(usingXMapping[x]);
    for (int y : ysToTake1) res.insert(usingYMapping[y]);
    for (int y : ysToTake2) res.insert(usingYMapping[y]);
    return res;
  }

  unordered_set<Rect*> get_all_alive_rects() {
    return aliveRects;
  }
};

struct Event {
  int x, y1, y2;
  bool adding;
  Rect *r;

  bool operator<(const Event &e) const {
    if (x != e.x) {
      return x < e.x;
    }
    return (y2 - y1) < (e.y2 - e.y1);
  }
};

int64 solve() {
  int N;
  vector<Rect> rects;
  vector<int> xvals, yvals;
  unordered_map<int, int> x_map, y_map, x_rev, y_rev;
  // Input.
  cin >> N;
  for (int i = 0, x, y; i < N; i++) {
    cin >> x >> y;
    rects.push_back(Rect(x - 1, y - 1, x + 1, y + 1, i));
    xvals.push_back(x - 1);
    xvals.push_back(x + 1);
    yvals.push_back(y - 1);
    yvals.push_back(y + 1);
  }
  // Compress rectangles.
  sort(xvals.begin(), xvals.end());
  xvals.resize(distance(xvals.begin(), unique(xvals.begin(), xvals.end())));
  sort(yvals.begin(), yvals.end());
  yvals.resize(distance(yvals.begin(), unique(yvals.begin(), yvals.end())));
  int i = 0;
  for (int x : xvals) {
    x_rev[i] = x;
    x_map[x] = i++;
  }
  i = 0;
  for (int y : yvals) {
    y_rev[i] = y;
    y_map[y] = i++;
  }
  for (Rect &r : rects) {
    r.x1 = x_map[r.x1];
    r.y1 = y_map[r.y1];
    r.x2 = x_map[r.x2];
    r.y2 = y_map[r.y2];
  }
  // Add to rect holder.
  RectHolder holder(rects);
  for (Rect &to_add : rects) {
    while (true) {
      auto kill_now = holder.get_alive_rect_intersecting(&to_add);
      if (kill_now.empty()) {
        break;
      }
      for (Rect *to_kill : kill_now) {
        holder.remove_rect(to_kill);
        to_add.merge_in(*to_kill);
      }
    }
    holder.add_rect(&to_add);
  }
  // Line sweep to remove any rectangles that are inside another rectangle sweep
  // left to right. On an X, process bigger then smaller.
  auto alive = holder.get_all_alive_rects();
  InnerST st(0, MAX_COORD);
  vector<Event> events;
  for (Rect *r : alive) {
    r->ends_alive = true;
    events.push_back((Event){r->x1, r->y1, r->y2, true, r});
    events.push_back((Event){r->x2, r->y1, r->y2, false, r});
  }
  sort(events.begin(), events.end());
  for (Event &e : events) {
    if (e.adding) {
      if (st.query(e.y1) == 0) {
        st.add(e.y1, e.y2, 1);
      } else {
        e.r->ends_alive = false;
      }
    } else {
      if (e.r->ends_alive) {
        st.add(e.y1, e.y2, -1);
      }
    }
  }
  // Uncompress and calculate areas.
  int64 ans = 0;
  for (const Rect &r : rects) {
    if (r.ends_alive) {
      int x1 = x_rev[r.x1], x2 = x_rev[r.x2];
      int y1 = y_rev[r.y1], y2 = y_rev[r.y2];
      ans += (int64)(x2 - x1) * (y2 - y1);
    }
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  for (int t = 1; t <= T; t++) {
    cout << "Case #" << t << ": " << solve() << endl;
  }
  return 0;
}