题解 P4648 【[IOI2007] pairs 动物对数】
这题好像没有通法,所以我选择分部来写
1.暴力
对于
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 200500
using namespace std;
inline int read() {
int x = 0,f = 1; char s = getchar();
while(s < '0' || s > '9') {if(s == '-') f = -1; s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + s - '0'; s = getchar();}
return x * f;
}
int n,m,B,D;
int b[N][4];
inline int DD(int x) {return x < 0 ? -x : x;}
inline bool Calc(int x,int y) {
int x1 = b[x][0],y1 = b[x][1],z1 = b[x][2];
int x2 = b[y][0],y2 = b[y][1],z2 = b[y][2];
if(DD(x1 - x2) + DD(y1 - y2) + DD(z1 - z2) <= D) return true;
return false;
}
int main() {
B = read(),n = read(),D = read(),m = read();
int ans = 0;
for(int i = 1;i <= n;i ++)
for(int j = 0;j < B;j ++)
b[i][j] = read();
for(int i = 1;i <= n;i ++)
for(int j = i + 1;j <= n;j ++)
if(Calc(i,j)) ans ++;
printf("%d\n",ans);
return 0;
}
2.B == 1 的情况
一维可以考虑下权值线段树,
具体怎么实现呢:
对于点
所以,对于前
统计完成后,在将次点插入到线段树中即可
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 200500
using namespace std;
inline int read() {
int x = 0,f = 1; char s = getchar();
while(s < '0' || s > '9') {if(s == '-') f = -1; s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + s - '0'; s = getchar();}
return x * f;
}
int n,m,B,D;
struct node {
int x,y,z;
}a[N];
inline int DD(int x) {return x < 0 ? -x : x;}
struct Tree {
int l,r;
long long tag;
} tr[N * 50];
int tot,root;
inline void Insert(int &k,int l,int r,int a) {
if(!k) k = ++ tot;
if(l == r) {
tr[k].tag ++;
return ;
}
int mid = (l + r) >> 1;
if(a <= mid) Insert(tr[k].l,l,mid,a);
else Insert(tr[k].r,mid + 1,r,a);
tr[k].tag = tr[tr[k].l].tag + tr[tr[k].r].tag;
}
inline long long Ask(int k,int l,int r,int a,int b) {
if(!k) return 0;
if(a <= l && r <= b) return tr[k].tag;
int mid = (l + r) >> 1; long long res = 0;
if(a <= mid) res += Ask(tr[k].l,l,mid,a,b);
if(b > mid) res += Ask(tr[k].r,mid + 1,r,a,b);
return res;
}
int main() {
B = read(),n = read(),D = read(),m = read();
if(B == 1) {
long long ans = 0;
for(int i = 1;i <= n;i ++)
a[i].x = read();
for(int i = 1;i <= n;i ++) {
ans += Ask(root,1,m,a[i].x - D,a[i].x + D);
Insert(root,1,m,a[i].x);
}
printf("%lld\n",ans); return 0;
}
return 0;
}
3.B == 2 的情况
这就比较自闭了
由题意得,距离公式为
要使
发现,难以维护菱形中的点的个数;
所以,我们要旋转坐标系,
具体的证明(不太会,我的理解)就是:
重写便曼哈顿距离:
把绝对值拆开:
1.
2.
3.
4.
发现:
在把柿子组合下:
这个和切比雪夫公式对比下:
发现对于坐标
具体的操作是,对于转化后的点:
先按
那么,对于
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 200500
using namespace std;
inline int read() {
int x = 0,f = 1; char s = getchar();
while(s < '0' || s > '9') {if(s == '-') f = -1; s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + s - '0'; s = getchar();}
return x * f;
}
int n,m,B,D;
struct node {
int x,y,z;
}a[N];
inline int DD(int x) {return x < 0 ? -x : x;}
struct Tree {
int l,r;
long long tag;
} tr[N * 50];
int tot,root;
inline void Insert(int &k,int l,int r,int a,int b) {
if(!k) k = ++ tot;
if(l == r) {
tr[k].tag += b;
return ;
}
int mid = (l + r) >> 1;
if(a <= mid) Insert(tr[k].l,l,mid,a,b);
else Insert(tr[k].r,mid + 1,r,a,b);
tr[k].tag = tr[tr[k].l].tag + tr[tr[k].r].tag;
}
inline long long Ask(int k,int l,int r,int a,int b) {
if(!k) return 0;
if(a <= l && r <= b) return tr[k].tag;
int mid = (l + r) >> 1; long long res = 0;
if(a <= mid) res += Ask(tr[k].l,l,mid,a,b);
if(b > mid) res += Ask(tr[k].r,mid + 1,r,a,b);
return res;
}
inline bool cmp1(const node &a,const node &b) {
return a.x < b.x;
}
int main() {
B = read(),n = read(),D = read(),m = read();
if(B == 2) {
long long ans = 0;
for(int i = 1;i <= n;i ++)
a[i].x = read(),a[i].y = read();
for(int i = 1;i <= n;i ++) {
int x = a[i].x,y = a[i].y;
a[i].x = x + y;
a[i].y = x - y;
}
sort(a + 1,a + n + 1,cmp1);
int tl = 1; Insert(root,-m,m,a[1].y,1);
for(int i = 2;i <= n;i ++) {
while(DD(a[tl].x - a[i].x) > D)
Insert(root,-m,m,a[tl].y,-1),tl ++;
ans += Ask(root,-m,m,a[i].y - D,a[i].y + D);
Insert(root,-m,m,a[i].y,1);
}
printf("%lld\n",ans); return 0;
}
return 0;
}
3.B==3的情况
观察到
其实也不是乱搞;
借鉴下
而对于此情况可以把维度提高,也就是多个平面
再次提及
之后对于每层求二维前缀和,这样就可以
不妨设
那么可以强制选
对于
具体见代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 200500
using namespace std;
inline int read() {
int x = 0,f = 1; char s = getchar();
while(s < '0' || s > '9') {if(s == '-') f = -1; s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + s - '0'; s = getchar();}
return x * f;
}
int n,m,B,D;
struct node {
int x,y,z;
}a[N];
int buk[100][155][155];
inline int DD(int x) {return x < 0 ? -x : x;}
int main() {
B = read(),n = read(),D = read(),m = read();
if(B == 3) {
for(int i = 1;i <= n;i ++)
a[i].x = read(),a[i].y = read(),a[i].z = read();
for(int i = 1;i <= n;i ++) {
int x = a[i].x,y = a[i].y;
a[i].x = x + y;
a[i].y = x - y + m;
}
for(int i = 1;i <= n;i ++)
buk[a[i].z][a[i].x][a[i].y] ++;
for(int k = 1;k <= m;k ++) {
for(int i = 1;i <= m * 2;i ++)
for(int j = 1;j <= m * 2;j ++)
buk[k][i][j] += buk[k][i - 1][j] + buk[k][i][j - 1] - buk[k][i - 1][j - 1];
}
long long ans1 = 0,ans2 = 0;
for(int i = 1;i <= n;i ++) {
for(int j = max(1,a[i].z - D);j < a[i].z;j ++) {
int d = D - (a[i].z - j);
int x1 = max(1,a[i].x - d),x2 = min(m * 2,a[i].x + d);
int y1 = max(1,a[i].y - d),y2 = min(m * 2,a[i].y + d);
ans1 += buk[j][x2][y2] - buk[j][x2][y1 - 1] - buk[j][x1 - 1][y2] + buk[j][x1 - 1][y1 - 1];
}
int d = D;
int x1 = max(1,a[i].x - d),x2 = min(m * 2,a[i].x + d);
int y1 = max(1,a[i].y - d),y2 = min(m * 2,a[i].y + d);
ans2 += buk[a[i].z][x2][y2] - buk[a[i].z][x2][y1 - 1] - buk[a[i].z][x1 - 1][y2] + buk[a[i].z][x1 - 1][y1 - 1];
}
ans1 += (ans2 - n) / 2;
printf("%lld\n",ans1); return 0;
}
return 0;
}
最后,放上AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 200500
using namespace std;
inline int read() {
int x = 0,f = 1; char s = getchar();
while(s < '0' || s > '9') {if(s == '-') f = -1; s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + s - '0'; s = getchar();}
return x * f;
}
int n,m,B,D;
struct node {
int x,y,z;
}a[N];
int buk[100][155][155];
inline int DD(int x) {return x < 0 ? -x : x;}
struct Tree {
int l,r;
long long tag;
} tr[N * 50];
int tot,root;
inline void Insert(int &k,int l,int r,int a,int b) {
if(!k) k = ++ tot;
if(l == r) {
tr[k].tag += b;
return ;
}
int mid = (l + r) >> 1;
if(a <= mid) Insert(tr[k].l,l,mid,a,b);
else Insert(tr[k].r,mid + 1,r,a,b);
tr[k].tag = tr[tr[k].l].tag + tr[tr[k].r].tag;
}
inline long long Ask(int k,int l,int r,int a,int b) {
if(!k) return 0;
if(a <= l && r <= b) return tr[k].tag;
int mid = (l + r) >> 1; long long res = 0;
if(a <= mid) res += Ask(tr[k].l,l,mid,a,b);
if(b > mid) res += Ask(tr[k].r,mid + 1,r,a,b);
return res;
}
inline bool cmp1(const node &a,const node &b) {
return a.x < b.x;
}
int main() {
B = read(),n = read(),D = read(),m = read();
if(B == 1) {
long long ans = 0;
for(int i = 1;i <= n;i ++)
a[i].x = read();
for(int i = 1;i <= n;i ++) {
ans += Ask(root,1,m,a[i].x - D,a[i].x + D);
Insert(root,1,m,a[i].x,1);
}
printf("%lld\n",ans); return 0;
}
if(B == 2) {
long long ans = 0;
for(int i = 1;i <= n;i ++)
a[i].x = read(),a[i].y = read();
for(int i = 1;i <= n;i ++) {
int x = a[i].x,y = a[i].y;
a[i].x = x + y;
a[i].y = x - y;
}
sort(a + 1,a + n + 1,cmp1);
int tl = 1; Insert(root,-m,m,a[1].y,1);
for(int i = 2;i <= n;i ++) {
while(DD(a[tl].x - a[i].x) > D)
Insert(root,-m,m,a[tl].y,-1),tl ++;
ans += Ask(root,-m,m,a[i].y - D,a[i].y + D);
Insert(root,-m,m,a[i].y,1);
}
printf("%lld\n",ans); return 0;
}
if(B == 3) {
for(int i = 1;i <= n;i ++)
a[i].x = read(),a[i].y = read(),a[i].z = read();
for(int i = 1;i <= n;i ++) {
int x = a[i].x,y = a[i].y;
a[i].x = x + y;
a[i].y = x - y + m;
}
for(int i = 1;i <= n;i ++)
buk[a[i].z][a[i].x][a[i].y] ++;
for(int k = 1;k <= m;k ++) {
for(int i = 1;i <= m * 2;i ++)
for(int j = 1;j <= m * 2;j ++)
buk[k][i][j] += buk[k][i - 1][j] + buk[k][i][j - 1] - buk[k][i - 1][j - 1];
}
long long ans1 = 0,ans2 = 0;
for(int i = 1;i <= n;i ++) {
for(int j = max(1,a[i].z - D);j < a[i].z;j ++) {
int d = D - (a[i].z - j);
int x1 = max(1,a[i].x - d),x2 = min(m * 2,a[i].x + d);
int y1 = max(1,a[i].y - d),y2 = min(m * 2,a[i].y + d);
ans1 += buk[j][x2][y2] - buk[j][x2][y1 - 1] - buk[j][x1 - 1][y2] + buk[j][x1 - 1][y1 - 1];
}
int d = D;
int x1 = max(1,a[i].x - d),x2 = min(m * 2,a[i].x + d);
int y1 = max(1,a[i].y - d),y2 = min(m * 2,a[i].y + d);
ans2 += buk[a[i].z][x2][y2] - buk[a[i].z][x2][y1 - 1] - buk[a[i].z][x1 - 1][y2] + buk[a[i].z][x1 - 1][y1 - 1];
}
ans1 += (ans2 - n) / 2;
printf("%lld\n",ans1); return 0;
}
return 0;
}
撒花✿✿ヽ(°▽°)ノ✿