CF12D
题意 给你3个坐标 x y z
如果一个人的 x y z 都小于另一个人的 x y z 那么这个人就会自杀 问你有多少人会自杀呢
做法 我们想想简化版本 如果只有两个坐标呢? 我们是不是给一维降序排个序 然后插入并且查询
y 到 n 的最大值是否大于当前 y 就行了
但是三维呢? 因为坐标是 1e9 但是只有 1e5个 那么我们首先要离散化 然后按 x 从大到小
以 y 当做坐标插入 z 那么我们每次插入一个点 只要看 y 到 n 是否有大于他的 z 有就要加入答案
但是问题是因为不能等于 所以我们必须把相同的 x 都先处理一下 不能一下子插入加进去
这也是为什么插入的时候有个while 再借助 j 转换的原因
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
const int MAX_N = 500025;
int maxx[MAX_N<<2],b[MAX_N],c[MAX_N];
struct node
{
int x,y,z;
bool operator <(const node other)
{
if(x==other.x)
{
if(y==other.y)
return z> other.z;
return y > other.y;
}
return x > other.x;
}
}arr[MAX_N];
void up(int rt)
{
maxx[rt] = max(maxx[rt<<1],maxx[rt<<1|1]);
}
void build(int rt,int l,int r)
{
maxx[rt] = 0;
if(l==r)
{
return ;
}
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
up(rt);
}
void update(int rt,int l,int r,int x,int v)
{
if(l==r)
{
maxx[rt] = max(maxx[rt],v);
return ;
}
if(x<=mid) update(rt<<1,l,mid,x,v);
else update(rt<<1|1,mid+1,r,x,v);
up(rt);
}
int query(int rt,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
{
return maxx[rt];
}
if(x>mid) return query(rt<<1|1,mid+1,r,x,y);
else if(y<=mid) return query(rt<<1,l,mid,x,y);
else return max(query(rt<<1,l,mid,x,y),query(rt<<1|1,mid+1,r,x,y));
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n;scanf("%d",&n);
long long ans = 0;
for(int i = 1;i<=n;++i) scanf("%d",&arr[i].x);
for(int i = 1;i<=n;++i) scanf("%d",&arr[i].y),b[i] = arr[i].y;
for(int i = 1;i<=n;++i) scanf("%d",&arr[i].z),c[i] = arr[i].z;
sort(b+1,b+1+n);sort(c+1,c+1+n);
sort(arr+1,arr+1+n);
int sz = unique(b+1,b+1+n)-b-1,sz_ = unique(c+1,c+1+n)-c-1;
for(int i = 1;i<=n;++i) arr[i].y = lower_bound(b+1,b+1+sz,arr[i].y) - b,arr[i].z = lower_bound(c+1,c+1+sz_,arr[i].z)-c;
build(1,1,n);
ans = 0;
int i ,j;
for(i = 1;i<=n;i = j)
{
j = i;
while(j<=n&&arr[j].x==arr[i].x)
{
if(query(1,1,n,arr[j].y+1,n)>arr[j].z) ans++;
j++;
}
for(int k = i;k<j;++k)
update(1,1,n,arr[k].y,arr[k].z);
}
printf("%lld\n",ans);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}

本文详细解析了CF12D题目,通过离散化和树状数组的方法解决三维坐标中的人际关系自杀问题。文章首先简化问题至二维坐标,使用一维排序和查询最大值的方法,随后扩展至三维,采用x坐标降序排列,以y为坐标插入z值,判断y至n范围内是否存在更大的z值,以此确定自杀人数。
173

被折叠的 条评论
为什么被折叠?



