萌新刚学cdq RE+WA0pts求条
查看原帖
萌新刚学cdq RE+WA0pts求条
530468
_determination_楼主2024/3/3 18:12

rt

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define inf 0x3f3f3f3f3f3f3f3f
// #define endl '\n'
#define debug cout << "114514" << endl
struct node{
    int x,y,z,ans,cnt;
}a[200010],b[200010];
int n,k;
int cmp(node p,node q)
{
    if(p.x==q.x)
    {
        if(p.y==q.y)
        {
            return p.z<q.z;
        }
        return p.y<q.y;
    }
    return p.x<q.x;
}
int t[200010];
int lowbit(int x){return x&(-x);}
void upd(int x,int v)
{
    while(x<=k)
    {
        t[x]+=v;
        x+=lowbit(x);
    }
}
int calc(int x)
{
    int ans=0;
    while(x)
    {
        ans+=t[x];
        x-=lowbit(x);
    }
    return ans;
}
void cdq(int l,int r)
{
    if(l==r)
    {
        return;
    }
    int mid=(l+r)/2;
    cdq(l,mid);
    cdq(mid+1,r);
    int p=l,q=mid+1,tot=0;
    while(p<=mid&&q<=r)
    {
        if(a[p].y<=a[q].y)
        {
            upd(a[p].z,a[p].cnt);
            b[++tot]=a[p++];
        }else{
            a[q].ans+=calc(a[q].z);
            b[++tot]=a[q++];
        }
    }
    while(p<=mid)
    {
        upd(a[p].z,a[p].cnt);
        b[++tot]=a[p++];
    }
    while(q<=r)
    {
        a[q].ans+=calc(a[q].z);
        b[++tot]=a[q++];
    }
    for ( int i = l ; i <= mid ; i++ )
    {
        upd(a[i].z,-a[p].cnt);
    }
    for ( int i = 1 ; i <= tot ; i++ )
    {
        a[i+l-1]=b[i];
    }
}
int ans[200010];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
	cin >> n >> k;
    assert(k<=200000);
    for ( int i = 1 ; i <= n ; i++ )
    {
        cin >> a[i].x >> a[i].y >> a[i].z ;
        a[i].ans=0;
        a[i].cnt=1;
    }
    sort(a+1,a+1+n,cmp);
    int m=1;
    for ( int i = 2 ; i <= n ; i++ )
    {
        if(a[i].x==a[m].x&&a[i].y==a[m].y&&a[i].z==a[m].z)
        {
            a[m].cnt++;
        }else{
            a[++m]=a[i];
        }
    }
    cdq(1,n);
    for ( int i = 1 ; i <= n ; i++ )
    {
        ans[a[i].ans+a[i].cnt-1]+=a[i].cnt;
    }
    for ( int i = 0 ; i < n ; i++ )
    {
        cout << ans[i] << endl;
    }
    return 0;
}
2024/3/3 18:12
加载中...