题目链接:http://hdu.hustoj.com/showproblem.php?pid=5126
Problem Description
John loves to see the sky. A day has Q times. Each time John will find a new star in the sky, or he wants to know how many stars between (x1,y1,z1) and (x2,y2,z2).
Input
The first line contains a single integer T(1≤T≤10) (the data for Q>100 less than 6 cases),, indicating the number of test cases.
The first line contains an integer Q(1≤Q≤50000),indicating how many times in a day.
Next Q lines contain some integers, first input an integer A(1≤A≤2).If A=1 then input 3 integers x, y and z, indicating a coordinate of one star.. If A=2 then input 6 integers x1,y1,z1,x2,y2,z2(1≤x,y,z,x1,y1,z1,x2,y2,z2≤109,x1≤x2,y1≤y2,z1≤z2).
Output
For each “A=2”,output an integer means how many stars in such a section.
Sample Input
2
11
1 1 1 1
2 1 1 1 1 1 1
1 2 2 2
1 1 1 2
2 1 1 1 2 2 2
1 3 3 3
1 4 4 4
1 5 5 5
1 6 6 6
2 1 1 1 6 6 6
2 3 3 3 6 6 6
11
1 1 1 1
2 1 1 1 1 1 1
1 2 2 2
1 1 1 2
2 1 1 1 2 2 2
1 3 3 3
1 4 4 4
1 5 5 5
1 6 6 6
2 1 1 1 6 6 6
2 3 3 3 6 6 6
Sample Output
1
3
7
4
1
3
7
4
题目大意:有两种操作,一种是插入(x,y,z)这个坐标,第二种是查询(x1,y1,z1)到(x2,y2,z2)(x1<=x2,y1<=y2,z1<=z2)的长方体包含多少个点。
题目思路:拆分成8个点容斥,求四维偏序。注意,需离散化z,不然树状数组存不下。
可先看BZOJ 1176这个题,求二维的子矩阵和,然后再来求这个会轻松很多,题目链接:https://blog.csdn.net/baodream/article/details/82683690
AC代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 0xffffffffffff;
const int mod = 1e9+7;
const int N = 1e6+5;
struct node{
int typ;
int x,y,z;
int id;
int val;
bool flag;
int pos;
}a[N],tmp[N],tmp1[N];
int vec[N],m,tot,ans[N];
void Hash(){
sort(vec+1,vec+1+m);
int len = unique(vec+1,vec+1+m)-vec-1;
for(int i=1;i<=m;i++)
a[i].z = lower_bound(vec+1,vec+len+1,a[i].z)-vec;
}
const int maxn = 500005;
int tree[maxn];
int lowbit(int x){
return x&(-x);
}
void Add(int x,int val){
while(x<maxn){
tree[x]+=val;
x+=lowbit(x);
}
}
int Query(int x){
int res=0;
while(x){
res+=tree[x];
x-=lowbit(x);
}
return res;
}
void clearr(int x){
while(x<maxn){
if(tree[x]==0)
break;
tree[x]=0;
x+=lowbit(x);
}
}
void CDQ2(int l,int r){
if(l>=r) return ;
int mid = l+r>>1;
CDQ2(l,mid);
CDQ2(mid+1,r);
//printf("l=%d,r=%d\n",l,r);
int p=l,q=mid+1,k=l;
while(p<=mid&&q<=r){
if(tmp[p].y<=tmp[q].y){
if(tmp[p].flag&&tmp[p].typ==1)
Add(tmp[p].z,1);
tmp1[k++] = tmp[p++];
}
else{
if(tmp[q].flag==0&&tmp[q].typ==2){
ans[tmp[q].pos]+=Query(tmp[q].z)*tmp[q].val;
//printf("-------1:%d %d %d %d %d\n",tmp[q].id,tmp[q].z,tmp[q].val,Query(tmp[q].z),Query(tmp[q].z)*tmp[q].val);
}
tmp1[k++] = tmp[q++];
}
}
while(p<=mid){
tmp1[k++] = tmp[p++];
}
while(q<=r){
if(tmp[q].flag==0&&tmp[q].typ==2){
ans[tmp[q].pos]+=Query(tmp[q].z)*tmp[q].val;
//printf("-------2:%d %d %d %d %d\n",tmp[q].id,tmp[q].z,tmp[q].val,Query(tmp[q].z),Query(tmp[q].z)*tmp[q].val);
}
tmp1[k++] = tmp[q++];
}
for(int i=l;i<=r;i++) clearr(tmp[i].z),tmp[i]=tmp1[i];
}
void CDQ(int l,int r){
if(l>=r) return;
int mid = l+r>>1;
CDQ(l,mid);
CDQ(mid+1,r);
int p=l,q=mid+1,k=l;
while(p<=mid&&q<=r){
if(a[p].x<=a[q].x){
tmp[k++] = a[p++];
tmp[k-1].flag = 1;
}
else{
tmp[k++] = a[q++];
tmp[k-1].flag = 0;
}
}
while(p<=mid){
tmp[k++] = a[p++];
tmp[k-1].flag = 1;
}
while(q<=r){
tmp[k++] = a[q++];
tmp[k-1].flag = 0;
}
for(int i=l;i<=r;i++) a[i] = tmp[i];
CDQ2(l,r);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
std::ios::sync_with_stdio(false);
int T,q,type;
scanf("%d",&T);
while(T--){
scanf("%d",&q);
m=0,tot=0;
int x1,y1,z1,x2,y2,z2;
while(q--){
scanf("%d",&type);
if(type==1){
scanf("%d%d%d",&x1,&y1,&z1);
a[++m] = node{1,x1,y1,z1,m,1,0,0};
vec[m] = z1;
}
else{
scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);
++tot;
a[++m] = node{2,x2,y2,z2,m,1,0,tot}; vec[m] = z2;
a[++m] = node{2,x1-1,y2,z2,m,-1,0,tot}; vec[m] = z2;
a[++m] = node{2,x2,y1-1,z2,m,-1,0,tot}; vec[m] = z2;
a[++m] = node{2,x2,y2,z1-1,m,-1,0,tot}; vec[m] = z1-1;
a[++m] = node{2,x2,y1-1,z1-1,m,1,0,tot}; vec[m] = z1-1;
a[++m] = node{2,x1-1,y2,z1-1,m,1,0,tot}; vec[m] = z1-1;
a[++m] = node{2,x1-1,y1-1,z2,m,1,0,tot}; vec[m] = z2;
a[++m] = node{2,x1-1,y1-1,z1-1,m,-1,0,tot}; vec[m] = z1-1;
}
}
Hash();
MEM(ans,0);
MEM(tree,0);
CDQ(1,m);
for(int i=1;i<=tot;i++)
printf("%d\n",ans[i]);
}
return 0;
}