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
|
#include <cstdio> #include <vector> #include <algorithm> #include <map>
using namespace std;
const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; }; const auto getr = [](int l, int r, int id) { return id + ((l + r) / 2 - l + 1) * 2; };
class constructor { public: int fa[200005], rk[200005]; int getfa(int x) { while(x != fa[x]) x = fa[x] = fa[fa[x]]; return x; } void merge(int x, int y) { if((x = getfa(x)) == (y = getfa(y))) return; if(rk[x] < rk[y]) { fa[x] = y; } else if(rk[y] < rk[x]) { fa[y] = x; } else { fa[x] = y; rk[y]++; } } } ds;
class segtree { public: class node { public: int vadd, maxn; } nodes[1200005]; void pushup(int l, int r, int id) { nodes[id].maxn = max(nodes[id + 1].maxn, nodes[getr(l, r, id)].maxn);} void pushdown(int l, int r, int id) { nodes[id + 1].vadd += nodes[id].vadd; nodes[id + 1].maxn += nodes[id].vadd; nodes[getr(l, r, id)].vadd += nodes[id].vadd; nodes[getr(l, r, id)].maxn += nodes[id].vadd; nodes[id].vadd = 0; } void build(int n) { U([&](auto &&self, int l, int r, int id) -> void { nodes[id] = {0, 0}; if(l == r) return; self(self, l, (l + r) / 2, id + 1); self(self, (l + r) / 2 + 1, r, getr(l, r, id)); })(1, n, 1); } void vadd(int n, int L, int R, int val) { U([&](auto &&self, int l, int r, int vl, int vr, int id) -> void { if(l == vl && r == vr) { nodes[id].vadd += val; nodes[id].maxn += val; return; } pushdown(l, r, id); if(vl <= (l + r) / 2) self(self, l, (l + r) / 2, vl, min(vr, (l + r) / 2), id + 1); if(vr > (l + r) / 2) self(self, (l + r) / 2 + 1, r, max(vl, (l + r) / 2 + 1), vr, getr(l, r, id)); pushup(l, r, id); })(1, n, L, R, 1); } bool qa0(int n, int L, int R) { return !U([&](auto &&self, int l, int r, int vl, int vr, int id) -> bool { if(l == vl && r == vr) return nodes[id].maxn > 0; pushdown(l, r, id); if(vl <= (l + r) / 2 && self(self, l, (l + r) / 2, vl, min(vr, (l + r) / 2), id + 1)) return true; if(vr > (l + r) / 2 && self(self, (l + r) / 2 + 1, r, max(vl, (l + r) / 2 + 1), vr, getr(l, r, id))) return true; return false; })(1, n, L, R, 1); } } seg;
class pt { public: int x, y; } pts[200005]; class rect { public: int x1, y1, x2, y2; } rects[200005]; class vcx { public: int p, l, r, v; } vcxs[400005]; class edge { public: int u, v, w; }; vector<int> vxr[600005], vyr[600005]; int krx[600005], kry[600005]; long long ar[200005], sr[200005];
int main() { int n, m, q; scanf("%d%d%d", &n, &m, &q); vector<edge> egs; map<int, int> qx, qy; for(int i=1;i<=n;i++) { scanf("%d%d", &pts[i].x, &pts[i].y); qx[pts[i].x]; qy[pts[i].y]; } for(int i=1;i<=m;i++) { scanf("%d%d%d%d", &rects[i].x1, &rects[i].y1, &rects[i].x2, &rects[i].y2); qx[rects[i].x1]; qx[rects[i].x2]; qy[rects[i].y1]; qy[rects[i].y2]; } int curx = 0, cury = 0; for(auto &[x, y] : qx) krx[y = ++curx] = x; for(auto &[x, y] : qy) kry[y = ++cury] = x; for(int i=1;i<=n;i++) { pts[i].x = qx[pts[i].x]; pts[i].y = qy[pts[i].y]; vxr[pts[i].x].push_back(i); vyr[pts[i].y].push_back(i); } for(int i=1;i<=m;i++) { rects[i].x1 = qx[rects[i].x1]; rects[i].x2 = qx[rects[i].x2]; rects[i].y1 = qy[rects[i].y1]; rects[i].y2 = qy[rects[i].y2]; } for(int i=1;i<=m;i++) { vcxs[2 * i - 1] = {rects[i].x1, rects[i].y1, rects[i].y2, 1}; vcxs[2 * i] = {rects[i].x2 + 1, rects[i].y1, rects[i].y2, -1}; } sort(vcxs + 1, vcxs + 2 * m + 1, [](const auto &x, const auto &y) { return x.p < y.p; }); seg.build(cury); int curp = 0; for(int i=1;i<=curx;i++) { while(curp < 2 * m && vcxs[curp + 1].p <= i) { curp++; seg.vadd(cury, vcxs[curp].l, vcxs[curp].r, vcxs[curp].v); } if(vxr[i].size() >= 2) { sort(vxr[i].begin(), vxr[i].end(), [](int x, int y) { return pts[x].y < pts[y].y; }); for(int j=0;j+1<vxr[i].size();j++) { if(seg.qa0(cury, pts[vxr[i][j]].y, pts[vxr[i][j+1]].y)) egs.push_back({vxr[i][j], vxr[i][j+1], kry[pts[vxr[i][j+1]].y] - kry[pts[vxr[i][j]].y]}); } } } for(int i=1;i<=m;i++) { vcxs[2 * i - 1] = {rects[i].y1, rects[i].x1, rects[i].x2, 1}; vcxs[2 * i] = {rects[i].y2 + 1, rects[i].x1, rects[i].x2, -1}; } sort(vcxs + 1, vcxs + 2 * m + 1, [](const auto &x, const auto &y) { return x.p < y.p; }); seg.build(curx); curp = 0; for(int i=1;i<=cury;i++) { while(curp < 2 * m && vcxs[curp + 1].p <= i) { curp++; seg.vadd(curx, vcxs[curp].l, vcxs[curp].r, vcxs[curp].v); } if(vyr[i].size() >= 2) { sort(vyr[i].begin(), vyr[i].end(), [](int x, int y) { return pts[x].x < pts[y].x; }); for(int j=0;j+1<vyr[i].size();j++) { if(seg.qa0(curx, pts[vyr[i][j]].x, pts[vyr[i][j+1]].x)) egs.push_back({vyr[i][j], vyr[i][j+1], krx[pts[vyr[i][j+1]].x] - krx[pts[vyr[i][j]].x]}); } } } sort(egs.begin(), egs.end(), [](const auto &x, const auto &y) { return x.w < y.w; }); int vcnt = n; int cure = 0; for(int i=1;i<=n;i++) ds.fa[i] = i; for(const auto &[u, v, w] : egs) { if(ds.getfa(u) != ds.getfa(v)) { ds.merge(u, v); ar[++cure] = w; vcnt--; } } reverse(ar + 1, ar + cure + 1); for(int i=1;i<=cure;i++) sr[i] = sr[i-1] + ar[i]; while(q--) { long long b, h; scanf("%lld%lld", &b, &h); if(h < vcnt) { printf("-1\n"); continue; } h -= vcnt; int vr = min<int>(lower_bound(ar + 1, ar + cure + 1, b, [](long long x, long long y) { return x > y; }) - ar - 1, h); printf("%lld\n", sr[cure] - sr[vr] + b * vr + vcnt * b); } return 0; }
|