[BZOJ2054] 疯狂的馒头/[BZOJ2375] 疯狂的涂色

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=2054
http://www.lydsy.com/JudgeOnline/problem.php?id=2375

题目大意

给定涂色的顺序,后涂色会覆盖之前的,询问m次涂色后的n个点的颜色

题解

后涂的会覆盖之前涂的,所以我们倒着涂还没涂色的部分就一定是最后版本的颜色,每次涂完把区间内的用并查集连起来,就可以做到 O(N+M) 的复杂度了

{$M 100000000,0,100000000}
const
    maxn=1000005;
var
    fa,ans:array[0..maxn]of longint;
    i,j,k:longint;
    n,m,p,q,l,r:longint;
    tt:int64;
function get(a:longint):longint;
begin
    if fa[a]=a then exit(a);
    fa[a]:=get(fa[a]);
    exit(fa[a]);
end;

begin
    readln(n,m,q,p);
    for i:=1 to n+1 do
        begin
            fa[i]:=i;
            ans[i]:=0;
        end;
    for i:=m downto 1 do
        begin
            l:=((i*p+q)mod n)+1; r:=((i*q+p)mod n)+1;
            if l>r then begin j:=l; l:=r; r:=j; end;
            j:=l;
            while j<=r do
                begin
                    if ans[j]=0
                    then begin ans[j]:=i; fa[j]:=get(fa[j+1]); end;                    
                    j:=get(fa[j]);
                end;
            if get(1)=n+1 then break;
        end;
    for i:=1 to n do
        writeln(ans[i]);
end.

你可能感兴趣的:([BZOJ2054] 疯狂的馒头/[BZOJ2375] 疯狂的涂色)