洛谷P2391 白雪皑皑 题解

洛谷P2391 白雪皑皑 题解

题目链接:P2391 白雪皑皑

题意

现在有 n n n 片雪花排成一列。 pty 要对雪花进行 m m m 次染色操作,第 i i i 次染色操作中,把第 ( ( i × p + q )   m o d   n ) + 1 ((i\times p+q)\bmod n)+1 ((i×p+q)modn)+1 片雪花和第 ( ( i × q + p )   m o d   n ) + 1 ((i\times q+p)\bmod n)+1 ((i×q+p)modn)+1 片雪花之间的雪花(包括端点)染成颜色 i i i。其中 p , q p,q p,q 是给定的两个正整数。他想知道最后 n n n 片雪花被染成了什么颜色。没有被染色输出 0 0 0

1 ≤ n ≤ 1 0 6 ,   1 ≤ m ≤ 1 0 7 1\leq n\leq 10^6,~1\leq m\leq 10^7 1n106, 1m107

保证 1 ≤ m × p + q , m × q + p ≤ 2 × 1 0 9 1\leq m\times p+q,m\times q+p\leq 2\times 10^9 1m×p+q,m×q+p2×109

一句话题意:线性区间推平(区间赋同一个值),全局询问。

我们模拟赛t3,也是我想了几个月结果有原题的东西,不写题解亏大了

考虑倒序处理修改,因为一个位置的值一定由它最后一次被修改的数决定

暴力推平?对于被推平的点,不可以再推了,所以我们要跳过它

这就需要维护每个点右侧的那个未被推平的结点

可以考虑链表或者并查集维护,这里用并查集

时间复杂度 O ( n ) O(n) O(n)

代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <random>
using namespace std;
// #define int long long
// #define INF 0x3f3f3f3f3f3f3f3f
namespace FastIO
{
    #define gc() readchar()
    #define pc(a) putchar(a)
    #define SIZ (int)(1e6+15)
    char buf1[SIZ],*p1,*p2;
    char readchar()
    {
        if(p1==p2)p1=buf1,p2=buf1+fread(buf1,1,SIZ,stdin);
        return p1==p2?EOF:*p1++;
    }
    template<typename T>void read(T &k)
    {
        char ch=gc();T x=0,f=1;
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}
        while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
        k=x*f;
    }
    template<typename T>void write(T k)
    {
        if(k<0){k=-k;pc('-');}
        static T stk[66];T top=0;
        do{stk[top++]=k%10,k/=10;}while(k);
        while(top){pc(stk[--top]+'0');}
    }
}using namespace FastIO;
#define N (int)(1e6+25)

int n,m,p,q,val[N],f[N];
void init(int n){for(int i=1; i<=n; i++) f[i]=i;}
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
    // freopen("check.in","r",stdin);
    // freopen("check.out","w",stdout);
    
    read(n);init(n+5);
    read(m);read(p);read(q);
    for(int i=m; i>=1; i--)
    {
        int l=(1ll*i*p+q)%n+1;
        int r=(1ll*i*q+p)%n+1;
        if(l>r)swap(l,r);
        for(int j=l; j<=r;)
        {
            int k=find(j); if(k>r)break;
            val[k]=i; f[k]=find(k+1); j=f[k];
        }
    }
    for(int i=1; i<=n; i++) write(val[i]),pc('\n');
    return 0;
}

是不是很神奇,我之前还想过差分、单调栈、线段覆盖、二维凸包(?等一大堆东西

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值