結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー 37zigen37zigen
提出日時 2020-04-20 22:28:12
言語 Java19
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,550 bytes
コンパイル時間 4,731 ms
コンパイル使用メモリ 77,916 KB
実行使用メモリ 106,136 KB
最終ジャッジ日時 2023-07-28 16:49:12
合計ジャッジ時間 25,610 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 581 ms
47,828 KB
testcase_01 AC 684 ms
48,568 KB
testcase_02 AC 271 ms
45,224 KB
testcase_03 AC 2,034 ms
103,312 KB
testcase_04 AC 4,359 ms
106,136 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class BIT {
	int n;
	long[] v;
	
	public BIT(int n) {
		this.n=n;
		v=new long[n+1];
	}
	
	void add(int k,int val) {
		for (k++;k<=n;k+=k&-k) v[k]+=val;
	}
	
	long sum(int k) {
		long ret=0;
		for (;k>0;k-=k&-k) {
			ret+=v[k];
		}
		return ret;
	}
	
	long sum(int l,int r) {
		++l;++r;
		return sum(r-1)-sum(l-1);
	}
}

public class Main {
	
	public static void main(String[] args) {
		new Main().run();
	}
	
	void run() {
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		int[][] A=new int[N][2];
		for (int i=0;i<N;++i) {
			A[i][0]=sc.nextInt();
			A[i][1]=i;
		}
		BIT bit=new BIT(N);
		Arrays.sort(A,Comparator.comparing((int[] a)->a[0]).thenComparing(a->a[1]));
		long ans=0;
		for (int i=0;i<N;++i) {
			int j=i;
			while (j+1<N && A[i][0]==A[j+1][0]) ++j;
			int L=0,R=j-i+1;
			for (int k=i;k<=j;++k) {
				ans+=bit.sum(0, A[k][1])*bit.sum(A[k][1]+1,N);
				++L;--R;
				if (k+1<=j) ans-=(A[k+1][1]-A[k][1]-1)*L*R;
			}
			for (int k=i;k<=j;++k) {
				bit.add((int)A[k][1], 1);
			}
			i=j;
		}
		Arrays.sort(A,Comparator.comparing((int[] a)->-a[0]).thenComparing(a->a[1]));
		bit=new BIT(N);
		for (int i=0;i<N;++i) {
			int j=i;
			while (j+1<N && A[i][0]==A[j+1][0]) ++j;
			for (int k=i;k<=j;++k) {
				ans+=bit.sum(0, A[k][1])*bit.sum(A[k][1]+1,N);
			}
			for (int k=i;k<=j;++k) {
				bit.add((int)A[k][1], 1);
			}
			i=j;
		}
		System.out.println(ans);
	}
	
	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}

0