bzoj1935: [Shoi2007]Tree 园丁的烦恼
没什么好说的吧QWq
cdq分治
#include<cstdio>
#include<algorithm>
//using namespace std;
inline int read() {
int x = 0,f = 1;
char c = getchar() ;
while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();}
while(c <= '9' && c >= '0')x = x * 10 + c -'0',c = getchar();
return x * f;
}
const int maxn = 500007;
const int maxB = 10000003;
struct Query {
int type,x,y,w,aid;
Query (int Type = 0,int X = 0,int Y = 0,int W = 0,int Aid = 0) : type(Type),x (X),y (Y),w (W),aid (Aid){};
bool operator < (const Query & A) const {
return x == A.x ? type < A.type : x < A.x;
}
} query[(maxn) * 5];
int maxy = -1;
namespace BIT {
#define lowbit(x) (x & (-x))
int sum[maxB << 1];
void add(int idx,int val) {
for(;idx <= maxy;idx += lowbit(idx)) sum[idx] += val;
}
int query(int idx) {
int ret = 0;
for(;idx;idx -= lowbit(idx)) ret += sum[idx];
return ret;
}
void clear(int idx) {
for( ;idx <= maxy;idx += lowbit(idx) )
if(sum[idx]) sum[idx] = 0;else break;
}
}
int ans[maxn];
Query tmp[maxn * 5 + 7];
void cdq(int l,int r) {
if(r - l <= 1) return ;
int mid = l + r >> 1; cdq(l,mid);cdq(mid,r);
int p = l,q = mid,cnt = l;
int sum = 0 ;
while(p < mid && q < r) {
if(query[p] < query[q]) {
if(query[p].type == 0) BIT::add(query[p].y,1);
tmp[cnt ++] = query[p ++];
} else {
if(query[q].type == 1) ans[query[q].aid] += query[q].w * BIT::query(query[q].y);
tmp[cnt ++] = query[q ++];
}
}
while(p < mid) tmp[cnt ++] = query[p ++];
while(q < r) {
if(query[q].type == 1) ans[query[q].aid] += query[q].w * BIT::query(query[q].y);
tmp[cnt ++] = query[q ++];
}
for(int i = l;i < r;++ i) {
BIT:: clear(tmp[i].y);
query[i] = tmp[i];
}
}
int n,m,qidx = 0,aidx;
int main() {
n = read(),m = read();
for(int x,y,i = 1;i <= n;++ i) {
x = read() + 1,y = read() + 1;
query[qidx ++] = Query(0,x,y,0,0);maxy = std::max(maxy,y);
}
for(int x1,y1,x2,y2,i = 1;i <= m;++ i) {
x1 = read() + 1,y1 = read() + 1,x2 = read() + 1,y2 = read() + 1;
query[qidx ++] = Query(1,x1 - 1,y1 - 1,1,aidx);
query[qidx ++] = Query(1,x1 - 1,y2,- 1,aidx);
query[qidx ++] = Query(1,x2,y1 - 1,-1,aidx);
query[qidx ++] = Query(1,x2,y2,1,aidx); aidx ++; \
maxy = std::max(maxy,std::max(y1,y2));
}
cdq(0,qidx);
for(int i = 0;i < aidx;++ i) printf("%d\n",ans[i]);
return 0;
}