提出 #74704742


ソースコード 拡げる

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

public class Main {
    static class BinaryIndexedTree {
        int size;
        long[] array;
        public BinaryIndexedTree(int size) {
            this.size = size;
            array = new long[size + 1];
        }
        public void update(int idx, int val) {
            for (idx++; idx <= size; idx += idx & -idx) {
                array[idx] += val;
            }
        }
        public long query(int idx) {
            long sum = 0;
            for (idx++; idx > 0; idx -= idx & -idx) {
                sum += array[idx];
            }
            return sum;
        }
        public long query(int lef, int rig) {
            if (lef > rig) return 0;
            return query(rig) - query(lef - 1);
        }
    }
    public static void main(String[] args) {
        InputReader reader = new InputReader(System.in);
        PrintWriter writer = new PrintWriter(System.out, false);
        int N = reader.nextInt();
        int K = reader.nextInt();
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = reader.nextInt();
        }
        writer.println(countAtMost(A, K) - countAtMost(A, K - 1));
        writer.close();
        System.exit(0);
    }

    private static long countAtMost(int[] A, int K) {
        if (K < 0) return 0;
        int N = A.length;
        long result = 0, inversion = 0;
        BinaryIndexedTree binaryIndexedTree = new BinaryIndexedTree(N + 1);
        for (int r = 0, l = 0; r < N; r++) {
            inversion += binaryIndexedTree.query(A[r] + 1, N);
            binaryIndexedTree.update(A[r], 1);
            while (inversion > K) {
                inversion -= binaryIndexedTree.query(A[l] - 1);
                binaryIndexedTree.update(A[l++], -1);
            }
            result += r - l + 1;
        }
        return result;
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }
        public long nextLong() {
            return Long.parseLong(next());
        }
        public double nextDouble() {
            return Double.parseDouble(next());
        }
        public String nextLine() {
            String str = "";
            try {
                str = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}

提出情報

提出日時
問題 F - Interval Inversion Count
ユーザ UttamS
言語 Java24 (OpenJDK 24.0.2)
得点 0
コード長 3146 Byte
結果 RE
実行時間 317 ms
メモリ 67576 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 500
結果
AC × 3
AC × 35
RE × 20
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 51 ms 39076 KiB
00_sample_01.txt AC 49 ms 39456 KiB
00_sample_02.txt AC 49 ms 39116 KiB
01_random_03.txt RE 42 ms 38076 KiB
01_random_04.txt RE 43 ms 38076 KiB
01_random_05.txt AC 203 ms 57956 KiB
01_random_06.txt RE 41 ms 38660 KiB
01_random_07.txt RE 42 ms 38168 KiB
01_random_08.txt RE 42 ms 38508 KiB
01_random_09.txt AC 228 ms 58884 KiB
01_random_10.txt RE 42 ms 38488 KiB
01_random_11.txt RE 43 ms 38316 KiB
01_random_12.txt AC 316 ms 66092 KiB
01_random_13.txt RE 43 ms 38676 KiB
01_random_14.txt RE 43 ms 38408 KiB
01_random_15.txt RE 43 ms 38204 KiB
01_random_16.txt AC 311 ms 66852 KiB
01_random_17.txt RE 42 ms 37936 KiB
01_random_18.txt RE 41 ms 38316 KiB
01_random_19.txt AC 317 ms 67016 KiB
01_random_20.txt RE 41 ms 37928 KiB
01_random_21.txt RE 41 ms 38304 KiB
01_random_22.txt RE 42 ms 38336 KiB
01_random_23.txt AC 315 ms 66684 KiB
01_random_24.txt AC 220 ms 67104 KiB
01_random_25.txt RE 42 ms 38076 KiB
01_random_26.txt RE 42 ms 38408 KiB
01_random_27.txt RE 42 ms 38120 KiB
01_random_28.txt RE 42 ms 38392 KiB
01_random_29.txt RE 42 ms 38308 KiB
01_random_30.txt AC 225 ms 66696 KiB
01_random_31.txt AC 269 ms 66368 KiB
01_random_32.txt AC 271 ms 66820 KiB
01_random_33.txt AC 268 ms 67416 KiB
01_random_34.txt AC 292 ms 66620 KiB
01_random_35.txt AC 257 ms 67076 KiB
01_random_36.txt AC 51 ms 39116 KiB
01_random_37.txt AC 260 ms 67284 KiB
01_random_38.txt AC 272 ms 67064 KiB
01_random_39.txt AC 247 ms 66868 KiB
01_random_40.txt AC 255 ms 67156 KiB
01_random_41.txt AC 263 ms 66444 KiB
01_random_42.txt AC 269 ms 66024 KiB
01_random_43.txt AC 260 ms 67576 KiB
01_random_44.txt AC 267 ms 66404 KiB
01_random_45.txt AC 249 ms 67052 KiB
01_random_46.txt AC 261 ms 67400 KiB
01_random_47.txt AC 262 ms 66472 KiB
01_random_48.txt AC 244 ms 66376 KiB
01_random_49.txt AC 249 ms 67360 KiB
01_random_50.txt AC 244 ms 64884 KiB
01_random_51.txt AC 245 ms 65196 KiB
01_random_52.txt AC 251 ms 65076 KiB
01_random_53.txt AC 261 ms 65472 KiB
01_random_54.txt AC 258 ms 65908 KiB