結果

問題 No.1300 Sum of Inversions
ユーザー ks2mks2m
提出日時 2020-11-27 23:09:25
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,719 bytes
コンパイル時間 2,493 ms
コンパイル使用メモリ 84,192 KB
実行使用メモリ 78,456 KB
最終ジャッジ日時 2023-10-09 21:04:10
合計ジャッジ時間 24,117 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
49,920 KB
testcase_01 AC 48 ms
49,792 KB
testcase_02 AC 50 ms
49,884 KB
testcase_03 AC 696 ms
78,456 KB
testcase_04 AC 622 ms
78,072 KB
testcase_05 AC 544 ms
71,580 KB
testcase_06 AC 669 ms
77,328 KB
testcase_07 AC 687 ms
78,184 KB
testcase_08 AC 712 ms
73,360 KB
testcase_09 AC 674 ms
73,232 KB
testcase_10 AC 458 ms
66,644 KB
testcase_11 AC 450 ms
66,796 KB
testcase_12 AC 637 ms
77,920 KB
testcase_13 AC 637 ms
77,708 KB
testcase_14 WA -
testcase_15 AC 681 ms
72,636 KB
testcase_16 AC 657 ms
77,628 KB
testcase_17 AC 472 ms
66,892 KB
testcase_18 AC 498 ms
66,732 KB
testcase_19 AC 584 ms
74,568 KB
testcase_20 AC 560 ms
74,624 KB
testcase_21 AC 592 ms
74,428 KB
testcase_22 AC 542 ms
71,488 KB
testcase_23 AC 684 ms
77,092 KB
testcase_24 AC 554 ms
71,856 KB
testcase_25 AC 557 ms
67,152 KB
testcase_26 AC 479 ms
68,548 KB
testcase_27 AC 539 ms
72,480 KB
testcase_28 AC 693 ms
73,340 KB
testcase_29 AC 587 ms
74,724 KB
testcase_30 AC 620 ms
72,748 KB
testcase_31 AC 561 ms
71,612 KB
testcase_32 AC 536 ms
72,480 KB
testcase_33 AC 347 ms
77,548 KB
testcase_34 AC 375 ms
72,376 KB
testcase_35 AC 416 ms
76,352 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 % mod + v2 % mod;
			ans += val % mod;
			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 % mod + v1 % mod;
			bit2.add(o.i, p % mod);
			bit2c.add(o.i, c1 % mod);

			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