题目大意:矩阵上有若干个数点,每次询问子矩阵的权
题解:差分后就是二维偏序了……
离线,x排序,y离散化上树状数组
我的收获:2333
#include <bits/stdc++.h>
using namespace std;
#define N 500005
#define MX 2999999
int n,m,w,tot;
int X[N],Y[N];
int A[N],B[N],C[N],D[N];
int z[MX];
int ans[N][5];
struct ques{
int x,y,id,opt;
void makeby(int i){x=X[i],y=lower_bound(z+1,z+w+1,Y[i])-z;}
}q[MX];
inline bool operator < (ques a,ques b){return a.x<b.x||(a.x==b.x&&a.opt<b.opt);}
struct Tree{
int c[MX];
void add(int x,int v){for(;x<=w;x+=x&-x) c[x]+=v;}
int ask(int x){int ret=0;for(;x;x-=x&-x) ret+=c[x];return ret;}
}T;
void addquery(int i)
{
B[i]=lower_bound(z+1,z+w+1,B[i])-z;
D[i]=lower_bound(z+1,z+w+1,D[i])-z;
q[++tot].x=A[i]-1,q[tot].y=B[i]-1,q[tot].id=i,q[tot].opt=1;
q[++tot].x=A[i]-1,q[tot].y=D[i],q[tot].id=i,q[tot].opt=2;
q[++tot].x=C[i],q[tot].y=B[i]-1,q[tot].id=i,q[tot].opt=3;
q[++tot].x=C[i];q[tot].y=D[i];q[tot].id=i;q[tot].opt=4;
}
void solve()
{
sort(q+1,q+tot+1);
for(int i=1;i<=tot;i++)
{
if(!q[i].opt) T.add(q[i].y,1);
else ans[q[i].id][q[i].opt]=T.ask(q[i].y);
}
}
void work()
{
solve();
for(int i=1;i<=m;i++) printf("%d\n",ans[i][4]-ans[i][3]-ans[i][2]+ans[i][1]);
}
void init()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d%d",&X[i],&Y[i]),X[i]++,Y[i]++,z[++w]=Y[i];
for(int i=1;i<=m;i++) scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]),A[i]++,B[i]++,C[i]++,D[i]++,z[++w]=B[i],z[++w]=D[i];
sort(z+1,z+w+1);w=unique(z+1,z+w+1)-z-1;
for(int i=1;i<=n;i++) q[++tot].makeby(i);
for(int i=1;i<=m;i++) addquery(i);
}
int main()
{
init();
work();
return 0;
}