結果

問題 No.1194 Replace
ユーザー tentententen
提出日時 2023-06-21 16:49:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,990 ms / 2,000 ms
コード長 2,839 bytes
コンパイル時間 5,278 ms
コンパイル使用メモリ 91,276 KB
実行使用メモリ 119,876 KB
最終ジャッジ日時 2023-09-11 11:59:48
合計ジャッジ時間 37,573 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,500 ms
103,244 KB
testcase_01 AC 1,552 ms
102,716 KB
testcase_02 AC 1,203 ms
89,916 KB
testcase_03 AC 1,097 ms
85,076 KB
testcase_04 AC 1,577 ms
102,496 KB
testcase_05 AC 1,515 ms
102,876 KB
testcase_06 AC 1,308 ms
103,372 KB
testcase_07 AC 1,972 ms
118,112 KB
testcase_08 AC 1,990 ms
118,228 KB
testcase_09 AC 1,943 ms
119,876 KB
testcase_10 AC 1,955 ms
118,012 KB
testcase_11 AC 1,956 ms
119,792 KB
testcase_12 AC 1,962 ms
117,640 KB
testcase_13 AC 1,207 ms
81,800 KB
testcase_14 AC 1,008 ms
77,524 KB
testcase_15 AC 1,015 ms
79,668 KB
testcase_16 AC 1,182 ms
82,092 KB
testcase_17 AC 943 ms
79,080 KB
testcase_18 AC 907 ms
79,200 KB
testcase_19 AC 1,221 ms
82,304 KB
testcase_20 AC 46 ms
49,492 KB
testcase_21 AC 45 ms
49,584 KB
testcase_22 AC 43 ms
49,588 KB
testcase_23 AC 551 ms
65,324 KB
testcase_24 AC 281 ms
60,900 KB
testcase_25 AC 187 ms
56,696 KB
testcase_26 AC 562 ms
64,068 KB
testcase_27 AC 225 ms
57,876 KB
testcase_28 AC 360 ms
61,136 KB
testcase_29 AC 146 ms
53,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static int[] values;
    static int[] orgs;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] lefts = new int[m];
        int[] rights = new int[m];
        TreeMap<Integer, Integer> compress = new TreeMap<>();
        long ans = n * (n + 1L) / 2;
        for (int i = 0; i < m; i++) {
            lefts[i] = sc.nextInt();
            rights[i] = sc.nextInt();
            compress.put(lefts[i], null);
            compress.put(rights[i], null);
        }
        int size = compress.size();
        orgs = new int[size];
        int idx = 0;
        for (int x : compress.keySet()) {
            ans -= x;
            compress.put(x, idx);
            orgs[idx] = x;
            idx++;
        }
        values = new int[size];
        Arrays.fill(values, -1);
        for (int i = 0; i < size; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            graph.get(compress.get(rights[i])).add(compress.get(lefts[i]));
        }
        for (int i = size - 1; i >= 0; i--) {
            check(i, orgs[i]);
            ans += values[i];
        }
        System.out.println(ans);
    }
    
    static void check(int x, int v) {
        if (values[x] < 0) {
            values[x] = v;
            for (int y : graph.get(x)) {
                check(y, v);
            }
        }
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0