比赛 进阶指南第0章测试 评测结果 AAAAAAAAAA
题目名称 国旗计划 最终得分 100
用户昵称 rzzakioi 运行时间 1.411 s
代码语言 C++ 内存使用 40.62 MiB
提交时间 2026-03-14 12:17:35
显示代码纯文本
#include<cstdio>
#include<algorithm>
#define int long long
using namespace std;
int n,m,to[400005][21],Ans[200005];
struct node{
    int l,r,id;
    bool operator <(const node &other)const{
        if(l==other.l)return r<other.r;
        return l<other.l;
    }
}a[400005];
signed main(){
    freopen("flagplan.in","r",stdin);
    freopen("flagplan.out","w",stdout);
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%lld%lld",&a[i].l,&a[i].r);
        if(a[i].l>a[i].r)a[i].r+=m;
        a[i].id=i;
    }
    for(int i=n+1;i<=2*n;i++){
        a[i]={a[i-n].l+m,a[i-n].r+m,a[i-n].id};
    }
    sort(a+1,a+2*n+1);
    int l=1,r=1,num=0;
    while(l<=2*n){
        num=0;
        if(a[r].r>num){
            num=a[r].r;
            to[l][0]=r;
        }
        while(a[r+1].l<=a[l].r&&r+1<=2*n){
            r++;
            if(a[r].r>num){
                num=a[r].r;
                to[l][0]=r;
            }
        }
        l++;
    }
    for(int j=1;(1<<j)<=2*n;j++){
        for(int i=1;i<=2*n;i++){
            to[i][j]=to[to[i][j-1]][j-1];
        }
    }
    for(int i=1;i<=n;i++){
        int ans=0,ed=a[i].l+m,x=i;
        for(int j=20;j>=0;j--){
            if(to[x][j]&&a[to[x][j]].r<ed){
                ans+=(1<<j);
                x=to[x][j];
            }
        }
        Ans[a[i].id]=ans+1;
    }
    for(int i=1;i<=n;i++){
        printf("%lld ",Ans[i]+1);
    }
    return 0;
}