結果

問題 No.1300 Sum of Inversions
ユーザー ks2mks2m
提出日時 2020-11-27 23:11:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 891 ms / 2,000 ms
コード長 1,747 bytes
コンパイル時間 2,182 ms
コンパイル使用メモリ 77,356 KB
実行使用メモリ 78,776 KB
最終ジャッジ日時 2023-10-09 21:05:11
合計ジャッジ時間 26,101 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
50,104 KB
testcase_01 AC 54 ms
49,968 KB
testcase_02 AC 52 ms
50,140 KB
testcase_03 AC 724 ms
78,776 KB
testcase_04 AC 669 ms
78,292 KB
testcase_05 AC 627 ms
72,080 KB
testcase_06 AC 732 ms
77,420 KB
testcase_07 AC 764 ms
78,372 KB
testcase_08 AC 809 ms
72,988 KB
testcase_09 AC 724 ms
72,836 KB
testcase_10 AC 518 ms
67,616 KB
testcase_11 AC 506 ms
66,916 KB
testcase_12 AC 695 ms
78,256 KB
testcase_13 AC 718 ms
78,160 KB
testcase_14 AC 891 ms
72,908 KB
testcase_15 AC 777 ms
72,832 KB
testcase_16 AC 738 ms
77,988 KB
testcase_17 AC 528 ms
66,912 KB
testcase_18 AC 566 ms
66,856 KB
testcase_19 AC 631 ms
74,708 KB
testcase_20 AC 637 ms
74,760 KB
testcase_21 AC 669 ms
74,584 KB
testcase_22 AC 630 ms
71,780 KB
testcase_23 AC 743 ms
77,096 KB
testcase_24 AC 606 ms
72,676 KB
testcase_25 AC 526 ms
66,688 KB
testcase_26 AC 548 ms
68,868 KB
testcase_27 AC 597 ms
72,808 KB
testcase_28 AC 756 ms
72,728 KB
testcase_29 AC 654 ms
75,096 KB
testcase_30 AC 748 ms
73,136 KB
testcase_31 AC 573 ms
69,748 KB
testcase_32 AC 659 ms
74,284 KB
testcase_33 AC 370 ms
77,708 KB
testcase_34 AC 431 ms
72,172 KB
testcase_35 AC 460 ms
76,508 KB
testcase_36 AC 429 ms
71,900 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(" ");
		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);
			c2 %= mod;
			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);
			c1 %= mod;
			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