結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2018-01-22 03:03:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,932 ms / 5,000 ms
コード長 4,620 bytes
コンパイル時間 2,668 ms
コンパイル使用メモリ 75,760 KB
実行使用メモリ 201,620 KB
最終ジャッジ日時 2023-08-26 20:13:30
合計ジャッジ時間 18,198 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 388 ms
62,840 KB
testcase_01 AC 499 ms
64,248 KB
testcase_02 AC 148 ms
57,396 KB
testcase_03 AC 1,254 ms
96,368 KB
testcase_04 AC 3,932 ms
201,620 KB
testcase_05 AC 1,534 ms
101,728 KB
testcase_06 AC 1,246 ms
113,696 KB
testcase_07 AC 1,348 ms
101,144 KB
testcase_08 AC 1,652 ms
103,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


class Main {
    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));
        PlusSegmentTree seg=new PlusSegmentTree();
        long[]left=new long[n];
        for(int i=0;i<n;++i){
            left[i]=seg.query(0,b[i]);
            seg.update(b[i],seg.query(b[i],b[i]+1)+1);
        }
        for(int i=0;i<n;++i)seg.update(i, 0);
        long[]right=new long[n];
        for(int i=n-1;i>=0;--i){
            right[i]=seg.query(b[i]+1,n);
            seg.update(b[i],seg.query(b[i],b[i]+1)+1);
        }
        long ans=0;
        for(int i=0;i<n;++i)ans+=(long)left[i]*(long)right[i];
        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;
        }
    }
}
class PlusSegmentTree {
    static final int SIZE = 1 << 20;
    long[] seg;
    PlusSegmentTree() {
	this.seg = new long[2 * SIZE];
    }
    void update(int x, long value) {
	x += SIZE - 1;
	this.seg[x] = value;
	while (x > 0) {
	    x = (x - 1) / 2;
	    this.seg[x] = this.seg[2 * x + 1] + this.seg[2 * x + 2];
	}
    }
    long query(int l, int r) {
	l += SIZE - 1;
	r += SIZE - 1;
        long y = 0;
	while (l < r) {
	    if ((l & 1) == 0) {
		y = y + this.seg[l];
	    }
	    if ((r & 1) == 0) {
		y = y + this.seg[r - 1];
	    }
	    l /= 2;
	    r = (r - 1) / 2;
	}
	return y;
    }
}
0