题目大意
![这里写图片描述](https://img-blog.csdn.net/20170124165708582?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2h1bmtpdGxhdQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
解题思路
考虑前面全是插入,后面全是询问的做法。考虑扫描线把插入拆成两个操作,按横坐标排序,用数据结构维护。考虑原问题,考虑分治,把前一半插入对后一半的贡献先求出来消除了影响,再递归求解。
code
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LF double
#define LL long long
#define min(a,b) ((a<b)?a:b)
#define max(a,b) ((a>b)?a:b)
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
using namespace std;
int const mxn=2*1e5;
int n,t[mxn+10],ans[mxn+10];
struct rec{
int op,x,y,xx,yy;
};
rec a[mxn+10],b[mxn*2+10];
bool cmp(rec x,rec y){return (x.x<y.x)||((x.x==y.x)&&(x.op)&&(!y.op));}
void oper(int p,int v){
p++;
while(p<=mxn+1){
t[p]+=v;
p+=p&(-p);
}
}
int qury(int p){
int ans=0;p++;
while(p>0){
ans+=t[p];
p-=p&(-p);
}
return ans+t[0];
}
void solve(int l,int r){
if(l==r)return;
int mid=(l+r)>>1,cnt=0;
fo(i,l,mid)if(!a[i].op){
b[++cnt].op=1;b[cnt].x=a[i].x;b[cnt].y=a[i].y;b[cnt].yy=a[i].yy;
b[++cnt].op=-1;b[cnt].x=a[i].xx+1;b[cnt].y=a[i].y;b[cnt].yy=a[i].yy;
}
fo(i,mid+1,r)if(a[i].op)
b[++cnt].op=0,b[cnt].x=a[i].x,b[cnt].y=a[i].y,b[cnt].xx=i;
sort(b+1,b+cnt+1,cmp);
for(int i=1,j;i<=cnt;i=j+1){
for(j=i;(j+1<=cnt)&&(b[i].x==b[j+1].x);j++);
fo(k,i,j)if(b[k].op)oper(b[k].y,b[k].op),oper(b[k].yy+1,-b[k].op);
else ans[b[k].xx]+=qury(b[k].y);
}
solve(l,mid);solve(mid+1,r);
}
int main(){
freopen("d.in","r",stdin);
freopen("d.out","w",stdout);
scanf("%d",&n);
fo(i,1,n){
scanf("%d%d%d",&a[i].op,&a[i].x,&a[i].y);
if(!a[i].op)scanf("%d%d",&a[i].xx,&a[i].yy);
}
solve(1,n);int cnt=0;
fo(i,1,n)if(a[i].op){
cnt++;
if(cnt==68468){
int bb;
bb++;
}
printf("%d\n",ans[i]);
}
return 0;
}