結果

問題 No.694 square1001 and Permutation 3
ユーザー 37zigen37zigen
提出日時 2018-05-24 01:25:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,642 ms / 3,000 ms
コード長 3,573 bytes
コンパイル時間 2,433 ms
コンパイル使用メモリ 80,352 KB
実行使用メモリ 90,036 KB
最終ジャッジ日時 2024-06-28 16:42:16
合計ジャッジ時間 13,404 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
36,604 KB
testcase_01 AC 51 ms
36,520 KB
testcase_02 AC 51 ms
36,740 KB
testcase_03 AC 59 ms
36,700 KB
testcase_04 AC 61 ms
37,024 KB
testcase_05 AC 56 ms
37,072 KB
testcase_06 AC 59 ms
36,792 KB
testcase_07 AC 1,150 ms
85,736 KB
testcase_08 AC 1,538 ms
84,600 KB
testcase_09 AC 1,618 ms
89,476 KB
testcase_10 AC 563 ms
73,548 KB
testcase_11 AC 1,619 ms
89,940 KB
testcase_12 AC 1,642 ms
90,036 KB
testcase_13 AC 50 ms
36,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Main {
	class BIT {
		int n;
		int[] a;

		public BIT(int n_) {
			n = n_;
			a = new int[n + 1];
		}

		void add(int k, int add) {
			++k;
			while (k < a.length) {
				a[k] += add;
				k += k & -k;
			}
		}

		int sum(int k) {
			++k;
			int ret = 0;
			while (k > 0) {
				ret += a[k];
				k -= k & -k;
			}
			return ret;
		}

		int sum(int a, int b) {
			return sum(b - 1) - sum(a - 1);
		}
	}

	void run() {
		FastScanner sc = new FastScanner();
		int N = (int) sc.nextLong();
		if (!(1 <= N && N <= 500000))
			throw new AssertionError();
		long[] A = new long[N];
		for (int i = 0; i < N; ++i) {
			A[i] = sc.nextLong();
			if (!(1 <= A[i] && A[i] <= 1000000000L))
				throw new AssertionError();
		}
		A = shrink(A);
		long sum = 0;
		BIT bit = new BIT(A.length);
		for (int i = 0; i < N; ++i) {
			sum += bit.sum((int) A[i] + 1, N);
			bit.add((int) A[i], 1);
		}
		PrintWriter pw = new PrintWriter(System.out);
		pw.println(sum);
		for (int i = 0; i < N - 1; i++) {
			sum -= bit.sum(0, (int) A[i]);
			sum += bit.sum((int) A[i] + 1, N);
			pw.println(sum);
		}
		pw.close();
	}

	long[] shrink(long[] A) {
		int N = A.length;
		long[] ret = new long[N];
		long[][] B = new long[N][2];
		for (int i = 0; i < N; ++i) {
			B[i][0] = i;
			B[i][1] = A[i];
		}
		Arrays.sort(B, new Comparator<long[]>() {
			@Override
			public int compare(long[] o1, long[] o2) {
				return Long.compare(o1[1], o2[1]);
			}
		});
		int p = 0;
		for (int i = 0; i < N; ++i) {
			ret[(int) B[i][0]] = p;
			if (i + 1 < N && B[i][1] == B[i + 1][1]) {
				continue;
			}
			++p;
		}
		return ret;
	}

	public static void main(String[] args) {
		new Main().run();
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

	class FastScanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

		public boolean hasNext() {
			skipUnprintable();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}
	}

}
0