题目链接
题意
给出
n
n
n 个点,每个点有三个属性
X
,
Y
,
Z
X,Y,Z
X,Y,Z,
f
[
i
]
f[i]
f[i] 表示点对
(
i
,
j
)
(i,j)
(i,j)满足
X
i
<
X
j
,
?
Y
i
<
Y
j
,
?
Z
i
<
Z
j
X_i < X_j,\ Y_i<Y_j,\ Z_i<Z_j
Xi?<Xj?,?Yi?<Yj?,?Zi?<Zj?的
j
j
j 的个数,求
f
[
0...
n
?
1
]
f[0...n-1]
f[0...n?1]
题解
CDQ分治的模板题,先对第一维排序消掉 X,再分治处理第二维 Y,用树状数组处理 Z
CDQ分治
#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include <utility>
#include<list>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<time.h>
#include<random>
using namespace std;
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
#include<ext/rope>
using namespace __gnu_cxx;
#define int long long
#define PI acos(-1.0)
#define eps 1e-9
#define lowbit(a) ((a)&-(a))
#define mid ((l+r)>>1)
const int mod = 1e9+7;
int qpow(int a,int b){
int ans=1;
while(b){
if(b&1)ans=(ans*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return ans;
}
const int INF = 0x3f3f3f3f;
const int N = 2e5+10;
struct node{
int x,y,z,w,ans;
}a[N],b[N];
int ta[N],n,m,k,res[N];
bool cmp1(node a,node b){ return a.x==b.x?a.y==b.y?a.z<b.z:a.y<b.y:a.x<b.x;}
bool cmp2(node a,node b){ return a.y==b.y?a.z<b.z:a.y<b.y;}
int ask(int x,int ans=0){ for(int i=x;i;i-=lowbit(i)) ans+=ta[i]; return ans;}
void add(int x,int w){ for(int i=x;i<=k;i+=lowbit(i)) ta[i]+=w;}
void cdq(int l,int r){
if(l==r)return;
cdq(l,mid),cdq(mid+1,r);
sort(b+l,b+1+mid,cmp2),sort(b+1+mid,b+1+r,cmp2);
int i=l,j=mid+1;
for(;j<=r;j++){
while(b[i].y<=b[j].y&&i<=mid) add(b[i].z,b[i].w),i++;
b[j].ans+=ask(b[j].z);
}
for(j=l;j<i;j++)add(b[j].z,-b[j].w);
}
#define endl '\n'
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i].x>>a[i].y>>a[i].z;
sort(a+1,a+1+n,cmp1);
int cnt=0;
for(int i=1;i<=n;i++){
cnt++;
if(a[i].x!=a[i+1].x||a[i].y!=a[i+1].y||a[i].z!=a[i+1].z)
b[++m]=a[i],b[m].w=cnt,cnt=0;
}
cdq(1,m);
for(int i=1;i<=m;i++) res[b[i].w+b[i].ans-1]+=b[i].w;
for(int i=0;i<n;i++)cout<<res[i]<<endl;
}
或者可以写个树套树,对第一维排序,第二维树状数组,第三维主席树
树套树
#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include <utility>
#include<list>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<time.h>
#include<random>
using namespace std;
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
#include<ext/rope>
using namespace __gnu_cxx;
#define int long long
#define PI acos(-1.0)
#define eps 1e-9
#define lowbit(a) ((a)&-(a))
#define mid ((l+r)>>1)
const int mod = 1e9+7;
int qpow(int a,int b){
int ans=1;
while(b){
if(b&1)ans=(ans*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return ans;
}
const int INF = 0x3f3f3f3f;
const int N = 2e5+10;
struct node{
int x,y,z;
bool friend operator == (const node x,const node y){
return (x.x==y.x&&x.y==y.y&&x.z==y.z);
}
}a[N];
struct Node{
int l,r;
int w;
}hjt[N*40];
int cnt,root[N];
int ta[N],n,m,k,res[N];
bool cmp(node a,node b){ return a.x==b.x?a.y==b.y?a.z<b.z:a.y<b.y:a.x<b.x;}
void update(int &now,int l,int r,int pos,int val){
if(!now)now=++cnt;
hjt[now].w+=val;
if(l==r)return;
if(pos<=mid)update(hjt[now].l,l,mid,pos,val);
else update(hjt[now].r,mid+1,r,pos,val);
}
int query(int now,int l,int r,int ql,int qr){
if(!now)return 0;
if(ql<=l&&r<=qr)return hjt[now].w;
int ans=0;
if(ql<=mid)ans+=query(hjt[now].l,l,mid,ql,qr);
if(qr>mid)ans+=query(hjt[now].r,mid+1,r,ql,qr);
return ans;
}
int ask(int x,int pos,int ans=0){ for(int i=x;i;i-=lowbit(i)) ans+=query(root[i],1,k,1,pos); return ans;}
void add(int x,int pos,int w){ for(int i=x;i<=k;i+=lowbit(i)) update(root[i],1,k,pos,w);}
#define endl '\n'
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i].x>>a[i].y>>a[i].z;
sort(a+1,a+1+n,cmp);
int c=1;
for(int i=1;i<=n;i++){
if(a[i]==a[i+1]){c++; continue;}
add(a[i].y,a[i].z,c);
int ans=ask(a[i].y,a[i].z);
res[ans]+=c;
c=1;
}
for(int i=1;i<=n;i++)cout<<res[i]<<endl;
}
|