結果

問題 No.1300 Sum of Inversions
ユーザー ks2mks2m
提出日時 2020-11-27 23:07:39
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,683 bytes
コンパイル時間 3,624 ms
コンパイル使用メモリ 81,116 KB
実行使用メモリ 78,324 KB
最終ジャッジ日時 2023-10-09 21:02:57
合計ジャッジ時間 26,306 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
51,592 KB
testcase_01 AC 52 ms
49,692 KB
testcase_02 AC 53 ms
50,000 KB
testcase_03 AC 695 ms
78,324 KB
testcase_04 AC 686 ms
77,992 KB
testcase_05 AC 599 ms
72,016 KB
testcase_06 AC 757 ms
76,980 KB
testcase_07 AC 757 ms
77,972 KB
testcase_08 AC 784 ms
73,420 KB
testcase_09 AC 699 ms
72,816 KB
testcase_10 AC 495 ms
66,612 KB
testcase_11 AC 484 ms
66,484 KB
testcase_12 AC 722 ms
78,016 KB
testcase_13 AC 680 ms
77,592 KB
testcase_14 WA -
testcase_15 AC 767 ms
73,304 KB
testcase_16 AC 739 ms
78,324 KB
testcase_17 AC 481 ms
66,988 KB
testcase_18 AC 539 ms
66,556 KB
testcase_19 AC 608 ms
74,064 KB
testcase_20 AC 641 ms
74,252 KB
testcase_21 AC 632 ms
74,560 KB
testcase_22 AC 613 ms
71,940 KB
testcase_23 AC 742 ms
77,592 KB
testcase_24 AC 585 ms
72,596 KB
testcase_25 AC 584 ms
66,916 KB
testcase_26 AC 540 ms
68,992 KB
testcase_27 AC 591 ms
74,980 KB
testcase_28 AC 758 ms
73,160 KB
testcase_29 AC 615 ms
74,920 KB
testcase_30 AC 741 ms
72,676 KB
testcase_31 AC 548 ms
70,988 KB
testcase_32 AC 592 ms
71,604 KB
testcase_33 AC 331 ms
76,528 KB
testcase_34 AC 414 ms
72,144 KB
testcase_35 AC 410 ms
77,120 KB
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		int[] a = new int[n];
		
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}
		br.close();

		Obj[] arr = new Obj[n];
		for (int i = 0; i < n; i++) {
			Obj o = new Obj();
			o.i = i + 1;
			o.a = a[i];
			arr[i] = o;
		}
		Arrays.sort(arr, (o1, o2) -> {
			if (o1.a != o2.a) {
				return o1.a - o2.a;
			} else {
				return o1.i - o2.i;
			}
		});

		int mod = 998244353;
		BIT bit1 = new BIT(n + 1);
		BIT bit1c = new BIT(n + 1);
		BIT bit2 = new BIT(n + 1);
		BIT bit2c = new BIT(n + 1);
		long ans = 0;
		for (int i = 0; i < n; i++) {
			Obj o = arr[i];
			long v2 = bit2.sum(n) - bit2.sum(o.i);
			long c2 = bit2c.sum(n) - bit2c.sum(o.i);
			long val = o.a * c2 + v2;
			ans += val;
			ans %= mod;

			long v1 = bit1.sum(n) - bit1.sum(o.i);
			long c1 = bit1c.sum(n) - bit1c.sum(o.i);
			long p = o.a * c1 + v1;
			bit2.add(o.i, p % mod);
			bit2c.add(o.i, c1);

			bit1.add(o.i, o.a);
			bit1c.add(o.i, 1);
		}
		System.out.println(ans);
	}

	static class Obj {
		int i, a;
	}

	static class BIT {
		int n;
		long[] arr;

		public BIT(int n) {
			this.n = n;
			arr = new long[n + 1];
		}

		void add(int idx, long val) {
			for (int i = idx; i <= n; i += i & -i) {
				arr[i] += val;
			}
		}

		long sum(int idx) {
			long sum = 0;
			for (int i = idx; i > 0; i -= i & -i) {
				sum += arr[i];
			}
			return sum;
		}
	}
}
0