結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2018-01-22 03:21:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,326 ms / 5,000 ms
コード長 4,499 bytes
コンパイル時間 2,474 ms
コンパイル使用メモリ 75,716 KB
実行使用メモリ 202,324 KB
最終ジャッジ日時 2023-09-11 04:14:01
合計ジャッジ時間 15,665 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 403 ms
68,764 KB
testcase_01 AC 515 ms
69,724 KB
testcase_02 AC 143 ms
61,980 KB
testcase_03 AC 1,020 ms
112,744 KB
testcase_04 AC 3,326 ms
202,324 KB
testcase_05 AC 1,279 ms
111,836 KB
testcase_06 AC 1,132 ms
111,040 KB
testcase_07 AC 1,238 ms
113,548 KB
testcase_08 AC 1,752 ms
113,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


class Main {
    static final int SIZE=1<<20;
    static long[]bit;
    static void add(int v,long x){
        while(v<SIZE){
            bit[v]+=x;
            v+=v&(-v);
        }
    }
    static long acc(int v){
        long a=0;
        while(v>0){
            a+=bit[v];
            v-=v&(-v);
        }
        return a;
    }
    static void init(){
        if(bit==null)bit=new long[SIZE];
        for(int i=0;i<SIZE;++i)bit[i]=0;
    }
    static final long calc(int[] a){
        int n=a.length;
        int[]b=a.clone();
        {
            // coord comp
            int[]c=b.clone();
            Arrays.sort(c);
            int pos=0;
            Map<Integer,Integer>hm=new HashMap<Integer,Integer>();
            for(int i=0;i<n;++i){
                if(i==0||c[i]!=c[i-1]){
                    hm.put(c[i],pos);
                    pos++;
                }
            }
            for(int i=0;i<n;++i)b[i]=hm.get(b[i]);
        }
        //System.err.println("a="+Arrays.toString(a));
        //System.err.println("coord(a)="+Arrays.toString(b));
        init();
        long[]left=new long[n];
        for(int i=0;i<n;++i){
            left[i]=acc(b[i]);
            add(b[i]+1,1);
        }
        init();
        long[]right=new long[n];
        for(int i=n-1;i>=0;--i){
            right[i]=acc(SIZE-1)-acc(b[i]+1);
            add(b[i]+1,1);
        }
        long ans=0;
        for(int i=0;i<n;++i)ans+=(long)left[i]*(long)right[i];
        //System.err.println("left="+Arrays.toString(left));
        //System.err.println("right="+Arrays.toString(right));
        //System.err.println("ans="+ans);
        return ans;
    }
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n=sc.nextInt();
        int[]a=sc.nextIntArray(n);
        int[]b=a.clone();
        Arrays.sort(b);
        long dis=0;
        int pos=0;
        int[]e=new int[n],f=new int[n];
        {
            int cnt=0;
            for(int i=0;i<n;++i){
                if(i>0&&b[i]!=b[i-1]){
                    e[pos]=b[i-1];
                    f[pos]=cnt;
                    pos++;
                    cnt=0;
                }
                cnt++;
            }
            e[pos]=b[n-1];
            f[pos]=cnt;
            pos++;
            e=Arrays.copyOf(e,pos);
            f=Arrays.copyOf(f,pos);
        }
        dis=(long)n*(long)(n-1)*(long)(n-2);
        for(int i=0;i<pos;++i){
            long tmp=f[i];
            tmp=tmp*(tmp-1)*(n-tmp)*3;
            dis-=tmp;
            tmp=f[i];
            tmp=tmp*(tmp-1)*(tmp-2);
            dis-=tmp;
        }
        //System.err.println(Arrays.toString(e));
        //System.err.println(Arrays.toString(f));
        //System.err.println(dis);
        dis/=6;
        // eliminate increasing subseq. of length 3
        // eliminate decreasing subseq. of length 3
        dis-=calc(a);
        for(int i=0;i<n;++i)a[i]=-a[i];
        dis-=calc(a);
        out.println(dis);
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        int[] nextIntArray(int n){
            int[]r=new int[n];
            for(int i=0;i<n;++i)r[i]=nextInt();
            return r;
        }
    }
}
0