結果

問題 No.1300 Sum of Inversions
ユーザー ks2mks2m
提出日時 2020-11-27 23:03:33
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,601 bytes
コンパイル時間 2,953 ms
コンパイル使用メモリ 81,548 KB
実行使用メモリ 79,032 KB
最終ジャッジ日時 2023-10-09 21:00:31
合計ジャッジ時間 21,549 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
50,216 KB
testcase_01 AC 48 ms
50,004 KB
testcase_02 AC 46 ms
50,120 KB
testcase_03 AC 597 ms
79,032 KB
testcase_04 AC 582 ms
78,384 KB
testcase_05 AC 528 ms
71,768 KB
testcase_06 AC 587 ms
77,056 KB
testcase_07 AC 615 ms
78,472 KB
testcase_08 AC 588 ms
72,940 KB
testcase_09 AC 570 ms
72,544 KB
testcase_10 AC 421 ms
66,604 KB
testcase_11 AC 412 ms
66,680 KB
testcase_12 AC 604 ms
78,224 KB
testcase_13 AC 603 ms
77,572 KB
testcase_14 WA -
testcase_15 AC 603 ms
72,964 KB
testcase_16 AC 555 ms
77,828 KB
testcase_17 AC 421 ms
66,612 KB
testcase_18 AC 434 ms
66,800 KB
testcase_19 AC 514 ms
75,336 KB
testcase_20 AC 519 ms
74,692 KB
testcase_21 AC 523 ms
74,704 KB
testcase_22 AC 468 ms
74,168 KB
testcase_23 AC 626 ms
76,936 KB
testcase_24 AC 528 ms
72,288 KB
testcase_25 AC 438 ms
67,088 KB
testcase_26 AC 415 ms
68,228 KB
testcase_27 AC 502 ms
72,208 KB
testcase_28 AC 607 ms
72,800 KB
testcase_29 AC 586 ms
74,884 KB
testcase_30 AC 594 ms
73,152 KB
testcase_31 AC 498 ms
72,392 KB
testcase_32 AC 499 ms
72,196 KB
testcase_33 AC 320 ms
77,376 KB
testcase_34 AC 356 ms
72,424 KB
testcase_35 AC 354 ms
76,212 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) -> o1.a - o2.a);

		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