Submission #74707161


Source Code Expand

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

@SuppressWarnings("DuplicatedCode")
public class Main {
    static int MAX_STRING_LENGTH = (int) 1e6; // 字符串最大长度
    static int MAX_DIGIT_LENGTH = (int) 1e6; // 数位最大长度
    static int MOD7 = (int) 1e9 + 7, MOD8 = 80112002, MOD9 = 998244353, inf = Integer.MAX_VALUE;

    /***** 检查 T *****/
    static class Solution {

        void solve() {
            int n = io.nextInt();
            long k = io.nextLong();
            int[] a = io.nextIntArray(n);
            long ans = 0;
            long cntl = 0, cntr = 0;
            Fenwick ltree = new Fenwick(n + 1), rtree = new Fenwick(n + 1);
            int l = 0, r = 0;
            for (int i = 0; i < n; i++) {
                int x = a[i];
                cntl += ltree.query(x + 1, n);
                ltree.add(x, 1);
                while (l < i && cntl > k) {
                    int v = a[l];
                    cntl -= ltree.pre(v - 1);
                    ltree.add(v, -1);
                    l++;
                }

                cntr += rtree.query(x + 1, n);
                rtree.add(x, 1);
                while (r <= i && cntr >= k) {
                    int v = a[r];
                    cntr -= rtree.pre(v - 1);
                    rtree.add(v, -1);
                    r++;
                }

                if (cntl == k) ans += r - l;
            }
            io.println(ans);
        }

        static class Fenwick {
            int[] tree;
            int n;

            Fenwick(int n) {
                tree = new int[n + 1];
                this.n = n + 1;
            }

            void add(int i, int v) {
                for (i++; i < n; i += i & -i) {
                    tree[i] += v;
                }
            }

            int query(int l, int r) {
                return pre(r) - pre(l - 1);
            }

            int pre(int i) {
                int s = 0;
                for (i++; i > 0; i &= i - 1) {
                    s += tree[i];
                }
                return s;
            }
        }


    }


    public static void main(String[] args) {
        io = new IO();
        int T = 1;
        // T = io.nextInt();
        for (; T > 0; T--) {
            new Solution().solve();
        }
        io.close();
    }

    static IO io;
    static class IO {
        BufferedInputStream bis = new BufferedInputStream(System.in);
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));

        int nextInt() {
            return (int) nextLong();
        }

        long nextLong() {
            try {
                long sign = 1, x = 0;
                int c = bis.read();
                while (c < '0' || c > '9') {
                    if (c == '-') sign = -1;
                    c = bis.read();
                }
                while (c >= '0' && c <= '9') {
                    x = x * 10 + (c - '0');
                    c = bis.read();
                }
                return sign * x;
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage());
            }
        }

        int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                try {
                    int sign = 1, x = 0;
                    int c = bis.read();
                    while (c < '0' || c > '9') {
                        if (c == '-') sign = -1;
                        c = bis.read();
                    }
                    while (c >= '0' && c <= '9') {
                        x = x * 10 + (c - '0');
                        c = bis.read();
                    }
                    a[i] = sign * x;
                } catch (IOException e) {
                    throw new RuntimeException(e.getMessage());
                }
            }
            return a;
        }

        long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) {
                try {
                    long sign = 1, x = 0;
                    int c = bis.read();
                    while (c < '0' || c > '9') {
                        if (c == '-') sign = -1;
                        c = bis.read();
                    }
                    while (c >= '0' && c <= '9') {
                        x = x * 10 + (c - '0');
                        c = bis.read();
                    }
                    a[i] = sign * x;
                } catch (IOException e) {
                    throw new RuntimeException(e.getMessage());
                }
            }
            return a;
        }

        int[] nextDigitArray(int n) {
            try {
                int[] a = new int[n];
                int x = bis.read();
                while (ignore(x)) {
                    x = bis.read();
                }
                int i = 0;
                while (!ignore(x)) {
                    a[i++] = x - '0';
                    x = bis.read();
                }
                return a;
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        }

        private int[] DIGIT_ARRAY;

        int[] nextDigitArray() {
            try {
                if (DIGIT_ARRAY == null) {
                    DIGIT_ARRAY = new int[MAX_DIGIT_LENGTH];
                }
                int x = bis.read();
                while (ignore(x)) {
                    x = bis.read();
                }
                int m = 0;
                while (!ignore(x)) {
                    DIGIT_ARRAY[m++] = x - '0';
                    x = bis.read();
                }
                int[] a = new int[m];
                System.arraycopy(DIGIT_ARRAY, 0, a, 0, m);
                return a;
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        }

        char nextChar() {
            try {
                int x = bis.read();
                while (ignore(x)) {
                    x = bis.read();
                }
                return (char) x;
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        }

        String[] nextStringArray(int n) {
            String[] a = new String[n];
            for (int i = 0; i < n; i++) {
                try {
                    if (CHAR_ARRAY == null) {
                        CHAR_ARRAY = new char[MAX_STRING_LENGTH];
                    }
                    int x = bis.read();
                    while (ignore(x)) {
                        x = bis.read();
                    }
                    int m = 0;
                    while (x != ' ' && x != ',' && !ignore(x)) {
                        CHAR_ARRAY[m++] = (char) x;
                        x = bis.read();
                    }
                    a[i] = new String(CHAR_ARRAY, 0, m);
                } catch (IOException e) {
                    throw new RuntimeException(e.getMessage());
                }
            }
            return a;
        }

        private char[] CHAR_ARRAY;

        char[] nextCharArray(boolean line) {
            try {
                if (CHAR_ARRAY == null) {
                    CHAR_ARRAY = new char[MAX_STRING_LENGTH];
                }
                int x = bis.read();
                while (ignore(x)) {
                    x = bis.read();
                }
                int m = 0;
                while (line ? (x == ' ' || x == ',' || !ignore(x)) : !ignore(x)) {
                    CHAR_ARRAY[m++] = (char) x;
                    x = bis.read();
                }
                char[] a = new char[m];
                System.arraycopy(CHAR_ARRAY, 0, a, 0, m);
                return a;
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        }

        char[] nextCharArray(int n) {
            try {
                char[] a = new char[n];
                int x = bis.read();
                while (ignore(x)) {
                    x = bis.read();
                }
                int i = 0;
                while (!ignore(x)) {
                    a[i++] = (char) x;
                    x = bis.read();
                }
                return a;
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        }

        private boolean ignore(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        String nextString(boolean line) {
            return new String(nextCharArray(line));
        }

        String nextString() {
            return nextString(false);
        }

        boolean nextBoolean() {
            return "true".equalsIgnoreCase(nextString());
        }

        double nextDouble() {
            return Double.parseDouble(nextString());
        }

        void println(Object obj) {
            print(obj);
            print("\n");
        }

        void println() {
            print('\n');
        }

        void print(Object obj) {
            pw.print(obj);
        }

        void printf(String format, Object... obj) {
            pw.printf(format, obj);
        }

        void close() {
            try {
                if (bis != null) bis.close();
                if (pw != null) {
                    pw.flush();
                    pw.close();
                }
                bis = null;
                pw = null;
            } catch (Exception ignore) {
            }
        }
    }
}

Submission Info

Submission Time
Task F - Interval Inversion Count
User gaoyuliang
Language Java24 (OpenJDK 24.0.2)
Score 500
Code Size 9997 Byte
Status AC
Exec Time 246 ms
Memory 47048 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 55
Set Name Test Cases
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
Case Name Status Exec Time Memory
00_sample_00.txt AC 44 ms 38004 KiB
00_sample_01.txt AC 40 ms 38004 KiB
00_sample_02.txt AC 40 ms 37860 KiB
01_random_03.txt AC 174 ms 44644 KiB
01_random_04.txt AC 164 ms 44216 KiB
01_random_05.txt AC 125 ms 42844 KiB
01_random_06.txt AC 127 ms 43244 KiB
01_random_07.txt AC 144 ms 43836 KiB
01_random_08.txt AC 137 ms 43040 KiB
01_random_09.txt AC 138 ms 43108 KiB
01_random_10.txt AC 244 ms 47048 KiB
01_random_11.txt AC 246 ms 46896 KiB
01_random_12.txt AC 238 ms 46660 KiB
01_random_13.txt AC 224 ms 46772 KiB
01_random_14.txt AC 237 ms 47020 KiB
01_random_15.txt AC 239 ms 46944 KiB
01_random_16.txt AC 238 ms 46816 KiB
01_random_17.txt AC 237 ms 47048 KiB
01_random_18.txt AC 219 ms 47012 KiB
01_random_19.txt AC 235 ms 46572 KiB
01_random_20.txt AC 214 ms 46580 KiB
01_random_21.txt AC 223 ms 46596 KiB
01_random_22.txt AC 203 ms 46568 KiB
01_random_23.txt AC 231 ms 46808 KiB
01_random_24.txt AC 191 ms 46016 KiB
01_random_25.txt AC 187 ms 46852 KiB
01_random_26.txt AC 179 ms 46652 KiB
01_random_27.txt AC 182 ms 46180 KiB
01_random_28.txt AC 181 ms 46352 KiB
01_random_29.txt AC 189 ms 46744 KiB
01_random_30.txt AC 189 ms 46224 KiB
01_random_31.txt AC 232 ms 47044 KiB
01_random_32.txt AC 218 ms 46820 KiB
01_random_33.txt AC 235 ms 46904 KiB
01_random_34.txt AC 246 ms 46984 KiB
01_random_35.txt AC 232 ms 47008 KiB
01_random_36.txt AC 41 ms 38020 KiB
01_random_37.txt AC 196 ms 46712 KiB
01_random_38.txt AC 202 ms 46764 KiB
01_random_39.txt AC 190 ms 46172 KiB
01_random_40.txt AC 204 ms 46860 KiB
01_random_41.txt AC 191 ms 46608 KiB
01_random_42.txt AC 181 ms 46308 KiB
01_random_43.txt AC 181 ms 45636 KiB
01_random_44.txt AC 202 ms 46936 KiB
01_random_45.txt AC 188 ms 46968 KiB
01_random_46.txt AC 185 ms 46832 KiB
01_random_47.txt AC 187 ms 47048 KiB
01_random_48.txt AC 190 ms 47044 KiB
01_random_49.txt AC 190 ms 46728 KiB
01_random_50.txt AC 189 ms 45872 KiB
01_random_51.txt AC 169 ms 45104 KiB
01_random_52.txt AC 173 ms 45708 KiB
01_random_53.txt AC 165 ms 45488 KiB
01_random_54.txt AC 180 ms 46204 KiB