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
| #include<bits/stdc++.h> using namespace std; #define maxn 110000 struct Point{ int x,y; }point[maxn]; struct Rect{ int p1,p2,q1,q2,id; }rect[maxn]; int n,m,ans[maxn]; int len[maxn],cnt; bool cmp(const Point &a,const Point &b){ return a.x < b.x; } bool cmp1(const Rect &a,const Rect &b){ return a.p1<b.p1; } bool cmp2(const Rect &a,const Rect &b){ return a.q1<b.q1; } struct Node{ int sum; }tree[maxn<<1]; void pushup(int rt){ tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; } void add(int rt,int l,int r,int x){ if(l==r){ if(l==x)tree[rt].sum+=1; return ; } int mid=(l+r)>>1; if(x<=mid)add(rt<<1,l,mid,x); if(x>mid)add(rt<<1|1,mid+1,r,x); pushup(rt); } int query(int rt,int l,int r,int x,int y){ if(x<=l&&r<=y){ return tree[rt].sum; } int mid=(l+r)>>1; int ans=0; if(x<=mid)ans+=query(rt<<1,l,mid,x,y); if(y>mid)ans+=query(rt<<1|1,mid+1,r,x,y); pushup(rt); return ans; } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%d%d",&point[i].x,&point[i].y); len[++cnt]=point[i].x,len[++cnt]=point[i].y; } for(int i=1;i<=m;i++){ int a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d); rect[i]={a,b,c,d,i}; len[++cnt]=a;len[++cnt]=b;len[++cnt]=c;len[++cnt]=d; } sort(len+1,len+cnt+1); int tot=unique(len+1,len+cnt+1)-len-1; for(int i=1;i<=n;i++){ point[i].x=lower_bound(len+1,len+cnt+1,point[i].x)-len; point[i].y=lower_bound(len+1,len+cnt+1,point[i].y)-len; } for(int i=1;i<=m;i++){ rect[i].p1=lower_bound(len+1,len+cnt+1,rect[i].p1)-len; rect[i].q1=lower_bound(len+1,len+cnt+1,rect[i].q1)-len; rect[i].p2=lower_bound(len+1,len+cnt+1,rect[i].p2)-len; rect[i].q1=lower_bound(len+1,len+cnt+1,rect[i].q2)-len; } sort(point+1,point+n+1,cmp); sort(rect+1,rect+m+1,cmp1); int pos=0; for(int i=1;i<=m;i++){ while(rect[i].p1>point[pos+1].x&&pos<=n){ pos+=1; add(1,1,n,point[i].y); } ans[rect[i].id]-=query(1,1,n,rect[i].p2,rect[i].q2); } memset(tree,0,sizeof(tree)); sort(rect+1,rect+m+1,cmp); pos=0; for(int i=1;i<=m;i++){ while(rect[i].q1>=point[pos+1].x&&pos<=n){ pos+=1; add(1,1,n,pos[i].y); } ans[rect[i].id]+=query(1,1,n,rect[i].p2,rect[i].q2); } for(int i=1;i<=m;i++)printf("%d\n",ans[i]); return 0; }
|