結果

問題 No.1036 Make One With GCD 2
ユーザー tentententen
提出日時 2023-06-08 09:07:23
言語 Java
(openjdk 23)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,427 bytes
コンパイル時間 3,178 ms
コンパイル使用メモリ 92,404 KB
実行使用メモリ 96,220 KB
最終ジャッジ日時 2024-12-30 15:53:19
合計ジャッジ時間 39,098 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        SegmentTree st = new SegmentTree(n);
        for (int i = 0; i < n; i++) {
            st.setValue(i, sc.nextLong());
        }
        int right = 0;
        long ans = 0;
        for (int i = 0; i < n; i++) {
            right = Math.max(i, right);
            while (right < n && st.getValue(i, right + 1) > 1) {
                right++;
            }
            if (right >= n) {
                break;
            }
            ans += n - right;
        }
        System.out.println(ans);
    }
    
    static class SegmentTree {
        int size;
        long[] tree;
        
        public SegmentTree(int x) {
            size = 1;
            while (x >= size) {
                size <<= 1;
            }
            tree = new long[size * 2 - 1];
        }
        
        public void setValue(int idx, long value) {
            int current = size - 1 + idx;
            tree[current] = value;
            calc((current - 1) / 2);
        }
        
        private void calc(int idx) {
            tree[idx] = getGCD(tree[idx * 2 + 1], tree[idx * 2 + 2]);
            if (idx > 0) {
                calc((idx - 1) / 2);
            }
        }
        
        public long getValue(int left, int right) {
            return getTreeValue(0, 0, size, left, right);
        }
        
        public long getTreeValue(int idx, int min, int max, int left, int right) {
            if (left >= max || right <= min) {
                return 0;
            }
            if (left <= min && max <= right) {
                return tree[idx];
            }
            return getGCD(getTreeValue(idx * 2 + 1, min, (min + max) / 2, left, right), getTreeValue(idx * 2 + 2, (min + max) / 2, max, left, right));
        }
        
        private long getGCD(long x, long y) {
            if (y == 0) {
                return x;
            } else if (x == 0) {
                return y;
            } else {
                return getGCD(y, x % y);
            }
        }
    }
}
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