結果

問題 No.1300 Sum of Inversions
ユーザー ks2mks2m
提出日時 2020-11-27 23:09:25
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,719 bytes
コンパイル時間 2,437 ms
コンパイル使用メモリ 83,764 KB
実行使用メモリ 80,080 KB
最終ジャッジ日時 2024-07-26 19:30:27
合計ジャッジ時間 26,019 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
50,312 KB
testcase_01 AC 62 ms
50,516 KB
testcase_02 AC 61 ms
50,748 KB
testcase_03 AC 731 ms
77,968 KB
testcase_04 AC 676 ms
77,024 KB
testcase_05 AC 598 ms
70,000 KB
testcase_06 AC 715 ms
75,632 KB
testcase_07 AC 769 ms
76,936 KB
testcase_08 AC 796 ms
72,132 KB
testcase_09 AC 736 ms
72,280 KB
testcase_10 AC 535 ms
66,188 KB
testcase_11 AC 509 ms
65,572 KB
testcase_12 AC 671 ms
77,320 KB
testcase_13 AC 707 ms
76,660 KB
testcase_14 WA -
testcase_15 AC 717 ms
72,344 KB
testcase_16 AC 732 ms
76,776 KB
testcase_17 AC 521 ms
66,020 KB
testcase_18 AC 560 ms
70,592 KB
testcase_19 AC 588 ms
72,892 KB
testcase_20 AC 614 ms
73,184 KB
testcase_21 AC 654 ms
73,144 KB
testcase_22 AC 603 ms
69,980 KB
testcase_23 AC 767 ms
75,520 KB
testcase_24 AC 599 ms
70,676 KB
testcase_25 AC 554 ms
67,180 KB
testcase_26 AC 584 ms
67,512 KB
testcase_27 AC 567 ms
70,888 KB
testcase_28 AC 756 ms
72,116 KB
testcase_29 AC 622 ms
73,284 KB
testcase_30 AC 751 ms
72,292 KB
testcase_31 AC 616 ms
70,388 KB
testcase_32 AC 580 ms
70,692 KB
testcase_33 AC 412 ms
80,080 KB
testcase_34 AC 429 ms
71,604 KB
testcase_35 AC 444 ms
75,936 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