中学的时候年轻气盛,应该拿主席树把这道题艹过去了。正好复习离线二维数点,再做了一遍。
我们把询问差分安排到x轴上,然后y轴用树状数组统计即可。注意到此题要离散化,但是询问中的x可能不在原序列中。不过这不要紧,记得二分完减一就好。
const int N = 5e5 + 5, S = 1e7 + 5;
int n, m, t, ans[N], x[N];
pair <int, int> a[N];
struct Query {
int p, q, x, y;
Query(int p = 0, int q = 0, int x = 0, int y = 0) : p(p), q(q), x(x), y(y) {}
};
int c[S];
inline void update(int pos, int val) {
for (; pos < N; pos += lowbit(pos)) c[pos] += val;
}
inline int query(int pos) {
int ret = 0;
for (; pos; pos -= lowbit(pos)) ret += c[pos];
return ret;
}
vector <Query> v[N];
signed main(void) {
read(n), read(m);
for (int i = 1; i <= n; i++) read(a[i].first), read(a[i].second), x[i] = a[i].first;
sort(a + 1, a + n + 1); sort(x + 1, x + n + 1); t = unique(x + 1, x + n + 1) - (x + 1);
for (int i = 1; i <= m; i++) {
int l, r, _x, _y;
read(l), read(_x), read(r), read(_y);
int _l = lower_bound(x + 1, x + t + 1, l) - x - 1;
int _r = upper_bound(x + 1, x + t + 1, r) - x - 1;
v[_l].push_back(Query(i, -1, _x, _y));
v[_r].push_back(Query(i, 1, _x, _y));
}
for (int i = 1, j = 1; i <= t; i++) {
while (a[j].first == x[i] && j <= n) update(a[j].second + 1, 1), j++;
for (auto u : v[i]) ans[u.p] -= u.q * query(u.x), ans[u.p] += u.q * query(u.y + 1);
}
for (int i = 1; i <= m; i++) writeln(ans[i]);
//fwrite(pf, 1, o1 - pf, stdout);
return 0;
}
标签:园丁,val,int,lowbit,pos,ret,P2163,SHOI2007
From: https://www.cnblogs.com/EternalEpic/p/18419316