結果

問題 No.1300 Sum of Inversions
ユーザー ks2mks2m
提出日時 2020-11-28 00:08:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 671 ms / 2,000 ms
コード長 1,632 bytes
コンパイル時間 3,794 ms
コンパイル使用メモリ 80,912 KB
実行使用メモリ 85,388 KB
最終ジャッジ日時 2023-10-09 22:13:48
合計ジャッジ時間 23,045 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
49,636 KB
testcase_01 AC 48 ms
49,780 KB
testcase_02 AC 48 ms
49,800 KB
testcase_03 AC 671 ms
78,912 KB
testcase_04 AC 548 ms
74,780 KB
testcase_05 AC 470 ms
73,960 KB
testcase_06 AC 607 ms
78,404 KB
testcase_07 AC 611 ms
78,096 KB
testcase_08 AC 608 ms
83,144 KB
testcase_09 AC 621 ms
79,356 KB
testcase_10 AC 406 ms
66,872 KB
testcase_11 AC 410 ms
66,872 KB
testcase_12 AC 528 ms
76,672 KB
testcase_13 AC 566 ms
74,572 KB
testcase_14 AC 635 ms
82,984 KB
testcase_15 AC 609 ms
79,484 KB
testcase_16 AC 612 ms
79,092 KB
testcase_17 AC 390 ms
66,804 KB
testcase_18 AC 436 ms
67,924 KB
testcase_19 AC 501 ms
74,072 KB
testcase_20 AC 508 ms
74,572 KB
testcase_21 AC 470 ms
74,048 KB
testcase_22 AC 464 ms
73,940 KB
testcase_23 AC 625 ms
78,924 KB
testcase_24 AC 474 ms
74,160 KB
testcase_25 AC 430 ms
70,000 KB
testcase_26 AC 429 ms
68,352 KB
testcase_27 AC 477 ms
71,616 KB
testcase_28 AC 651 ms
85,212 KB
testcase_29 AC 521 ms
74,724 KB
testcase_30 AC 608 ms
79,320 KB
testcase_31 AC 466 ms
73,736 KB
testcase_32 AC 524 ms
73,244 KB
testcase_33 AC 302 ms
79,440 KB
testcase_34 AC 368 ms
85,388 KB
testcase_35 AC 403 ms
78,172 KB
testcase_36 AC 386 ms
85,344 KB
権限があれば一括ダウンロードができます

ソースコード

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(" ");
		Obj[] arr = new Obj[n];
		for (int i = 0; i < n; i++) {
			Obj o = new Obj();
			o.i = i + 1;
			o.a = Integer.parseInt(sa[i]);
			arr[i] = o;
		}
		br.close();

		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);
			c2 %= mod;
			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);
			c1 %= mod;
			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