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
|
#include <algorithm> #include <cstring> using namespace std; const int MAXL = 1e7+5; const int MAXN = 500100; int n,m,ans[MAXN*5],qid,aid,maxy=-1; namespace BIT{ int bit[MAXL<<1]; int (int x){ return x&(-x); } void add(int pos,int val){ while(pos<=maxy){ bit[pos]+=val; pos+=lowbit(pos); } } int query(int pos){ int ans=0; while(pos){ ans+=bit[pos]; pos-=lowbit(pos); } return ans; } void clear(int pos){ while(pos<=maxy){ if(bit[pos]) bit[pos]=0; else break; pos+=lowbit(pos); } } }; struct Query{ int mode,val,IorD,ansid,posx,posy; bool operator < (const Query &b) const{ return posx==b.posx?mode<b.mode:posx<b.posx; }; }query[MAXN*5]; Query tmp[MAXN*5]; void cdq(int L,int R){ if(R<=L+1) return; int mid=(L+R)>>1; cdq(L,mid); cdq(mid,R); int l=L,r=mid,tot=0; while(l<mid&&r<R){ if(query[l]<query[r]){ if(query[l].mode==1) BIT::add(query[l].posy,1); tmp[++tot]=query[l]; l++; } else{ if(query[r].mode==2) ans[query[r].ansid]+=query[r].IorD*BIT::query(query[r].posy); tmp[++tot]=query[r]; r++; } } while(l<mid) tmp[++tot]=query[l++]; while(r<R){ if(query[r].mode==2) ans[query[r].ansid]+=query[r].IorD*BIT::query(query[r].posy); tmp[++tot]=query[r]; r++; } for(int i=1;i<=tot;i++){ BIT::clear(tmp[i].posy); query[L+i-1]=tmp[i]; } } int main(){ scanf("%d %d",&n,&m); for(int i=1;i<=n;i++){ int x,y; scanf("%d %d",&x,&y); x++,y++; maxy=max(maxy,y); query[++qid].mode=1; query[qid].posx=x; query[qid].posy=y; query[qid].val=1; } for(int i=1;i<=m;i++){ int x1,x2,y1,y2; scanf("%d %d %d %d",&x1,&y1,&x2,&y2); x1++,x2++,y1++,y2++; maxy=max(maxy,max(y1,y2));
query[++qid].ansid=++aid; query[qid].IorD=1; query[qid].mode=2; query[qid].posx=x2; query[qid].posy=y2;
query[++qid].ansid=aid; query[qid].IorD=-1; query[qid].mode=2; query[qid].posx=x2; query[qid].posy=y1-1;
query[++qid].ansid=aid; query[qid].IorD=-1; query[qid].mode=2; query[qid].posx=x1-1; query[qid].posy=y2;
query[++qid].ansid=aid; query[qid].IorD=1; query[qid].mode=2; query[qid].posx=x1-1; query[qid].posy=y1-1; } cdq(0,qid+1); for(int i=1;i<=m;i++) printf("%dn",ans[i]); return 0; }
|
近期评论