結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー 37zigen37zigen
提出日時 2020-04-20 22:40:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,216 ms / 5,000 ms
コード長 3,596 bytes
コンパイル時間 2,703 ms
コンパイル使用メモリ 81,400 KB
実行使用メモリ 108,272 KB
最終ジャッジ日時 2024-04-16 01:04:04
合計ジャッジ時間 16,175 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 259 ms
45,028 KB
testcase_01 AC 327 ms
47,612 KB
testcase_02 AC 127 ms
39,388 KB
testcase_03 AC 758 ms
99,368 KB
testcase_04 AC 3,216 ms
104,468 KB
testcase_05 AC 942 ms
104,876 KB
testcase_06 AC 1,793 ms
105,684 KB
testcase_07 AC 2,152 ms
108,272 KB
testcase_08 AC 2,268 ms
108,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;
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() {
		FastScanner sc=new FastScanner();
		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-=(long)(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));}
}




class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
    public boolean hasNext() { skipUnprintable(); return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
    	return (int)nextLong();
    }
}
0