传送门
在不强制在线的情况下我们为啥要去写树套树呢?
首先我们将询问离散化,然后我们将每一个询问分成四个小的询问
然后我们将修改和询问按照x坐标排序。
每次树状数组暴力求解即可。
时间复杂度O((N+M)logN)
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#define N 500005
using namespace std;
struct data{int x,y,id,f;}q[N*5];
int d[N*5],a[N],b[N],ask[N][4],ans[N][5],c[N*3];
int n,m,tot,tot1,sum;
inline bool cmp(data a,data b){
if (a.x==b.x) return a.f<b.f;
return a.x<b.x;
}
inline int read(){
int x=0;
char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar());
for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x;
}
inline int ef(int x){
int l=0,r=tot,m;
while (l<r){
m=(l+r+1)/2;
if (c[m]>x) r=m-1; else l=m;
}
return l;
}
inline void build(int x,int y,int id,int f){
q[++sum].x=x;
q[sum].y=ef(y);
q[sum].f=f;
q[sum].id=id;
}
inline void change(int x){
for (;x<=tot;x+=x&(-x)) d[x]++;
}
inline int get(int x){
int s=0;
for (;x>0;x-=x&(-x)) s+=d[x];
return s;
}
int main(){
n=read();
m=read();
for (int i=1;i<=n;i++){
a[i]=read();
c[++tot]=b[i]=read();
}
for (int i=1;i<=m;i++){
ask[i][0]=read();
c[++tot]=ask[i][1]=read();
ask[i][2]=read();
c[++tot]=ask[i][3]=read();
}
sort(c+1,c+tot+1);
tot1=1;
for (int i=2;i<=tot;i++)
if (c[i]!=c[i-1]) c[++tot1]=c[i];
tot=tot1;
sum=0;
for (int i=1;i<=n;i++){
q[++sum].x=a[i];
q[sum].y=ef(b[i]);
}
for (int i=1;i<=m;i++){
build(ask[i][2],ask[i][3],i,1);
build(ask[i][0]-1,ask[i][3],i,2);
build(ask[i][2],ask[i][1]-1,i,3);
build(ask[i][0]-1,ask[i][1]-1,i,4);
}
sort(q+1,q+sum+1,cmp);
for (int i=1;i<=sum;i++)
if (q[i].f) ans[q[i].id][q[i].f]=get(q[i].y);
else change(q[i].y);
for (int i=1;i<=m;i++) printf("%d\n",ans[i][1]+ans[i][4]-ans[i][2]-ans[i][3]);
}