P3810 三维偏序(陌上花开)

题目链接:https://www.luogu.com.cn/problem/P3810
线段树套线段树

#include<bits/stdc++.h>
#define ls lc[o]
#define rs rc[o]
#define mid (l+r)/2
using namespace std;
typedef long long ll;
const int NN=100100;
const int inf=0x3f3f3f3f;
ll n,m,k,x,y,z,q,W,T,N,cnt,tmp,dst,cas,val,tot,num;
int c[NN*80],lc[NN*80],rc[NN*80],rot[NN*2],ans[NN];
struct node{
    int x,y,z;
    bool operator <(const node nod)const{
        return (x==nod.x?(y==nod.y?z<nod.z:y<nod.y):x<nod.x);
    }
    bool operator !=(const node nod)const{
        return x!=nod.x||y!=nod.y||z!=nod.z;
    }
    void input(){
        cin>>x>>y>>z;
    }
};

node a[NN];

//动态开点,其实没那么难,不要被吓到了
void update(int l,int r,int pos,int &o){
    if(!o) o=++tot;
    c[o]++;
    if(l==r) return ;
    if(mid>=pos) update(l,mid,pos,ls);
    else update(mid+1,r,pos,rs);
}

void update(int i,int val){
    while(i<=k){
    	//对权值树状数组的每一个权值维护一个动态开点的权值线段树
        update(1,k,val,rot[i]);
        i+=i&-i;
    }
}

ll query(int l,int r,int pos,int o){
    if(r<=pos||c[o]==0) return c[o];
    ll sum=0;
    sum+=query(l,mid,pos,ls);
    if(mid<pos) sum+=query(mid+1,r,pos,rs);
    return sum;
}

ll getnum(int i,int val){
    ll sum=0;
    while(i){
        sum+=query(1,k,val,rot[i]);
        i-=i&-i;
    }
    return sum;
}

int main(){
    ios::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1;i<=n;i++) a[i].input();
    sort(a+1,a+n+1);
    int cnt=1;
    for(int i=1;i<=n;i++){
        update(a[i].y,a[i].z);
        if(a[i]!=a[i+1]) ans[getnum(a[i].y,a[i].z)]+=cnt,cnt=1;
        else cnt++;
    }
    for(int i=1;i<=n;i++) cout<<ans[i]<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值