結果

問題 No.945 YKC饅頭
ユーザー 37zigen
提出日時 2019-12-06 01:46:03
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,542 ms / 2,000 ms
コード長 2,356 bytes
コンパイル時間 2,586 ms
コンパイル使用メモリ 81,644 KB
実行使用メモリ 64,648 KB
最終ジャッジ日時 2024-12-23 04:46:21
合計ジャッジ時間 53,905 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 74
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
    
    class DJSet{
        int n;
        int[] upper;
        int[] ma;
        int[] mi;
        int[] cols;
        
        public DJSet(int n){
            this.n=n;
            upper=new int[n];
            ma=new int[n];
            mi=new int[n];
            cols=new int[n];
            Arrays.fill(upper,-1);
            Arrays.fill(cols,-1);
            for(int i=0;i<n;++i){
                ma[i]=i;
                mi[i]=i;
            }
        }
        
        //[l,r)
        void paint(int l, int r, int col){
            if(l>=r)return;
            if(cols[l]!=-1){
                paint(ma[root(l)]+1,r,col);
            }else{
                cols[l]=col;
                paint(l+1,r,col);
            }
            setUnion(l,r-1);
        }
        
        boolean equiv(int a, int b){
            return root(a)==root(b);
        }
        
        int root(int x){
            return upper[x]<0?x:(upper[x]=root(upper[x]));
        }
        
        void setUnion(int x,int y){
            x=root(x);
            y=root(y);
            if(x==y)return;
            if(upper[x]<upper[y]){
                x^=y;y^=x;x^=y;
            }
            upper[y]+=upper[x];
            upper[x]=y;
            ma[y]=Math.max(ma[y],ma[x]);
            mi[y]=Math.min(mi[y],mi[x]);
        }
    }
    
    void run() {
        Scanner sc = new Scanner(System.in);
        int N=sc.nextInt();
        int M=sc.nextInt();
        DJSet ds=new DJSet(N);
        for(int i=0;i<M;++i){
            int L=sc.nextInt();
            int R=sc.nextInt();
            --L;--R;
            String T=sc.next();
            int col=-1;
            if(T.equals("Y")){
                col=0;
            }else if(T.equals("K")){
                col=1;
            }else if(T.equals("C")){
                col=2;
            }else{
                throw new AssertionError();
            }
            ds.paint(L,R+1,col);
        }
        int[] cnt=new int[3];
        for(int i=0;i<N;++i){
            if(ds.cols[i]!=-1)
                cnt[ds.cols[i]]++;
        }
        System.out.println(cnt[0]+" "+cnt[1]+" "+cnt[2]);
    }
    
    void tr(Object...objects){
        System.out.println(Arrays.deepToString(objects));
    }
}
0