洛谷 - P3810 陌上花开


传送门:洛谷 - P3810

思路分析

先对一维进行排序,然后对二维进行归并的时候计算贡献
因为只计算左边对右边的贡献,所以要进行去重

AC代码


#include <bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 2e5+10;

struct node{
    int a,b,c,val,sum;
}s[N],t[N];

int b[N],ans[N],n,m,cnt;

void add(int x,int k){
    for(int i=x;i<N;i+=lowbit(i)) b[i]+=k;
}

int query(int x){
    int ans=0;
    for(int i=x;i;i-=lowbit(i)) ans+=b[i];
    return ans;
}

void cdq(int l,int r){
    if(l==r) return ;
    int mid=l+r>>1;
    cdq(l,mid);
    cdq(mid+1,r);
    int i=l,j=mid+1,k=l;
    while(i<=mid && j<=r){
        if(s[i].b<=s[j].b){
            add(s[i].c,s[i].sum);
            t[k++]=s[i++];
        }else{
            s[j].val+=query(s[j].c);
            t[k++]=s[j++];
        }
    }
    while(i<=mid) {
        add(s[i].c,s[i].sum);
        t[k++]=s[i++];
    }
    while(j<=r){
        s[j].val+=query(s[j].c);
        t[k++]=s[j++];
    }
    for(int i=l;i<=mid;i++) add(s[i].c,-s[i].sum);
    for(int i=l;i<=r;i++) s[i]=t[i];
}

int main() {

#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif

    read(n,m);
    for(int i=1;i<=n;i++){
        read(s[i].a,s[i].b,s[i].c);
        s[i].sum=1;
    } 
    sort(s+1,s+1+n,[](node x,node y){
        if(x.a==y.a){
            if(x.b==y.b) return x.c<y.c;
            else return x.b<y.b;
        }else return x.a<y.a;
    });
    cnt=1;
    for(int i=2;i<=n;i++){
        if(s[i].a==s[cnt].a && s[i].b==s[cnt].b && s[i].c==s[cnt].c) s[cnt].sum++;
        else s[++cnt]=s[i];
    }
    cdq(1,cnt);
    for(int i=1;i<=cnt;i++) ans[s[i].val+s[i].sum-1]+=s[i].sum;
    for(int i=0;i<n;i++) printf("%d\n",ans[i]);



    return 0;
}

文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}